Files
yunhaifinance/backend/extract-users-precise.js
T

103 lines
3.4 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');
// 找到users模块的开始(第一个app.get('/api/users'
let startLine = -1;
for (let i = 0; i < lines.length; i++) {
if (lines[i].includes("app.get('/api/users'")) {
startLine = i + 1; // 转换为1-based行号
break;
}
}
// 找到users模块的结束(在删除用户路由之后,下一个模块开始之前)
let endLine = -1;
for (let i = startLine - 1; i < lines.length; i++) {
// 找到删除用户路由的结束
if (lines[i].includes("app.delete('/api/users/:id'")) {
// 找到这个路由的结束(找到下一个}后跟);的行)
for (let j = i; j < lines.length; j++) {
if (lines[j].trim() === '});') {
// 检查下一行是否开始新模块
for (let k = j + 1; k < Math.min(j + 10, lines.length); k++) {
if (lines[k].includes('app.') && lines[k].includes('/api/')) {
const nextPath = lines[k].match(/['\"](\/api\/[^'\"]+)['\"]/);
if (nextPath && !nextPath[1].startsWith('/api/users')) {
endLine = j; // 结束在});这一行
break;
}
}
}
if (endLine === -1) {
endLine = j; // 如果没有找到新模块,就使用这个
}
break;
}
}
break;
}
}
if (startLine === -1 || endLine === -1) {
console.log('❌ 无法找到users模块的边界');
process.exit(1);
}
console.log(`users模块范围:第${startLine}行到第${endLine + 1}行`);
// 提取users模块代码
const usersCode = lines.slice(startLine - 1, endLine + 1).join('\n');
console.log('\n提取的users模块代码:');
console.log('=' .repeat(50));
console.log(usersCode);
console.log('=' .repeat(50));
// 将app.替换为router.
const routerCode = usersCode.replace(/app\.(get|post|put|delete|patch)/g, 'router.$1');
// 修复路径:移除/api前缀,因为主文件会使用app.use('/api/users', usersRoutes)
const fixedRouterCode = routerCode
.replace(/router\.get\('\/api\/users'/g, "router.get('/'")
.replace(/router\.put\('\/api\/users\/(:id)'/g, "router.put('/$1'")
.replace(/router\.put\('\/api\/users\/(:id)\/password'/g, "router.put('/$1/password'")
.replace(/router\.post\('\/api\/users'/g, "router.post('/'")
.replace(/router\.delete\('\/api\/users\/(:id)'/g, "router.delete('/$1'");
console.log('\n转换后的router代码:');
console.log('=' .repeat(50));
console.log(fixedRouterCode);
console.log('=' .repeat(50));
// 创建完整的users路由文件
const fullUsersCode = `const express = require('express');
const router = express.Router();
// 导入依赖
const db = require('../db-sqlite');
const { hashPassword, verifyPassword } = require('../utils/auth');
const { authenticate } = require('../middleware/auth');
${fixedRouterCode}
module.exports = router;`;
console.log('\n完整的users路由文件内容:');
console.log('=' .repeat(50));
console.log(fullUsersCode);
console.log('=' .repeat(50));
// 写入文件
fs.writeFileSync('routes/users.js', fullUsersCode);
console.log('\n✅ users路由文件已创建:routes/users.js');
// 验证文件
const fileContent = fs.readFileSync('routes/users.js', 'utf8');
console.log(`文件大小:${fileContent.length} 字符`);
if (fileContent.length > 100) {
console.log('✅ 文件创建成功,内容长度 > 100 字符');
} else {
console.log('❌ 文件创建失败,内容长度不足');
process.exit(1);
}