1
This commit is contained in:
102
_script/update_controller_ids.js
Normal file
102
_script/update_controller_ids.js
Normal file
@@ -0,0 +1,102 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
// 递归查找所有 .js 文件
|
||||
function findJsFiles(dir) {
|
||||
let results = [];
|
||||
const list = fs.readdirSync(dir);
|
||||
|
||||
list.forEach(file => {
|
||||
const filePath = path.join(dir, file);
|
||||
const stat = fs.statSync(filePath);
|
||||
|
||||
if (stat && stat.isDirectory()) {
|
||||
results = results.concat(findJsFiles(filePath));
|
||||
} else if (file.endsWith('.js')) {
|
||||
results.push(filePath);
|
||||
}
|
||||
});
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
// 更新 controller 中的 ID 字段使用
|
||||
function updateControllerIds(filePath) {
|
||||
try {
|
||||
let content = fs.readFileSync(filePath, 'utf8');
|
||||
const originalContent = content;
|
||||
|
||||
// 定义字段映射关系
|
||||
const fieldMappings = {
|
||||
'applyId': 'id',
|
||||
'taskId': 'id',
|
||||
'chatId': 'id',
|
||||
'resumeId': 'id',
|
||||
'deviceId': 'id'
|
||||
};
|
||||
|
||||
// 替换字段名
|
||||
Object.keys(fieldMappings).forEach(oldField => {
|
||||
const newField = fieldMappings[oldField];
|
||||
|
||||
// 替换变量声明和赋值
|
||||
content = content.replace(new RegExp(`const ${oldField} =`, 'g'), `const ${newField} =`);
|
||||
content = content.replace(new RegExp(`let ${oldField} =`, 'g'), `let ${newField} =`);
|
||||
content = content.replace(new RegExp(`var ${oldField} =`, 'g'), `var ${newField} =`);
|
||||
|
||||
// 替换对象属性
|
||||
content = content.replace(new RegExp(`${oldField}:`, 'g'), `${newField}:`);
|
||||
|
||||
// 替换解构赋值
|
||||
content = content.replace(new RegExp(`\\{ ${oldField} \\}`, 'g'), `{ ${newField} }`);
|
||||
content = content.replace(new RegExp(`\\{${oldField}\\}`, 'g'), `{${newField}}`);
|
||||
|
||||
// 替换 where 条件中的字段名
|
||||
content = content.replace(new RegExp(`where: \\{ ${oldField}:`, 'g'), `where: { ${newField}:`);
|
||||
content = content.replace(new RegExp(`where: \\{${oldField}:`, 'g'), `where: {${newField}:`);
|
||||
|
||||
// 替换注释中的字段名
|
||||
content = content.replace(new RegExp(`- ${oldField}`, 'g'), `- ${newField}`);
|
||||
content = content.replace(new RegExp(`${oldField}:`, 'g'), `${newField}:`);
|
||||
});
|
||||
|
||||
// 清理多余的空行
|
||||
content = content.replace(/\n\s*\n\s*\n/g, '\n\n');
|
||||
|
||||
if (content !== originalContent) {
|
||||
fs.writeFileSync(filePath, content, 'utf8');
|
||||
console.log(`已更新: ${filePath}`);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
} catch (error) {
|
||||
console.error(`处理文件失败 ${filePath}:`, error.message);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// 主函数
|
||||
function main() {
|
||||
const controllerDir = path.join(__dirname, 'api', 'controller_front');
|
||||
|
||||
if (!fs.existsSync(controllerDir)) {
|
||||
console.error('Controller 目录不存在:', controllerDir);
|
||||
return;
|
||||
}
|
||||
|
||||
const jsFiles = findJsFiles(controllerDir);
|
||||
let updatedCount = 0;
|
||||
|
||||
console.log(`找到 ${jsFiles.length} 个 controller 文件`);
|
||||
|
||||
jsFiles.forEach(filePath => {
|
||||
if (updateControllerIds(filePath)) {
|
||||
updatedCount++;
|
||||
}
|
||||
});
|
||||
|
||||
console.log(`更新完成,共更新 ${updatedCount} 个文件`);
|
||||
}
|
||||
|
||||
main();
|
||||
Reference in New Issue
Block a user