feat: 完成PHP到NestJS迁移工具和代码生成

-  成功运行迁移工具,生成28个模块的完整NestJS代码
-  生成所有实体、服务、控制器、验证器等组件
-  修复npm依赖冲突,更新package-lock.json
-  添加Docker测试脚本和配置文件
-  完善迁移工具的调试日志和错误处理
- 🔧 包含增量更新工具和质量检查工具
- 📊 迁移统计:28个模块,数千个文件,耗时26.47秒

主要变更:
- wwjcloud-nest/src/core/* - 生成的业务模块代码
- tools/* - 迁移工具和辅助脚本
- wwjcloud-nest/package.json - 依赖更新
- docker/* - 容器化配置和测试脚本
This commit is contained in:
wanwujie
2025-10-20 18:43:52 +08:00
parent 5fafaa9135
commit c4e588a2fe
565 changed files with 36188 additions and 4897 deletions

View File

@@ -14,6 +14,7 @@ const ListenerGenerator = require('./generators/listener-generator');
// const CommandGenerator = require('./generators/command-generator'); // 文件不存在,暂时注释
const DictGenerator = require('./generators/dict-generator');
const QualityGate = require('./generators/quality-gate');
const IncrementalUpdater = require('./incremental-updater');
/**
* 🎯 迁移协调器
@@ -28,7 +29,8 @@ class MigrationCoordinator {
enableJobs: true,
enableListeners: true,
enableCommands: false,
dryRun: false
dryRun: false,
incrementalMode: process.env.INCREMENTAL === 'true' || process.argv.includes('--incremental')
};
this.stats = {
@@ -46,6 +48,38 @@ class MigrationCoordinator {
*/
async run() {
console.log('🚀 启动完整自动化迁移工具...');
if (this.config.incrementalMode) {
console.log('🔄 增量模式:仅处理变更的文件');
return await this.runIncrementalUpdate();
} else {
console.log('🏗️ 完整模式:重新生成所有文件');
return await this.runFullMigration();
}
}
/**
* 🔄 运行增量更新
*/
async runIncrementalUpdate() {
console.log('🔄 启动增量更新模式...\n');
try {
const incrementalUpdater = new IncrementalUpdater();
await incrementalUpdater.run();
console.log('✅ 增量更新完成');
return true;
} catch (error) {
console.error('❌ 增量更新失败:', error.message);
return false;
}
}
/**
* 🏗️ 运行完整迁移
*/
async runFullMigration() {
console.log('目标完整迁移PHP项目到NestJS包括所有组件\n');
this.stats.startTime = new Date();
@@ -54,40 +88,47 @@ class MigrationCoordinator {
// 第1阶段加载PHP文件发现结果
console.log('📊 第1阶段加载PHP文件发现结果...');
await this.loadDiscoveryData();
console.log('✅ 第1阶段完成 - 数据加载成功');
// 第2阶段创建完整模块结构
console.log('📊 第2阶段创建完整模块结构...');
await this.createCompleteModuleStructure();
console.log('✅ 第2阶段完成 - 模块结构创建成功');
// 第3阶段生成实体数据模型层
console.log('📊 第3阶段生成实体...');
await this.generateEntities();
console.log('🔍 验证实体生成结果...');
await this.validateEntities();
console.log('✅ 第3阶段完成 - 实体生成和验证成功');
// 第4阶段生成服务业务逻辑层
console.log('📊 第4阶段生成服务...');
await this.generateServices();
console.log('🔍 验证服务生成结果...');
await this.validateServices();
console.log('✅ 第4阶段完成 - 服务生成和验证成功');
// 第5阶段生成验证器依赖服务
console.log('📊 第5阶段生成验证器...');
await this.generateValidators();
console.log('🔍 验证验证器生成结果...');
await this.validateValidators();
console.log('✅ 第5阶段完成 - 验证器生成和验证成功');
// 第6阶段生成控制器依赖服务和验证器
console.log('📊 第6阶段生成控制器...');
await this.generateControllersWithClassification();
console.log('🔍 验证控制器生成结果...');
await this.validateControllers();
console.log('✅ 第6阶段完成 - 控制器生成和验证成功');
// 第7阶段生成路由依赖控制器
console.log('📊 第7阶段生成路由...');
await this.generateRoutes();
console.log('🔍 验证路由生成结果...');
await this.validateRoutes();
console.log('✅ 第7阶段完成 - 路由生成和验证成功');
// 第8阶段生成任务
if (this.config.enableJobs) {
@@ -95,6 +136,7 @@ class MigrationCoordinator {
await this.generateJobs();
console.log('🔍 验证任务生成结果...');
await this.validateJobs();
console.log('✅ 第8阶段完成 - 任务生成和验证成功');
} else {
console.log('⏭️ 跳过任务生成 (已禁用)');
}
@@ -105,6 +147,7 @@ class MigrationCoordinator {
await this.generateListeners();
console.log('🔍 验证监听器生成结果...');
await this.validateListeners();
console.log('✅ 第9阶段完成 - 监听器生成和验证成功');
} else {
console.log('⏭️ 跳过监听器生成 (已禁用)');
}
@@ -115,6 +158,7 @@ class MigrationCoordinator {
await this.generateCommands();
console.log('🔍 验证命令生成结果...');
await this.validateCommands();
console.log('✅ 第10阶段完成 - 命令生成和验证成功');
} else {
console.log('⏭️ 跳过命令生成 (已禁用)');
}
@@ -124,20 +168,24 @@ class MigrationCoordinator {
await this.generateDicts();
console.log('🔍 验证字典生成结果...');
await this.validateDicts();
console.log('✅ 第11阶段完成 - 字典生成和验证成功');
// 第12阶段生成模块文件依赖所有组件
console.log('📊 第12阶段生成模块文件...');
await this.generateModuleFiles();
console.log('🔍 验证模块文件生成结果...');
await this.validateModuleFiles();
console.log('✅ 第12阶段完成 - 模块文件生成和验证成功');
// 第13阶段最终质量检查
console.log('📊 第13阶段最终质量检查...');
await this.runQualityGate();
console.log('✅ 第13阶段完成 - 质量检查通过');
// 第14阶段生成统计报告
console.log('📊 第14阶段生成统计报告...');
this.generateStatsReport();
console.log('✅ 第14阶段完成 - 统计报告生成成功');
} catch (error) {
console.error('❌ 迁移过程中发生错误:', error.message);
@@ -147,6 +195,7 @@ class MigrationCoordinator {
this.stats.endTime = new Date();
const duration = this.stats.endTime - this.stats.startTime;
console.log(`\n⏱️ 总耗时: ${(duration / 1000).toFixed(2)}`);
console.log('🎉 完整迁移流程完成!');
}
}
@@ -155,8 +204,29 @@ class MigrationCoordinator {
*/
async loadDiscoveryData() {
try {
console.log(' 🔍 开始读取发现结果文件:', this.config.discoveryResultPath);
// 检查文件是否存在
if (!fs.existsSync(this.config.discoveryResultPath)) {
throw new Error(`发现结果文件不存在: ${this.config.discoveryResultPath}`);
}
// 获取文件大小
const stats = fs.statSync(this.config.discoveryResultPath);
console.log(` 📏 文件大小: ${(stats.size / 1024 / 1024).toFixed(2)} MB`);
console.log(' 📖 正在读取文件内容...');
const data = fs.readFileSync(this.config.discoveryResultPath, 'utf-8');
console.log(' 🔄 正在解析JSON数据...');
this.discoveryData = JSON.parse(data);
// 输出数据统计
const controllers = Object.keys(this.discoveryData.controllers || {}).length;
const services = Object.keys(this.discoveryData.services || {}).length;
const models = Object.keys(this.discoveryData.models || {}).length;
console.log(` 📊 数据统计: 控制器${controllers}个, 服务${services}个, 模型${models}`);
console.log(' ✅ 成功加载PHP文件发现结果');
} catch (error) {
console.error(' ❌ 加载发现数据失败:', error.message);
@@ -168,35 +238,56 @@ class MigrationCoordinator {
* 创建完整模块结构
*/
async createCompleteModuleStructure() {
console.log(' 🔨 创建完整模块结构...');
console.log(' 🏗️ 开始创建模块结构...');
// 获取所有模块
const modules = new Set();
// 从控制器中提取模块
const controllerModules = Object.keys(this.discoveryData.controllers || {});
console.log(` 📁 从控制器提取到 ${controllerModules.length} 个模块:`, controllerModules.slice(0, 5).join(', ') + (controllerModules.length > 5 ? '...' : ''));
for (const [moduleName, controllers] of Object.entries(this.discoveryData.controllers)) {
console.log(` 🔨 创建控制器模块: ${moduleName}`);
modules.add(moduleName);
}
// 从服务中提取模块
const serviceModules = [];
for (const [layerName, services] of Object.entries(this.discoveryData.services)) {
for (const [serviceName, serviceInfo] of Object.entries(services)) {
const moduleName = this.extractModuleNameFromServicePath(serviceInfo.filePath);
if (!modules.has(moduleName)) {
serviceModules.push(moduleName);
console.log(` 🔨 创建服务模块: ${moduleName}`);
}
modules.add(moduleName);
}
}
console.log(` 📁 从服务提取到 ${serviceModules.length} 个新模块:`, serviceModules.slice(0, 5).join(', ') + (serviceModules.length > 5 ? '...' : ''));
// 从模型中提取模块
const modelModules = [];
for (const [moduleName, models] of Object.entries(this.discoveryData.models)) {
if (!modules.has(moduleName)) {
modelModules.push(moduleName);
console.log(` 🔨 创建模型模块: ${moduleName}`);
}
modules.add(moduleName);
}
console.log(` 📁 从模型提取到 ${modelModules.length} 个新模块:`, modelModules.slice(0, 5).join(', ') + (modelModules.length > 5 ? '...' : ''));
// 创建每个模块的目录结构
console.log(` 📂 开始创建 ${modules.size} 个模块的目录结构...`);
let processedCount = 0;
for (const moduleName of modules) {
processedCount++;
console.log(` 📁 [${processedCount}/${modules.size}] 创建模块目录: ${moduleName}`);
await this.createModuleStructure(moduleName);
}
console.log(`创建了 ${modules.size} 个模块的目录结构`);
console.log('模块结构创建完成');
}
/**