freeCodeCamp 如何新建一份认证考试文件并用 create-exams 种子化?
【免费下载链接】freeCodeCampfreeCodeCamp.org's open-source codebase and curriculum. Learn math, programming, and computer science for free.项目地址: https://gitcode.com/GitHub_Trending/fr/freeCodeCamp
在 freeCodeCamp 仓库中,认证考试(certification exam)的题目存放在tools/scripts/seed-exams/工具的exams/目录里,通过create-exams.js脚本写入 MongoDB。如果你要新增一份考试,完整任务路径是:在exams/下新建一份 YAML 文件并填写元数据与题目 → 用add-nano-ids.js为每道题和每个选项生成id→ 把新文件加入create-exams.js的播种清单并运行脚本,把考试写入数据库的Exam集合。
开始前先记住仓库文档给出的两条硬约束(见 seed-exams README):
- Never change any of the ID's or delete anything. Mark things as deprecated instead.已有的题目、选项、考试 ID 不能修改或删除,只能加
deprecated: true标记。 - 该目录下的考试文件不用于生产环境,永远不要把真实考试题目推送到 GitHub 或任何公开位置。仓库里现有的 YAML 只是本地开发与测试用的示例;要种子化真实考试时,需要把示例考试替换为真实考试、连接目标数据库后再运行
create-exams.js。
前置条件
运行环境满足 seed-exams 的 package.json 声明:
node >= 24、pnpm >= 10,依赖(mongodb、joi、js-yaml、nanoid、dotenv、debug等)已由该工作区包安装。仓库根目录存在
.env文件且包含MONGOHQ_URL。create-exams.js通过dotenv.config({ path: path.resolve(__dirname, '../../../.env') })加载根目录.env,并用MONGOHQ_URL建立 Mongo 连接。sample.env 中给出的本地示例值为:MONGOHQ_URL=mongodb://127.0.0.1:27017/freecodecamp?directConnection=true目标数据库可达。脚本连接后对
freecodecamp库的Exam集合执行 upsert。
第一步:在 exams 目录新建考试文件
参照现成示例 example-certification-exam.yml,在 tools/scripts/seed-exams/exams 目录下新建一份 YAML 文件。仓库中已有的两份文件可作为结构参照:
example-certification-exam.yml:5 题、passingPercent: 70,无prerequisites;foundational-c-sharp-with-microsoft-certification-exam.yml:含prerequisites数组的完整示例。
元数据字段按 README 的要求填写:
| 字段 | 要求 |
|---|---|
_id | 必须与考试对应的 challenge markdown 文件中的id一致(例如 en-a2-certification-exam 的 challenge 文件 frontmatter 中的id: 6721db5d9f0c116e6a0fe25a) |
title | 必须与考试 challenge markdown 文件中的title一致 |
numberOfQuestionsInExam | 考生将看到的题目数,必须小于或等于questions数组长度 |
passingPercent | 通过考试所需的正确率百分比 |
prerequisites | 可选。数组,每项含id与title,须与对应 challenge markdown 文件中的内容匹配 |
questions | 题目数组 |
题目与选项的结构是:question为题干字符串;wrongAnswers至少 4 项(README 建议 6–7 项);correctAnswers至少 1 项(建议 2 项以上);每个选项都带answer字段。此时先不要填id——question、wrongAnswers、correctAnswers各级的id由下一步的脚本统一生成。
第二步:用 add-nano-ids.js 生成题目与选项 ID
打开 add-nano-ids.js,把顶部的examPath变量改为新考试文件的路径(默认值是./exams/example-certification-exam.yml):
// Change this to the path of the file you want to add id's to const examPath = './exams/your-new-exam.yml';然后在tools/scripts/seed-exams/目录下运行:
node add-nano-ids.js注意副作用:该脚本会读取目标文件、为缺少id的每个question和每个wrongAnswers/correctAnswers选项生成一个 10 位[a-z0-9]的 nano id,再用writeFileSync原地重写这份 YAML 文件(已存在的id保持不变)。运行前确认文件内容无误,运行后应能看到每个对象顶部多出id字段。
第三步:把新考试加入 create-exams.js 的播种清单
打开 create-exams.js,把新文件名加入examFilenames数组——脚本顶部注释明确说明Only these will be added to or updated in the database,不在清单里的文件不会被种子化:
// Only these will be added to or updated in the database const examFilenames = [ 'foundational-c-sharp-with-microsoft-certification-exam.yml', 'example-certification-exam.yml', 'your-new-exam.yml' ];第四步:运行 create-exams.js 种子化
副作用说明:该脚本会连接MONGOHQ_URL指向的数据库,对freecodecamp库的Exam集合执行updateOne(..., { $set: examJson }, { upsert: true })——按_id匹配,已存在的文档会被覆盖更新,不存在时新建。因此只对本地开发库或你明确授权写入的 staging 库运行。
在tools/scripts/seed-exams/目录下运行:
node create-exams.js脚本对清单中每个文件依次执行:读取 YAML → 用 exam-schema.js 做 Joi 校验 → 把_id转成ObjectId→ upsert 到Exam集合。
如何判断执行成功
create-exams.js使用debug('fcc:tools:seedExams')输出日志,成功时的关键输出为:
Connected successfully to mongo 'Example Certification Exam' added to exams database. Finished seeding exams.(以上文字取自脚本源码的 log 语句,其中考试标题会随你的title字段变化。)
失败时脚本打印Oh noes!! Error seeding exams.和具体错误后以退出码 1 结束。最常见的失败是 schema 校验不过,报错形如Invalid exam schema for '<文件名>': <原因>。exam-schema.js 会拦截的典型问题包括:
wrongAnswers中未标记 deprecated 的选项少于 4 个('wrongAnswers' must have at least 4 non-deprecated answers.);correctAnswers中未标记 deprecated 的选项少于 1 个;numberOfQuestionsInExam超过未废弃题目数量(上限按非 deprecated 的questions计数计算);question/answer的id缺失或不符合 10 位[a-z0-9]格式——这正是漏跑add-nano-ids.js会触发的情况;_id不是合法的 ObjectId,passingPercent不在 0–100 范围内。
废弃题目与选项:只能标记,不能删除
README 规定:对questions、wrongAnswers、correctAnswers中的任意一项加上deprecated: true,该项在生成考试时即被省略。示例文件 example-certification-exam.yml 中有两处现成用法:id: hx0gxr5yjc的题目整体标了deprecated: true;id: fmo9sof6v2的选项(answer: '== ')单独标了废弃。这与 schema 的计数逻辑对应——废弃项不计入numberOfQuestionsInExam上限和最少选项数的校验。
完成后的状态与限制
- 考试文档已按
_id写入Exam集合;同一_id重跑脚本会整体更新该文档。 _id、title必须与考试 challenge markdown 文件保持一致,prerequisites每项的id/title同理——这是 README 明确的匹配要求。- 仓库内
exams/目录中的文件只用于本地开发;真实题目不要提交到公开仓库。
【免费下载链接】freeCodeCampfreeCodeCamp.org's open-source codebase and curriculum. Learn math, programming, and computer science for free.项目地址: https://gitcode.com/GitHub_Trending/fr/freeCodeCamp
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考