47 lines
1.4 KiB
JavaScript
47 lines
1.4 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const questionsDir = path.resolve(__dirname, '../src/data/questions');
|
|
const outputFile = path.resolve(__dirname, '../src/data/demoQuestions.ts');
|
|
|
|
const jsonlFiles = fs
|
|
.readdirSync(questionsDir)
|
|
.filter(name => name.endsWith('.jsonl'))
|
|
.sort();
|
|
|
|
const allQuestions = [];
|
|
|
|
for (const file of jsonlFiles) {
|
|
const filePath = path.join(questionsDir, file);
|
|
const raw = fs.readFileSync(filePath, 'utf-8');
|
|
const lines = raw
|
|
.split('\n')
|
|
.map(line => line.trim())
|
|
.filter(line => line.length > 0 && !line.startsWith('#'));
|
|
|
|
for (let i = 0; i < lines.length; i++) {
|
|
const line = lines[i];
|
|
try {
|
|
const question = JSON.parse(line);
|
|
allQuestions.push(question);
|
|
} catch (error) {
|
|
console.error(`Parse error in ${file} at line ${i + 1}:`, line);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
}
|
|
|
|
const output = `import { Question } from '@/types/question';
|
|
|
|
// 本文件由 scripts/build-questions.js 自动生成,请勿手动编辑
|
|
// 题库源文件位于 src/data/questions/*.jsonl
|
|
// 用于「导入示例题库」功能
|
|
|
|
export const demoQuestions: Question[] = ${JSON.stringify(allQuestions, null, 2)};
|
|
|
|
export default demoQuestions;
|
|
`;
|
|
|
|
fs.writeFileSync(outputFile, output, 'utf-8');
|
|
console.log(`Generated ${outputFile} with ${allQuestions.length} questions from ${jsonlFiles.length} JSONL files.`);
|