Files
wwjcloud-nest-v1/tools-v1/php-tools/generators/quality-gate.js
wanwujie b5826ee469 feat: 发布 v1 智能框架 0.1.0 版本
🚀 新增功能:
- wwjcloud-nest-v1: 完整的 NestJS 智能框架
- AI 自愈机制: @wwjcloud/auto-healing 模块
- 智能代码生成: tools-v1/php-tools 迁移工具链
- AI 能力规划: v1/docs/AI-CAPABILITY-ROADMAP.md

📦 核心模块:
- libs/wwjcloud-ai: AI 策略和恢复服务
- libs/wwjcloud-boot: 启动和配置管理
- libs/wwjcloud-core: 核心基础设施
- libs/wwjcloud-addon: 插件系统

🏗️ 架构特性:
- 分层渐进式 AI 策略
- 微服务导向的模块化设计
- 与 PHP 项目 100% 业务一致性
- Docker 容器化部署支持

📋 版本信息:
- 版本: v0.1.0
- 发布日期: 2025-01-25
- 分支: v1
2025-10-19 19:55:52 +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;