#!/usr/bin/env node
// Generates web/teaching/cases/production-line-course.html from course.json,
// per-episode scripts and the media directory. Missing media is marked pending.
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
const here = path.dirname(fileURLToPath(import.meta.url));
const webRoot = path.resolve(here, "../..");
const course = JSON.parse(fs.readFileSync(path.join(here, "course.json"), "utf8"));
const mediaDir = path.join(webRoot, "teaching", "media", "line-course");
const outFile = path.join(webRoot, "teaching", "cases", "production-line-course.html");
const esc = value => String(value ?? "").replace(/&/g, "&").replace(//g, ">").replace(/"/g, """);
const mediaBase = `/media/line-course/`;
const has = suffix => episode => fs.existsSync(path.join(mediaDir, `${episode.id}-${episode.slug}${suffix}`));
const episodeHtml = episode => {
const scriptPath = path.join(here, "episodes", episode.id, "script.zh.json");
const lesson = fs.existsSync(scriptPath)
? JSON.parse(fs.readFileSync(scriptPath, "utf8")).lesson ?? {}
: {};
const base = `${mediaBase}${episode.id}-${episode.slug}`;
const hasVideo = has(".mp4")(episode);
const parameters = (lesson.parameters ?? []).map(item =>
`
| ${esc(item.name)} | ${esc(item.value)} |
`).join("");
const checklist = (lesson.checklist ?? []).map(item => `${esc(item)}`).join("");
const mistakes = (lesson.mistakes ?? []).map(item => `${esc(item)}`).join("");
const objectives = (lesson.objectives ?? []).map(item => `${esc(item)}`).join("");
const source = path.join(here, "checkpoints", `${episode.id}.FCStd`);
const copied = path.join(mediaDir, "checkpoints", `${episode.id}.FCStd`);
if (fs.existsSync(source)) {
fs.mkdirSync(path.dirname(copied), { recursive: true });
fs.copyFileSync(source, copied);
}
const checkpoint = fs.existsSync(copied)
? `下载 ${episode.id} 检查点 FCStd`
: `检查点:${esc(episode.checkpointOut ?? "无")}`;
const stillSource = path.join(here, "stills", episode.id);
const stillFiles = fs.existsSync(stillSource)
? fs.readdirSync(stillSource).filter(name => name.endsWith(".png")).sort()
: [];
const stillTarget = path.join(mediaDir, "stills", episode.id);
if (stillFiles.length) {
fs.mkdirSync(stillTarget, { recursive: true });
for (const name of stillFiles) fs.copyFileSync(path.join(stillSource, name), path.join(stillTarget, name));
}
const stillsHtml = stillFiles.map(name =>
`
${esc(name.replace(/\.png$/, ""))}`).join("");
return `
${hasVideo
? `
MP4 · WebM · SRT
`
: `视频待制作(脚本与参数已就绪)。
`}
分步截图(${stillFiles.length})
${stillsHtml}
学习目标与参数表
自检清单
常见错误与练习
${esc(lesson.exercise ?? "")}
${checkpoint}
`;
};
const seasons = (course.seasons ?? []).map(season => {
const episodes = season.episodes.map(id => course.episodes.find(item => item.id === id)).filter(Boolean);
return `
${esc(season.id)} · ${esc(season.title)}
${episodes.map(episodeHtml).join("\n")}
`;
}).join("\n");
const heroHtml = fs.existsSync(path.join(webRoot, "teaching", "cases", "production-line-assembly.html"))
? `
整线场景:参考模型与重建结果
重建结果:FreeCAD 27 件模型 · 交互查看
参考模型 27 个 GLB · 交互查看
` : "";
const html = `
${esc(course.title)}
${heroHtml}
${seasons}
`;
fs.writeFileSync(outFile, html);
console.log(JSON.stringify({ ok: true, outFile, episodes: course.episodes.length }));