Files
wwjcloud-nest-v1/wwjcloud-nest-v1/tools/tools-v1/java-tools/generators/quality-gate.js
wanwujie 0f105d3a21 🎯 重构目录结构:完成项目组织优化
- 将wwjcloud目录重命名为wwjcloud-nest-v1作为项目根目录
- 将原nestjs目录重命名为wwjcloud作为NestJS后端目录
- 实现真正的前后端分离架构
- 恢复工作区中丢失的目录结构
- 更新相关配置文件路径引用
- 清理重复和嵌套目录问题

目录结构:
wwjcloud-nest-v1/
├── wwjcloud/          # NestJS 后端
├── admin/             # 管理端前端
├── web/               # PC端前端
├── uni-app-x/         # 移动端前端
├── wwjcloud-web/      # 部署根目录
├── docker/            # Docker 配置
├── docs/              # 文档
└── tools/             # 工具集
2025-10-21 13:38:58 +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;