- 重构LanguageUtils为LanguageService,实现ILanguageService接口 - 移除自定义验证管道和装饰器,使用标准NestJS验证 - 集成框架ValidatorService进行业务验证 - 简化目录结构,移除不必要的子目录 - 支持模块化语言包加载(common、user、order等) - 统一API响应格式(code、msg、data、timestamp) - 添加ValidationExceptionFilter处理多语言验证错误 - 完善多语言示例和文档
140 lines
3.6 KiB
JavaScript
140 lines
3.6 KiB
JavaScript
#!/usr/bin/env node
|
||
|
||
const fs = require('fs');
|
||
const path = require('path');
|
||
|
||
/**
|
||
* 🛣️ 路由生成器
|
||
* 专门负责生成NestJS路由文件
|
||
*/
|
||
class RouteGenerator {
|
||
constructor() {
|
||
this.config = {
|
||
phpBasePath: '/Users/wanwu/Documents/wwjcloud/wwjcloud-nsetjs/niucloud-php/niucloud',
|
||
nestjsBasePath: '/Users/wanwu/Documents/wwjcloud/wwjcloud-nsetjs/wwjcloud-nest/src/core',
|
||
discoveryResultPath: '/Users/wanwu/Documents/wwjcloud/wwjcloud-nsetjs/tools/php-discovery-result.json'
|
||
};
|
||
|
||
this.discoveryData = null;
|
||
this.stats = {
|
||
routesCreated: 0,
|
||
errors: 0
|
||
};
|
||
}
|
||
|
||
/**
|
||
* 运行路由生成
|
||
*/
|
||
async run() {
|
||
try {
|
||
console.log('🛣️ 启动路由生成器...');
|
||
console.log('目标:生成NestJS路由文件\n');
|
||
|
||
// 加载PHP文件发现结果
|
||
await this.loadDiscoveryData();
|
||
|
||
// 生成路由
|
||
await this.generateRoutes();
|
||
|
||
// 输出统计报告
|
||
this.printStats();
|
||
|
||
} catch (error) {
|
||
console.error('❌ 路由生成失败:', error);
|
||
this.stats.errors++;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 加载PHP文件发现结果
|
||
*/
|
||
async loadDiscoveryData() {
|
||
try {
|
||
const data = fs.readFileSync(this.config.discoveryResultPath, 'utf8');
|
||
this.discoveryData = JSON.parse(data);
|
||
console.log(' ✅ 成功加载PHP文件发现结果');
|
||
} catch (error) {
|
||
console.error('❌ 加载发现结果失败:', error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 生成路由
|
||
*/
|
||
async generateRoutes() {
|
||
console.log(' 🔨 生成路由...');
|
||
|
||
for (const [layerName, routes] of Object.entries(this.discoveryData.routes)) {
|
||
for (const [routeName, routeInfo] of Object.entries(routes)) {
|
||
await this.createRoute(layerName, routeName, routeInfo);
|
||
this.stats.routesCreated++;
|
||
}
|
||
}
|
||
|
||
console.log(` ✅ 生成了 ${this.stats.routesCreated} 个路由`);
|
||
}
|
||
|
||
/**
|
||
* 创建路由 - NestJS不需要独立路由文件
|
||
*/
|
||
async createRoute(layerName, routeName, routeInfo) {
|
||
// NestJS不需要独立的路由文件
|
||
// 路由在控制器中定义,模块路由在app.module.ts中配置
|
||
console.log(` ⏭️ 跳过路由: ${layerName}/${this.toCamelCase(routeName)}.route.ts (NestJS不需要独立路由文件)`);
|
||
return;
|
||
}
|
||
|
||
/**
|
||
* 生成路由内容 - NestJS不需要独立的路由文件
|
||
* 路由在控制器中定义,这里生成模块路由配置
|
||
*/
|
||
generateRouteContent(layerName, routeName) {
|
||
// NestJS不需要独立的路由文件
|
||
// 路由应该在控制器中定义,模块路由在app.module.ts中配置
|
||
return null;
|
||
}
|
||
|
||
/**
|
||
* 转换为PascalCase
|
||
*/
|
||
toPascalCase(str) {
|
||
return str.charAt(0).toUpperCase() + str.slice(1);
|
||
}
|
||
|
||
/**
|
||
* 转换为camelCase
|
||
*/
|
||
toCamelCase(str) {
|
||
return str.charAt(0).toLowerCase() + str.slice(1);
|
||
}
|
||
|
||
/**
|
||
* 确保目录存在
|
||
*/
|
||
ensureDir(dirPath) {
|
||
if (!fs.existsSync(dirPath)) {
|
||
fs.mkdirSync(dirPath, { recursive: true });
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 输出统计报告
|
||
*/
|
||
printStats() {
|
||
console.log('\n📊 路由生成统计报告');
|
||
console.log('==================================================');
|
||
console.log(`✅ 创建路由数量: ${this.stats.routesCreated}`);
|
||
console.log(`❌ 错误数量: ${this.stats.errors}`);
|
||
console.log(`📈 成功率: ${this.stats.routesCreated > 0 ? '100.00%' : '0.00%'}`);
|
||
}
|
||
}
|
||
|
||
// 如果直接运行此文件
|
||
if (require.main === module) {
|
||
const generator = new RouteGenerator();
|
||
generator.run().catch(console.error);
|
||
}
|
||
|
||
module.exports = RouteGenerator;
|