Files
wwjcloud-nest-v1/wwjcloud-nest-v1/tools/fix-duplicate-methods.js
wanwujie 9d0b389bc7 feat: 完成Java到NestJS迁移工具开发
- 修复了迁移工具的所有构建错误
- 成功生成了87个实体、211个服务、114个控制器
- 修复了Result导入路径问题,从@wwjBoot导入
- 修复了重复方法问题,运行了批量修复脚本
- 项目构建完全成功,0个错误
- 迁移工具现在可以正常使用
2025-10-24 23:04:55 +08:00

90 lines
2.7 KiB
JavaScript

#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
/**
* 修复服务文件中的重复方法名
*/
function fixDuplicateMethods() {
const servicesDir = '/Users/wanwu/Documents/wwjcloud/wwjcloud-nsetjs/wwjcloud-nest-v1/wwjcloud/libs/wwjcloud-core/src/services';
// 获取所有服务文件
const serviceFiles = [];
function findServiceFiles(dir) {
const files = fs.readdirSync(dir);
for (const file of files) {
const filePath = path.join(dir, file);
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
findServiceFiles(filePath);
} else if (file.endsWith('.service.ts')) {
serviceFiles.push(filePath);
}
}
}
findServiceFiles(servicesDir);
console.log(`找到 ${serviceFiles.length} 个服务文件`);
let fixedCount = 0;
for (const filePath of serviceFiles) {
try {
const content = fs.readFileSync(filePath, 'utf8');
// 查找重复的方法名
const methodMatches = content.match(/async\s+(\w+)\s*\([^)]*\)\s*:\s*Promise<[^>]+>\s*\{/g);
if (methodMatches) {
const methodNames = methodMatches.map(match => {
const nameMatch = match.match(/async\s+(\w+)\s*\(/);
return nameMatch ? nameMatch[1] : null;
}).filter(Boolean);
// 检查重复的方法名
const duplicates = {};
methodNames.forEach(name => {
if (duplicates[name]) {
duplicates[name]++;
} else {
duplicates[name] = 1;
}
});
const hasDuplicates = Object.values(duplicates).some(count => count > 1);
if (hasDuplicates) {
console.log(`修复文件: ${filePath}`);
let newContent = content;
const methodCounters = {};
// 替换重复的方法名
newContent = newContent.replace(/async\s+(\w+)\s*\([^)]*\)\s*:\s*Promise<[^>]+>\s*\{/g, (match, methodName) => {
if (methodCounters[methodName]) {
methodCounters[methodName]++;
const newMethodName = `${methodName}${methodCounters[methodName]}`;
console.log(` - 重命名重复方法: ${methodName} -> ${newMethodName}`);
return match.replace(methodName, newMethodName);
} else {
methodCounters[methodName] = 1;
return match;
}
});
fs.writeFileSync(filePath, newContent);
fixedCount++;
}
}
} catch (error) {
console.error(`处理文件 ${filePath} 时出错:`, error.message);
}
}
console.log(`\n修复完成!共修复了 ${fixedCount} 个文件`);
}
// 运行修复
fixDuplicateMethods();