81 lines
2.1 KiB
JavaScript
81 lines
2.1 KiB
JavaScript
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;
|
|
}
|
|
|
|
// 移除自定义主键字段
|
|
function removeCustomIdsInFile(filePath) {
|
|
try {
|
|
let content = fs.readFileSync(filePath, 'utf8');
|
|
const originalContent = content;
|
|
|
|
// 匹配自定义主键字段模式
|
|
const customIdPatterns = [
|
|
// 模式1: applyId, taskId, chatId, resumeId, deviceId 等
|
|
/(\w+Id):\s*\{\s*comment:\s*'[^']*',\s*type:\s*Sequelize\.STRING\(\d+\),\s*allowNull:\s*false,\s*primaryKey:\s*true\s*\},?\s*/g,
|
|
// 模式2: 带 autoIncrement 的 id 字段
|
|
/(\w+Id):\s*\{\s*comment:\s*'[^']*',\s*type:\s*Sequelize\.INTEGER,\s*primaryKey:\s*true,\s*autoIncrement:\s*true\s*\},?\s*/g
|
|
];
|
|
|
|
customIdPatterns.forEach(pattern => {
|
|
content = content.replace(pattern, '');
|
|
});
|
|
|
|
// 清理多余的空行
|
|
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 modelDir = path.join(__dirname, 'api', 'model');
|
|
|
|
if (!fs.existsSync(modelDir)) {
|
|
console.error('模型目录不存在:', modelDir);
|
|
return;
|
|
}
|
|
|
|
const jsFiles = findJsFiles(modelDir);
|
|
let fixedCount = 0;
|
|
|
|
console.log(`找到 ${jsFiles.length} 个模型文件`);
|
|
|
|
jsFiles.forEach(filePath => {
|
|
if (removeCustomIdsInFile(filePath)) {
|
|
fixedCount++;
|
|
}
|
|
});
|
|
|
|
console.log(`修复完成,共修复 ${fixedCount} 个文件`);
|
|
}
|
|
|
|
main();
|