|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import fs from 'fs'; |
| 4 | +import path from 'path'; |
| 5 | +import { fileURLToPath } from 'url'; |
| 6 | + |
| 7 | +const __filename = fileURLToPath(import.meta.url); |
| 8 | +const __dirname = path.dirname(__filename); |
| 9 | + |
| 10 | +// 需要临时修改的 tsconfig.json 文件路径(相对于 webapp 根目录) |
| 11 | +const WEBAPP_ROOT = path.resolve(__dirname, '../..'); |
| 12 | +const TSCONFIG_PATHS = ['packages/plugin-data-viewer/tsconfig.json']; |
| 13 | + |
| 14 | +// 需要临时移除的循环引用路径 |
| 15 | +const CIRCULAR_REFERENCES = ['../plugin-sql-editor', '../plugin-sql-editor-navigation-tab']; |
| 16 | + |
| 17 | +// 备份文件后缀 |
| 18 | +const BACKUP_SUFFIX = '.backup-before-build'; |
| 19 | + |
| 20 | +/** |
| 21 | + * 备份并修改 tsconfig.json 文件,移除循环引用 |
| 22 | + */ |
| 23 | +function removeCircularReferences() { |
| 24 | + console.log('🔧 正在移除循环引用...'); |
| 25 | + |
| 26 | + TSCONFIG_PATHS.forEach(tsconfigPath => { |
| 27 | + const fullPath = path.join(WEBAPP_ROOT, tsconfigPath); |
| 28 | + const backupPath = fullPath + BACKUP_SUFFIX; |
| 29 | + |
| 30 | + try { |
| 31 | + // 检查是否已存在备份文件 |
| 32 | + if (fs.existsSync(backupPath)) { |
| 33 | + console.log(`⚠️ 备份文件已存在: ${tsconfigPath}${BACKUP_SUFFIX}`); |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + // 读取原始文件 |
| 38 | + const originalContent = fs.readFileSync(fullPath, 'utf8'); |
| 39 | + |
| 40 | + // 创建备份 |
| 41 | + fs.writeFileSync(backupPath, originalContent); |
| 42 | + console.log(`📝 已备份: ${tsconfigPath}`); |
| 43 | + |
| 44 | + // 解析 JSON |
| 45 | + const tsconfig = JSON.parse(originalContent); |
| 46 | + |
| 47 | + // 移除循环引用 |
| 48 | + if (tsconfig.references) { |
| 49 | + const originalCount = tsconfig.references.length; |
| 50 | + tsconfig.references = tsconfig.references.filter(ref => !CIRCULAR_REFERENCES.includes(ref.path)); |
| 51 | + const removedCount = originalCount - tsconfig.references.length; |
| 52 | + |
| 53 | + if (removedCount > 0) { |
| 54 | + console.log(`🗑️ 已从 ${tsconfigPath} 移除 ${removedCount} 个循环引用`); |
| 55 | + |
| 56 | + // 写入修改后的内容 |
| 57 | + fs.writeFileSync(fullPath, JSON.stringify(tsconfig, null, 2) + '\n'); |
| 58 | + } else { |
| 59 | + console.log(`ℹ️ ${tsconfigPath} 中未找到需要移除的循环引用`); |
| 60 | + } |
| 61 | + } |
| 62 | + } catch (error) { |
| 63 | + console.error(`❌ 处理 ${tsconfigPath} 时出错:`, error.message); |
| 64 | + throw error; |
| 65 | + } |
| 66 | + }); |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * 恢复备份的 tsconfig.json 文件 |
| 71 | + */ |
| 72 | +function restoreBackups() { |
| 73 | + console.log('🔄 正在恢复备份文件...'); |
| 74 | + |
| 75 | + TSCONFIG_PATHS.forEach(tsconfigPath => { |
| 76 | + const fullPath = path.join(WEBAPP_ROOT, tsconfigPath); |
| 77 | + const backupPath = fullPath + BACKUP_SUFFIX; |
| 78 | + |
| 79 | + try { |
| 80 | + if (fs.existsSync(backupPath)) { |
| 81 | + // 恢复备份 |
| 82 | + fs.copyFileSync(backupPath, fullPath); |
| 83 | + |
| 84 | + // 删除备份文件 |
| 85 | + fs.unlinkSync(backupPath); |
| 86 | + |
| 87 | + console.log(`✅ 已恢复: ${tsconfigPath}`); |
| 88 | + } else { |
| 89 | + console.log(`ℹ️ 未找到备份文件: ${tsconfigPath}${BACKUP_SUFFIX}`); |
| 90 | + } |
| 91 | + } catch (error) { |
| 92 | + console.error(`❌ 恢复 ${tsconfigPath} 时出错:`, error.message); |
| 93 | + } |
| 94 | + }); |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * 清理备份文件(用于异常情况) |
| 99 | + */ |
| 100 | +function cleanupBackups() { |
| 101 | + console.log('🧹 清理备份文件...'); |
| 102 | + |
| 103 | + TSCONFIG_PATHS.forEach(tsconfigPath => { |
| 104 | + const fullPath = path.join(WEBAPP_ROOT, tsconfigPath); |
| 105 | + const backupPath = fullPath + BACKUP_SUFFIX; |
| 106 | + |
| 107 | + if (fs.existsSync(backupPath)) { |
| 108 | + fs.unlinkSync(backupPath); |
| 109 | + console.log(`🧹 已清理备份文件: ${tsconfigPath}${BACKUP_SUFFIX}`); |
| 110 | + } |
| 111 | + }); |
| 112 | +} |
| 113 | + |
| 114 | +// 处理进程中断信号 |
| 115 | +process.on('SIGINT', () => { |
| 116 | + console.log('\n\n⚠️ 收到中断信号,正在清理...'); |
| 117 | + restoreBackups(); |
| 118 | + process.exit(0); |
| 119 | +}); |
| 120 | + |
| 121 | +process.on('SIGTERM', () => { |
| 122 | + console.log('\n\n⚠️ 收到终止信号,正在清理...'); |
| 123 | + restoreBackups(); |
| 124 | + process.exit(0); |
| 125 | +}); |
| 126 | + |
| 127 | +// 命令行参数处理 |
| 128 | +const command = process.argv[2]; |
| 129 | + |
| 130 | +switch (command) { |
| 131 | + case 'remove': |
| 132 | + console.log('🚀 开始移除循环引用...\n'); |
| 133 | + try { |
| 134 | + removeCircularReferences(); |
| 135 | + console.log('\n✅ 循环引用移除完成!'); |
| 136 | + } catch (error) { |
| 137 | + console.error('\n❌ 移除循环引用失败:', error.message); |
| 138 | + process.exit(1); |
| 139 | + } |
| 140 | + break; |
| 141 | + |
| 142 | + case 'restore': |
| 143 | + console.log('🚀 开始恢复备份文件...\n'); |
| 144 | + try { |
| 145 | + restoreBackups(); |
| 146 | + console.log('\n✅ 备份恢复完成!'); |
| 147 | + } catch (error) { |
| 148 | + console.error('\n❌ 恢复备份失败:', error.message); |
| 149 | + process.exit(1); |
| 150 | + } |
| 151 | + break; |
| 152 | + |
| 153 | + case 'cleanup': |
| 154 | + cleanupBackups(); |
| 155 | + console.log('✅ 清理完成!'); |
| 156 | + break; |
| 157 | + |
| 158 | + default: |
| 159 | + console.log(` |
| 160 | +用法: node cycle-fix.js <command> |
| 161 | +
|
| 162 | +命令: |
| 163 | + remove - 移除循环引用(创建备份) |
| 164 | + restore - 恢复备份文件 |
| 165 | + cleanup - 清理备份文件 |
| 166 | +
|
| 167 | +示例: |
| 168 | + node cycle-fix.js remove # 移除循环引用 |
| 169 | + yarn build # 执行构建 |
| 170 | + node cycle-fix.js restore # 恢复原始文件 |
| 171 | +`); |
| 172 | + process.exit(1); |
| 173 | +} |
0 commit comments