126 lines
4.0 KiB
JavaScript
126 lines
4.0 KiB
JavaScript
const fs = require('fs');
|
||||
|
|
const content = fs.readFileSync('final-backend.js', 'utf8');
|
|||
|
|
const lines = content.split('\n');
|
|||
|
|
|
|||
|
|
// 找到products模块的开始(第一个app.get('/api/products')
|
|||
|
|
let startLine = -1;
|
|||
|
|
for (let i = 0; i < lines.length; i++) {
|
|||
|
|
if (lines[i].includes("app.get('/api/products'")) {
|
|||
|
|
startLine = i + 1; // 转换为1-based行号
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 找到products模块的结束(在products模块之后,下一个模块开始之前)
|
|||
|
|
let endLine = -1;
|
|||
|
|
for (let i = startLine - 1; i < lines.length; i++) {
|
|||
|
|
if (lines[i].includes('app.') && lines[i].includes('/api/')) {
|
|||
|
|
const nextPath = lines[i].match(/['\"](\/api\/[^'\"]+)['\"]/);
|
|||
|
|
if (nextPath && !nextPath[1].startsWith('/api/products')) {
|
|||
|
|
// 找到上一个路由的结束
|
|||
|
|
for (let j = i - 1; j >= 0; j--) {
|
|||
|
|
if (lines[j].trim() === '});') {
|
|||
|
|
endLine = j;
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (startLine === -1) {
|
|||
|
|
console.log('❌ 无法找到products模块的开始');
|
|||
|
|
process.exit(1);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (endLine === -1) {
|
|||
|
|
// 如果没找到下一个模块,使用文件末尾
|
|||
|
|
endLine = lines.length - 1;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
console.log(`准备删除products模块代码(第${startLine}-${endLine + 1}行)`);
|
|||
|
|
|
|||
|
|
// 备份原文件
|
|||
|
|
fs.writeFileSync('final-backend.js.products-backup', content);
|
|||
|
|
console.log('✅ 已创建备份文件:final-backend.js.products-backup');
|
|||
|
|
|
|||
|
|
// 删除products模块代码
|
|||
|
|
const linesWithoutProducts = [
|
|||
|
|
...lines.slice(0, startLine - 1),
|
|||
|
|
...lines.slice(endLine + 1)
|
|||
|
|
];
|
|||
|
|
|
|||
|
|
// 在users路由引用之后插入products路由引用
|
|||
|
|
// 找到users路由引用的位置
|
|||
|
|
let usersRoutesLine = -1;
|
|||
|
|
for (let i = 0; i < linesWithoutProducts.length; i++) {
|
|||
|
|
if (linesWithoutProducts[i].includes('const usersRoutes = require')) {
|
|||
|
|
usersRoutesLine = i;
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (usersRoutesLine === -1) {
|
|||
|
|
console.log('❌ 未找到users路由引用位置');
|
|||
|
|
process.exit(1);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
console.log(`users路由引用在第${usersRoutesLine + 1}行`);
|
|||
|
|
|
|||
|
|
// 在users路由引用之后插入products路由引用
|
|||
|
|
const updatedLines = [
|
|||
|
|
...linesWithoutProducts.slice(0, usersRoutesLine + 3), // 包括usersRoutes行和app.use行
|
|||
|
|
'',
|
|||
|
|
'// ==================== 商品路由 ====================',
|
|||
|
|
'const productsRoutes = require(\'./routes/products\');',
|
|||
|
|
'app.use(\'/api/products\', productsRoutes);',
|
|||
|
|
'',
|
|||
|
|
...linesWithoutProducts.slice(usersRoutesLine + 3)
|
|||
|
|
];
|
|||
|
|
|
|||
|
|
// 写入更新后的文件
|
|||
|
|
fs.writeFileSync('final-backend.js', updatedLines.join('\n'));
|
|||
|
|
console.log('✅ 已更新主文件');
|
|||
|
|
|
|||
|
|
// 验证修改
|
|||
|
|
const updatedContent = fs.readFileSync('final-backend.js', 'utf8');
|
|||
|
|
const updatedLinesArray = updatedContent.split('\n');
|
|||
|
|
|
|||
|
|
// 检查products路由引用是否存在
|
|||
|
|
let productsRoutesFound = false;
|
|||
|
|
let productsUseFound = false;
|
|||
|
|
for (let i = 0; i < updatedLinesArray.length; i++) {
|
|||
|
|
if (updatedLinesArray[i].includes('const productsRoutes = require(\'./routes/products\')')) {
|
|||
|
|
productsRoutesFound = true;
|
|||
|
|
console.log(`✅ 找到products路由引用(第${i + 1}行)`);
|
|||
|
|
}
|
|||
|
|
if (updatedLinesArray[i].includes('app.use(\'/api/products\', productsRoutes)')) {
|
|||
|
|
productsUseFound = true;
|
|||
|
|
console.log(`✅ 找到products路由使用(第${i + 1}行)`);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 检查原products代码是否被删除
|
|||
|
|
let productsCodeFound = false;
|
|||
|
|
for (let i = 0; i < updatedLinesArray.length; i++) {
|
|||
|
|
if (updatedLinesArray[i].includes('app.get(\'/api/products\'')) {
|
|||
|
|
productsCodeFound = true;
|
|||
|
|
console.log(`❌ 发现未删除的products代码(第${i + 1}行)`);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (productsRoutesFound && productsUseFound && !productsCodeFound) {
|
|||
|
|
console.log('✅ 文件修改验证成功');
|
|||
|
|
|
|||
|
|
// 显示相关部分
|
|||
|
|
console.log('\n更新后的路由引用部分:');
|
|||
|
|
for (let i = 0; i < Math.min(30, updatedLinesArray.length); i++) {
|
|||
|
|
if (updatedLinesArray[i].includes('const') || updatedLinesArray[i].includes('app.use')) {
|
|||
|
|
console.log(`${i + 1}: ${updatedLinesArray[i]}`);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
} else {
|
|||
|
|
console.log('❌ 文件修改验证失败');
|
|||
|
|
process.exit(1);
|
|||
|
|
}
|