/** * Canonical catalog of the 23 secondary-reducer parts. * * This module deliberately owns no part list of its own: it reads the project * manifest that the shared UI driver already uses * (web/tools/reducer-parts-course/project.json) and joins it with the two * evidence trees that already exist on disk: * - out/ui-steps/.json — the per-step UI actions drive.mjs replays * - out/replay/.json — the build record, including final volume * * A part is only offered when both files exist, so `create-part` can never * start a browser for a part that has nothing to replay. */ import fs from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; const HERE = path.dirname(fileURLToPath(import.meta.url)); export const REPO_ROOT = path.resolve(HERE, "../.."); export const PROJECT_DIR = path.join(REPO_ROOT, "web/tools/reducer-parts-course"); export const PROJECT_FILE = path.join(PROJECT_DIR, "project.json"); const STEPS_DIR = path.join(PROJECT_DIR, "out/ui-steps"); const RUN_DIR = path.join(PROJECT_DIR, "out/ui-run"); const STILLS_DIR = path.join(PROJECT_DIR, "ui-stills"); /** Document prefix → teaching group. Longest prefix wins. */ const GROUPS = [ { id: "gears", label: "齿轮", prefixes: ["Gear"] }, { id: "shafts", label: "轴", prefixes: ["Shaft"] }, { id: "bearings", label: "轴承", prefixes: ["Bearing"] }, { id: "keys", label: "平键", prefixes: ["Key"] }, { id: "seals", label: "骨架油封", prefixes: ["Seal"] }, { id: "coupling", label: "联轴器", prefixes: ["Coupling"] }, { id: "housing", label: "箱体与箱盖", prefixes: ["Housing", "Inspection"] }, ]; export function groupOf(document) { const match = GROUPS.find((group) => group.prefixes.some((prefix) => document.startsWith(prefix))); return match ?? { id: "other", label: "其他", prefixes: [] }; } function readJson(file) { try { return JSON.parse(fs.readFileSync(file, "utf8")); } catch { return null; } } function stepsOf(document) { const payload = readJson(path.join(STEPS_DIR, `${document}.json`)); return Array.isArray(payload?.steps) ? payload.steps : null; } function replayOf(document) { return readJson(path.join(PROJECT_DIR, "out/replay", `${document}.json`)) ?? readJson(path.join(REPO_ROOT, "web/teaching/cases/secondary-reducer-pro/out/replay", `${document}.json`)); } function finalVolume(steps) { const volumes = (steps ?? []).map((step) => step.volume).filter((volume) => typeof volume === "number" && volume > 0); return volumes.length ? volumes.at(-1) : null; } export function projectManifest() { const manifest = readJson(PROJECT_FILE); if (!manifest) throw new Error(`缺少项目清单:${PROJECT_FILE}`); return manifest; } /** All parts, in manifest order, enriched with step counts and recorded volume. */ export function parts() { const manifest = projectManifest(); return (manifest.parts ?? []).map((entry) => { const steps = stepsOf(entry.document); const replay = replayOf(entry.document); const group = groupOf(entry.document); return { id: entry.id, document: entry.document, title: entry.title, group: group.id, groupLabel: group.label, steps: steps?.length ?? 0, volume: finalVolume(replay?.steps), reconstructable: Boolean(steps && steps.length && replay?.steps?.length), stepsFile: path.relative(REPO_ROOT, path.join(STEPS_DIR, `${entry.document}.json`)), runFile: path.relative(REPO_ROOT, path.join(RUN_DIR, `${entry.document}.json`)), stillsDir: path.relative(REPO_ROOT, path.join(STILLS_DIR, entry.id)), }; }); } export function groups() { return GROUPS.map((group) => { const members = parts().filter((part) => part.group === group.id); return { id: group.id, label: group.label, count: members.length, ids: members.map((part) => part.id) }; }).filter((group) => group.count > 0); } const ID_PATTERN = /^[A-Za-z][A-Za-z0-9_-]{0,63}$/; /** * Resolve "P06", "p06" or "Bearing6206_Inner" to a part. * Rejects anything that is not a plain token so a shell fragment can never be * forwarded to the driver. */ export function find(key) { if (typeof key !== "string" || !ID_PATTERN.test(key)) { const error = new Error(`非法零件标识:${String(key).slice(0, 40)}`); error.code = "E_PART"; throw error; } const wanted = key.toLowerCase(); const all = parts(); const match = all.find((part) => part.id.toLowerCase() === wanted) ?? all.find((part) => part.document.toLowerCase() === wanted); if (!match) { const error = new Error(`没有该零件:${key}`); error.code = "E_PART"; throw error; } return match; } export function stats() { const all = parts(); return { parts: all.length, steps: all.reduce((sum, part) => sum + part.steps, 0), reconstructable: all.filter((part) => part.reconstructable).length, }; }