Files
wwjcloud-nest-v1/tools-v1/java-tools/generators/quality-gate.js
wanwujie 699680c93a feat: 重构v1框架架构和清理整理
- 将preset.ts移动到config目录,符合架构规范
- 迁移php-tools到java-tools,参考Java架构而非PHP
- 清理AI层文档,整合为单一README
- 删除core层,专注boot和ai层
- 集成AI层与Boot层,实现100%组件集成
- 清理废弃js文件和临时报告文件
- 更新导入路径,保持代码一致性
2025-10-20 23:07:37 +08:00

277 lines
7.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env node
const { execSync } = require('child_process');
const path = require('path');
const fs = require('fs');
/**
* Quality Gate - 质量门禁工具
* 执行 TypeScript 编译检查和 ESLint 检查
*/
class QualityGate {
constructor(nestjsBasePath) {
this.nestjsBasePath = nestjsBasePath || '/Users/wanwu/Documents/wwjcloud/wwjcloud-nsetjs/wwjcloud-nest-v1';
// Resolve project root for npm commands
this.projectRoot = this.findProjectRoot(this.nestjsBasePath) || this.nestjsBasePath;
// Resolve core directory to check (prefer libs/wwjcloud-core/src)
const libCore = path.join(this.projectRoot, 'libs', 'wwjcloud-core', 'src');
this.coreDir = libCore;
if (!fs.existsSync(this.coreDir)) {
console.warn(`⚠️ 核心目录不存在,预期为: ${this.coreDir}`);
}
this.stats = {
tsErrors: 0,
eslintErrors: 0,
eslintWarnings: 0,
filesChecked: 0
};
}
// Find nearest directory containing package.json, starting from provided path
findProjectRoot(startDir) {
let dir = startDir;
for (let i = 0; i < 4; i++) {
if (fs.existsSync(path.join(dir, 'package.json'))) return dir;
const parent = path.dirname(dir);
if (parent === dir) break;
dir = parent;
}
return null;
}
/**
* 运行所有质量检查
*/
async run() {
console.log('🚦 启动 Quality Gate 检查...\n');
let passed = true;
// TypeScript 编译检查
console.log('📝 第1阶段TypeScript 编译检查...');
const tsResult = await this.checkTypeScript();
if (!tsResult) {
passed = false;
console.log(' ❌ TypeScript 编译检查失败\n');
} else {
console.log(' ✅ TypeScript 编译检查通过\n');
}
// ESLint 检查
console.log('📝 第2阶段ESLint 代码规范检查...');
const eslintResult = await this.checkESLint();
if (!eslintResult) {
passed = false;
console.log(' ❌ ESLint 检查失败\n');
} else {
console.log(' ✅ ESLint 检查通过\n');
}
// 输出统计报告
this.printStats();
return passed;
}
/**
* TypeScript 编译检查
*/
async checkTypeScript() {
try {
console.log(' 🔍 检查 TypeScript 类型...');
const result = execSync('npm run type-check', {
cwd: this.projectRoot,
encoding: 'utf8',
stdio: 'pipe'
});
console.log(' ✅ TypeScript 类型检查通过');
return true;
} catch (error) {
this.stats.tsErrors++;
if (error.stdout) {
console.error(' ❌ TypeScript 错误:');
console.error(error.stdout);
}
if (error.stderr) {
console.error(error.stderr);
}
return false;
}
}
/**
* ESLint 检查
*/
async checkESLint() {
try {
console.log(' 🔍 检查代码规范...');
const result = execSync('npm run lint', {
cwd: this.projectRoot,
encoding: 'utf8',
stdio: 'pipe'
});
console.log(' ✅ ESLint 检查通过');
return true;
} catch (error) {
// ESLint 返回非零退出码表示有错误或警告
if (error.stdout) {
const output = error.stdout;
// 解析错误和警告数量
const errorMatch = output.match(/(\d+)\s+errors?/);
const warningMatch = output.match(/(\d+)\s+warnings?/);
if (errorMatch) {
this.stats.eslintErrors = parseInt(errorMatch[1]);
}
if (warningMatch) {
this.stats.eslintWarnings = parseInt(warningMatch[1]);
}
console.error(' ❌ ESLint 发现问题:');
console.error(output);
// 如果只有警告,不算失败
return this.stats.eslintErrors === 0;
}
return false;
}
}
/**
* 检查单个文件
*/
async checkFile(filePath) {
console.log(` 🔍 检查文件: ${filePath}`);
try {
execSync(`npx tsc --noEmit ${filePath}`, {
cwd: this.projectRoot,
encoding: 'utf8',
stdio: 'pipe'
});
execSync(`npx eslint ${filePath}`, {
cwd: this.projectRoot,
encoding: 'utf8',
stdio: 'pipe'
});
this.stats.filesChecked++;
return true;
} catch (error) {
console.error(` ❌ 文件检查失败: ${filePath}`);
if (error.stdout) {
console.error(error.stdout);
}
return false;
}
}
/**
* 快速检查(只检查核心层)
*/
async quickCheck() {
console.log('🚀 快速质量检查(仅核心层)...\n');
const coreFiles = this.getGeneratedFiles();
console.log(` 📁 发现 ${coreFiles.length} 个生成的文件\n`);
let passed = 0;
let failed = 0;
for (const file of coreFiles) {
const result = await this.checkFile(file);
if (result) {
passed++;
} else {
failed++;
}
}
console.log(`\n📊 快速检查结果:`);
console.log(` ✅ 通过: ${passed}`);
console.log(` ❌ 失败: ${failed}`);
return failed === 0;
}
/**
* 获取所有生成的文件
*/
getGeneratedFiles() {
const coreDir = this.coreDir;
const files = [];
const scanDir = (dir) => {
if (!fs.existsSync(dir)) return;
const entries = fs.readdirSync(dir, { withFileTypes: true });
for (const entry of entries) {
const fullPath = path.join(dir, entry.name);
if (entry.isDirectory()) {
scanDir(fullPath);
} else if (entry.name.endsWith('.ts') && !entry.name.endsWith('.d.ts')) {
files.push(fullPath);
}
}
};
scanDir(coreDir);
return files;
}
/**
* 输出统计报告
*/
printStats() {
console.log('📊 Quality Gate 统计报告');
console.log('==================================================');
console.log(` 📝 TypeScript 错误: ${this.stats.tsErrors}`);
console.log(` 📝 ESLint 错误: ${this.stats.eslintErrors}`);
console.log(` ⚠️ ESLint 警告: ${this.stats.eslintWarnings}`);
console.log(` 📁 检查文件数: ${this.stats.filesChecked}`);
console.log('==================================================');
const passed = this.stats.tsErrors === 0 && this.stats.eslintErrors === 0;
if (passed) {
console.log('\n✅ 🎉 所有质量检查通过!');
} else {
console.log('\n❌ 质量检查失败,请修复上述问题');
console.log('提示: 运行 "npm run lint:fix" 自动修复部分问题');
}
return passed;
}
}
// 如果直接运行此文件
if (require.main === module) {
const args = process.argv.slice(2);
const mode = args[0] || 'full';
const gate = new QualityGate();
if (mode === 'quick') {
gate.quickCheck().then(passed => {
process.exit(passed ? 0 : 1);
});
} else {
gate.run().then(passed => {
process.exit(passed ? 0 : 1);
});
}
}
module.exports = QualityGate;