#!/usr/bin/env node const { execSync } = require('child_process'); const fs = require('fs'); const path = require('path'); /** * 统一迁移执行脚本 * 按步骤执行完整的PHP到NestJS迁移 */ class MigrationRunner { constructor() { this.toolsDir = path.join(__dirname); this.steps = [ { name: '步骤1: PHP文件发现', tool: 'php-file-discovery.js', description: '扫描PHP项目结构,发现所有相关文件' }, { name: '步骤2: 生成NestJS结构', tool: 'real-business-logic-generator.js', description: '基于PHP结构生成NestJS代码框架' }, { name: '步骤3: 生成模块文件', tool: 'module-generator.js', description: '为每个模块生成.module.ts文件并正确引用所有组件' } ]; this.stats = { totalSteps: this.steps.length, completedSteps: 0, failedSteps: 0, startTime: null, endTime: null }; } /** * 运行完整迁移流程 */ async run() { console.log('🚀 启动PHP到NestJS完整迁移流程'); console.log('=====================================\n'); this.stats.startTime = new Date(); try { // 检查工具文件是否存在 await this.checkTools(); // 执行每个步骤 for (let i = 0; i < this.steps.length; i++) { const step = this.steps[i]; console.log(`\n📋 ${step.name}`); console.log(`📝 ${step.description}`); console.log('─'.repeat(50)); try { await this.executeStep(step, i + 1); this.stats.completedSteps++; console.log(`✅ ${step.name} 完成\n`); } catch (error) { this.stats.failedSteps++; console.error(`❌ ${step.name} 失败:`, error.message); // 询问是否继续 if (!await this.askContinue()) { console.log('🛑 迁移流程已停止'); break; } } } this.stats.endTime = new Date(); this.generateFinalReport(); } catch (error) { console.error('❌ 迁移流程发生严重错误:', error.message); process.exit(1); } } /** * 检查工具文件是否存在 */ async checkTools() { console.log('🔍 检查工具文件...'); for (const step of this.steps) { const toolPath = path.join(this.toolsDir, step.tool); if (!fs.existsSync(toolPath)) { throw new Error(`工具文件不存在: ${step.tool}`); } } console.log('✅ 所有工具文件检查通过\n'); } /** * 执行单个步骤 */ async executeStep(step, stepNumber) { const toolPath = path.join(this.toolsDir, step.tool); console.log(`🔄 执行工具: ${step.tool}`); try { // 执行工具 const output = execSync(`node "${toolPath}"`, { encoding: 'utf8', cwd: process.cwd(), stdio: 'inherit' }); return output; } catch (error) { throw new Error(`工具执行失败: ${error.message}`); } } /** * 询问是否继续执行 */ async askContinue() { // 在非交互模式下自动继续 if (process.env.NODE_ENV === 'production' || process.env.CI) { return true; } // 这里可以添加交互式询问逻辑 // 目前默认继续执行 return true; } /** * 生成最终报告 */ generateFinalReport() { const duration = this.stats.endTime - this.stats.startTime; const durationMinutes = Math.round(duration / 1000 / 60 * 100) / 100; console.log('\n' + '='.repeat(60)); console.log('📊 迁移完成报告'); console.log('='.repeat(60)); console.log(`⏱️ 总耗时: ${durationMinutes} 分钟`); console.log(`📋 总步骤: ${this.stats.totalSteps}`); console.log(`✅ 完成步骤: ${this.stats.completedSteps}`); console.log(`❌ 失败步骤: ${this.stats.failedSteps}`); console.log(`📈 完成率: ${Math.round(this.stats.completedSteps / this.stats.totalSteps * 100)}%`); if (this.stats.failedSteps === 0) { console.log('\n🎉 恭喜!所有步骤都成功完成!'); console.log('📁 请检查 wwjcloud/src/common/ 目录查看生成的代码'); } else { console.log('\n⚠️ 部分步骤失败,请检查错误信息并重试'); } console.log('='.repeat(60)); } } // 运行迁移 if (require.main === module) { const runner = new MigrationRunner(); runner.run().catch(console.error); } module.exports = MigrationRunner;