A lesson-oriented page plus a loopback job service: click a lesson and the page really runs tools/web-freecad-functional-suite.mjs, tools/layout-probe.mjs or tools/static-host.mjs, streaming logs and rendering verdicts from the suite's own manifest/checkpoint/results files. - web/tools/teaching-console: catalog derived from scenarios + registry, 73 workbench lessons, template, builder (--check), Playwright gate, no-browser self-test for every spawn validation path - web/teaching/suite-runner.mjs: argv whitelist, env whitelist, single concurrent job, process-group cancel, ring-buffered logs, preflight - web/teaching/server.mjs: /api/console/* routes with startup token, Origin check, SSE events, artifact serving; existing routes untouched - preflight blocks runs (E_PREFLIGHT) instead of starting without dist, Chrome, DISPLAY, kernel or Playwright - gates: self-test OK, build --check idempotent, offline + live Playwright (MOD-Part-01 pass, cancel path) OK Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
98 lines
3.8 KiB
JavaScript
98 lines
3.8 KiB
JavaScript
#!/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 <file>
|
||
* 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 <file>]");
|
||
process.exit(0);
|
||
} else {
|
||
console.error(`未知参数:${arg}`);
|
||
process.exit(2);
|
||
}
|
||
}
|
||
return options;
|
||
}
|
||
|
||
function json(value) {
|
||
return JSON.stringify(value).replace(/</g, "\\u003c");
|
||
}
|
||
|
||
export function buildHtml() {
|
||
const cases = loadCatalog();
|
||
const curriculum = buildLessons(cases);
|
||
const commands = {};
|
||
for (const lesson of curriculum.lessons) {
|
||
if (lesson.job.kind === "preflight") continue;
|
||
if (lesson.job.selectable) {
|
||
commands[lesson.id] = "(自由练习:勾选用例后由服务端生成受约束命令)";
|
||
continue;
|
||
}
|
||
const { argv } = buildJobArgv(lesson, {}, { jobDir: "<jobDir>" });
|
||
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();
|