Files
yunhaifinance/backend/extract-products.js
T

98 lines
3.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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}行`);
// 提取products模块代码
const productsCode = lines.slice(startLine - 1, endLine + 1).join('\n');
console.log('\n提取的products模块代码(前200字符):');
console.log('=' .repeat(50));
console.log(productsCode.substring(0, 200) + '...');
console.log('=' .repeat(50));
// 将app.替换为router.
const routerCode = productsCode.replace(/app\.(get|post|put|delete|patch)/g, 'router.$1');
// 修复路径:移除/api前缀,因为主文件会使用app.use('/api/products', productsRoutes)
const fixedRouterCode = routerCode
.replace(/router\.get\('\/api\/products'/g, "router.get('/'")
.replace(/router\.get\('\/api\/products\/template'/g, "router.get('/template'")
.replace(/router\.get\('\/api\/products\/:id'/g, "router.get('/:id'")
.replace(/router\.post\('\/api\/products'/g, "router.post('/'")
.replace(/router\.put\('\/api\/products\/:id'/g, "router.put('/:id'")
.replace(/router\.delete\('\/api\/products\/:id'/g, "router.delete('/:id'")
.replace(/router\.post\('\/api\/products\/batch-import'/g, "router.post('/batch-import'");
console.log('\n转换后的router代码(前200字符):');
console.log('=' .repeat(50));
console.log(fixedRouterCode.substring(0, 200) + '...');
console.log('=' .repeat(50));
// 创建完整的products路由文件
const fullProductsCode = `const express = require('express');
const router = express.Router();
// 导入依赖
const db = require('../db-sqlite');
${fixedRouterCode}
module.exports = router;`;
console.log('\n完整的products路由文件内容(前300字符):');
console.log('=' .repeat(50));
console.log(fullProductsCode.substring(0, 300) + '...');
console.log('=' .repeat(50));
// 写入文件
fs.writeFileSync('routes/products.js', fullProductsCode);
console.log('\n✅ products路由文件已创建:routes/products.js');
// 验证文件
const fileContent = fs.readFileSync('routes/products.js', 'utf8');
console.log(`文件大小:${fileContent.length} 字符`);
if (fileContent.length > 100) {
console.log('✅ 文件创建成功,内容长度 > 100 字符');
} else {
console.log('❌ 文件创建失败,内容长度不足');
process.exit(1);
}