2025-10-19 19:55:52 +08:00
|
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
|
|
|
|
const fs = require('fs');
|
|
|
|
|
|
const path = require('path');
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 🛣️ 路由生成器
|
2025-10-20 23:07:37 +08:00
|
|
|
|
* 专门负责生成NestJS路由文件 (参考Java架构)
|
2025-10-19 19:55:52 +08:00
|
|
|
|
*/
|
|
|
|
|
|
class RouteGenerator {
|
|
|
|
|
|
constructor() {
|
|
|
|
|
|
this.config = {
|
2025-10-20 23:07:37 +08:00
|
|
|
|
javaBasePath: '/Users/wanwu/Documents/wwjcloud/wwjcloud-nsetjs/niucloud-java/niucloud-core/src/main/java',
|
|
|
|
|
|
phpBasePath: '/Users/wanwu/Documents/wwjcloud/wwjcloud-nsetjs/niucloud-php/niucloud', // 仅用于业务逻辑提取
|
2025-10-19 19:55:52 +08:00
|
|
|
|
nestjsBasePath: '/Users/wanwu/Documents/wwjcloud/wwjcloud-nsetjs/wwjcloud-nest-v1/libs/wwjcloud-core/src',
|
2025-10-20 23:07:37 +08:00
|
|
|
|
discoveryResultPath: '/Users/wanwu/Documents/wwjcloud/wwjcloud-nsetjs/tools-v1/java-tools/java-discovery-result.json'
|
2025-10-19 19:55:52 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
this.discoveryData = null;
|
|
|
|
|
|
this.stats = {
|
|
|
|
|
|
routesCreated: 0,
|
|
|
|
|
|
errors: 0
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 运行路由生成
|
|
|
|
|
|
*/
|
|
|
|
|
|
async run() {
|
|
|
|
|
|
try {
|
|
|
|
|
|
console.log('🛣️ 启动路由生成器...');
|
|
|
|
|
|
console.log('目标:生成NestJS路由文件\n');
|
|
|
|
|
|
|
2025-10-20 23:07:37 +08:00
|
|
|
|
// 加载Java架构发现结果(含PHP业务逻辑)
|
2025-10-19 19:55:52 +08:00
|
|
|
|
await this.loadDiscoveryData();
|
|
|
|
|
|
|
|
|
|
|
|
// 生成路由
|
|
|
|
|
|
await this.generateRoutes();
|
|
|
|
|
|
|
|
|
|
|
|
// 输出统计报告
|
|
|
|
|
|
this.printStats();
|
|
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('❌ 路由生成失败:', error);
|
|
|
|
|
|
this.stats.errors++;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-10-20 23:07:37 +08:00
|
|
|
|
* 加载Java架构发现结果(含PHP业务逻辑)
|
2025-10-19 19:55:52 +08:00
|
|
|
|
*/
|
|
|
|
|
|
async loadDiscoveryData() {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const data = fs.readFileSync(this.config.discoveryResultPath, 'utf8');
|
|
|
|
|
|
this.discoveryData = JSON.parse(data);
|
2025-10-20 23:07:37 +08:00
|
|
|
|
console.log(' ✅ 成功加载Java架构发现结果(含PHP业务逻辑)');
|
2025-10-19 19:55:52 +08:00
|
|
|
|
} 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;
|