Files
online_sys/frontend_智能开发/syncProjectDetail.cjs

77 lines
2.4 KiB
JavaScript
Raw Normal View History

const fs = require('fs');
// 读取projectLibraryMock.js文件
const mockFilePath = './src/mocks/projectLibraryMock.js';
let mockFileContent = fs.readFileSync(mockFilePath, 'utf-8');
// 找到getMockProjectsList中的projects数组
const firstProjectsMatch = mockFileContent.match(/const projects = \[([\s\S]*?)\];\s*\/\/ 搜索过滤/);
if (!firstProjectsMatch) {
console.error('❌ 未找到getMockProjectsList中的projects数组');
process.exit(1);
}
// 提取第一个projects数组的内容不包含const projects = 和 ;
const projectsArrayContent = firstProjectsMatch[1];
// 找到getMockProjectDetail函数的开始位置
const detailFunctionStart = mockFileContent.indexOf('export const getMockProjectDetail = (id) => {');
if (detailFunctionStart === -1) {
console.error('❌ 未找到getMockProjectDetail函数');
process.exit(1);
}
// 找到getMockProjectDetail函数的结束位置
const functionEndPattern = /\n\};/g;
functionEndPattern.lastIndex = detailFunctionStart;
let functionEndMatch;
let detailFunctionEnd = -1;
while ((functionEndMatch = functionEndPattern.exec(mockFileContent))) {
// 简单检查如果这是getMockProjectDetail函数的结束
const functionContent = mockFileContent.substring(detailFunctionStart, functionEndMatch.index + functionEndMatch[0].length);
if (functionContent.split('{').length === functionContent.split('}').length) {
detailFunctionEnd = functionEndMatch.index + functionEndMatch[0].length;
break;
}
}
if (detailFunctionEnd === -1) {
console.error('❌ 未找到getMockProjectDetail函数的结束位置');
process.exit(1);
}
// 构建新的getMockProjectDetail函数
const newDetailFunction = `export const getMockProjectDetail = (id) => {
// 直接根据ID返回对应项目的详情
const projects = [${projectsArrayContent}];
const project = projects.find(p => p.id === parseInt(id));
if (!project) {
return {
code: 404,
msg: "项目不存在",
data: null
};
}
return {
code: 0,
msg: "success",
data: project
};
}`;
// 替换getMockProjectDetail函数
mockFileContent =
mockFileContent.substring(0, detailFunctionStart) +
newDetailFunction +
mockFileContent.substring(detailFunctionEnd);
// 写入文件
fs.writeFileSync(mockFilePath, mockFileContent, 'utf-8');
console.log('✅ getMockProjectDetail函数已同步更新');
console.log('现在两个函数使用相同的智能开发项目数据。');