#!/usr/bin/env node /** * Generate web/teaching/console.html from template.html + the curriculum. * * Deterministic: no timestamps, sorted inputs, so `--check` can byte-compare. * The generated page embeds the lesson catalog and a copyable command per * lesson; at runtime it talks to web/teaching/server.mjs for real runs. * * Usage: * node web/tools/teaching-console/build-console.mjs # write * node web/tools/teaching-console/build-console.mjs --check # exit 1 if stale * node web/tools/teaching-console/build-console.mjs --out * Exit: 0 written/identical, 1 stale (--check), 2 bad arguments. */ import fs from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { loadCatalog } from "./catalog.mjs"; import { buildLessons } from "./lessons.mjs"; import { argvToCommand, buildJobArgv } from "../../teaching/suite-runner.mjs"; const HERE = path.dirname(fileURLToPath(import.meta.url)); const DEFAULT_OUT = path.resolve(HERE, "../../teaching/console.html"); function parseArgs(argv) { const options = { check: false, out: DEFAULT_OUT }; for (let index = 0; index < argv.length; index += 1) { const arg = argv[index]; if (arg === "--check") options.check = true; else if (arg === "--out") options.out = path.resolve(argv[++index] ?? DEFAULT_OUT); else if (arg === "--help" || arg === "-h") { console.log("node web/tools/teaching-console/build-console.mjs [--check] [--out ]"); process.exit(0); } else { console.error(`未知参数:${arg}`); process.exit(2); } } return options; } function json(value) { return JSON.stringify(value).replace(/" }); commands[lesson.id] = argvToCommand(argv); } const template = fs.readFileSync(path.join(HERE, "template.html"), "utf8"); const html = template .replace("{{LESSONS_JSON}}", json(curriculum)) .replace("{{CATALOG_JSON}}", json({ shell: cases.shell, crosscut: cases.crosscut, chapters: cases.chapters, modules: cases.modules })) .replace("{{COMMANDS_JSON}}", json(commands)); for (const marker of ["{{LESSONS_JSON}}", "{{CATALOG_JSON}}", "{{COMMANDS_JSON}}"]) { if (html.includes(marker)) throw new Error(`占位符未替换:${marker}`); } return { html, curriculum, cases }; } function main() { const options = parseArgs(process.argv.slice(2)); const { html, curriculum, cases } = buildHtml(); if (options.check) { let existing = ""; try { existing = fs.readFileSync(options.out, "utf8"); } catch { console.error(`FAIL --check:目标文件不存在 ${options.out}`); process.exit(1); } if (existing === html) { console.log(`check: OK(${options.out} 已是最新)`); return; } let offset = 0; while (offset < existing.length && offset < html.length && existing[offset] === html[offset]) offset += 1; console.error(`FAIL --check:磁盘文件与生成结果不一致(首个差异偏移 ${offset})`); process.exit(1); } fs.mkdirSync(path.dirname(options.out), { recursive: true }); fs.writeFileSync(options.out, html); console.log(`written ${path.relative(path.resolve(HERE, "../../.."), options.out)} (${(Buffer.byteLength(html) / 1024).toFixed(0)} KB, ${curriculum.lessons.length} lessons, ${cases.ids.length} cases)`); } if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) main();