/** * Canonical catalog of the 20 carrier PartDesign parts. * * Like tools/reducer/catalog.mjs, this module owns no part list of its own. The * single source is the case spec (one inventory for builders, toolkit and * teaching), joined with the three evidence trees on disk: * - web/teaching/cases/carrier-partdesign/out/replay/.json (build record) * - web/tools/carrier-partdesign-course/out/ui-steps/.json (UI steps) * - web/tools/carrier-partdesign-course/out/ui-run/.json (last headed run) * * A part is only offered when both replay and ui-steps 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 CASE_DIR = path.join(REPO_ROOT, "web/teaching/cases/carrier-partdesign"); export const SPEC_FILE = path.join(CASE_DIR, "spec.json"); export const REPLAY_DIR = path.join(CASE_DIR, "out/replay"); export const PROJECT_DIR = path.join(REPO_ROOT, "web/tools/carrier-partdesign-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"); function readJson(file) { try { return JSON.parse(fs.readFileSync(file, "utf8")); } catch { return null; } } export function spec() { const payload = readJson(SPEC_FILE); if (!payload) throw new Error(`缺少案例清单:${SPEC_FILE}`); return payload; } export function projectManifest() { const manifest = readJson(PROJECT_FILE); if (!manifest) throw new Error(`缺少项目清单:${PROJECT_FILE}`); return manifest; } 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(REPLAY_DIR, `${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; } /** All parts, in spec order, enriched with step counts and recorded volume. */ export function parts() { const manifest = spec(); const systems = manifest.systems ?? {}; return (manifest.parts ?? []).map(entry => { const steps = stepsOf(entry.document); const replay = replayOf(entry.document); return { id: entry.id, document: entry.document, title: entry.title, system: entry.system, systemLabel: systems[entry.system] ?? entry.system, module: entry.module, featureTypes: entry.featureTypes ?? [], placement: entry.placement ?? null, 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() { const systems = spec().systems ?? {}; const all = parts(); return Object.entries(systems) .map(([id, label]) => { const members = all.filter(part => part.system === id); return { id, 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 "CarrierElevators" 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, volume: all.reduce((sum, part) => sum + (part.volume ?? 0), 0), }; }