77 lines
2.4 KiB
JavaScript
77 lines
2.4 KiB
JavaScript
import fs from 'fs';
|
||
|
||
// 读取生成的课程数据
|
||
const courseLiveListData = JSON.parse(fs.readFileSync('courseLiveListData.json', 'utf-8'));
|
||
|
||
// 读取mockData.js文件
|
||
let mockDataContent = fs.readFileSync('src/data/mockData.js', 'utf-8');
|
||
|
||
// 找到courseLiveList的开始和结束位置
|
||
const startPattern = /mockData\.courseLiveList = \[/;
|
||
const startMatch = mockDataContent.match(startPattern);
|
||
|
||
if (startMatch) {
|
||
const startIndex = mockDataContent.indexOf(startMatch[0]);
|
||
|
||
// 找到对应的结束位置(找到配对的 ]; )
|
||
let braceCount = 0;
|
||
let endIndex = startIndex + startMatch[0].length;
|
||
let inString = false;
|
||
let stringChar = '';
|
||
|
||
for (let i = endIndex; i < mockDataContent.length; i++) {
|
||
const char = mockDataContent[i];
|
||
const prevChar = i > 0 ? mockDataContent[i - 1] : '';
|
||
|
||
// 处理字符串
|
||
if (!inString && (char === '"' || char === "'") && prevChar !== '\\') {
|
||
inString = true;
|
||
stringChar = char;
|
||
} else if (inString && char === stringChar && prevChar !== '\\') {
|
||
inString = false;
|
||
}
|
||
|
||
if (!inString) {
|
||
if (char === '[') braceCount++;
|
||
if (char === ']') {
|
||
if (braceCount === 0) {
|
||
// 找到了结束位置
|
||
endIndex = i + 1;
|
||
// 检查是否有分号
|
||
if (mockDataContent[i + 1] === ';') {
|
||
endIndex = i + 2;
|
||
}
|
||
break;
|
||
}
|
||
braceCount--;
|
||
}
|
||
}
|
||
}
|
||
|
||
// 替换courseLiveList部分
|
||
const newCourseLiveList = `mockData.courseLiveList = ${JSON.stringify(courseLiveListData.courseLiveList, null, 2)};`;
|
||
|
||
mockDataContent =
|
||
mockDataContent.substring(0, startIndex) +
|
||
newCourseLiveList +
|
||
mockDataContent.substring(endIndex);
|
||
|
||
// 写回文件
|
||
fs.writeFileSync('src/data/mockData.js', mockDataContent);
|
||
console.log('✅ mockData.js中的courseLiveList已更新');
|
||
|
||
// 验证9月3日的课程状态
|
||
const sept3Course = courseLiveListData.courseLiveList
|
||
.find(unit => unit.unitId === 'unit7')
|
||
?.courses.find(c => c.date === '2025-09-03');
|
||
|
||
if (sept3Course) {
|
||
console.log('\n9月3日课程状态:');
|
||
console.log(` 课程:${sept3Course.courseName}`);
|
||
console.log(` completed: ${sept3Course.completed}`);
|
||
console.log(` current: ${sept3Course.current}`);
|
||
console.log(` upcoming: ${sept3Course.upcoming}`);
|
||
}
|
||
} else {
|
||
console.error('❌ 无法找到mockData.courseLiveList的位置');
|
||
} |