#!/usr/bin/env node // Builds the authoritative manual feature matrix from the command matrix, // desktop routes, registry, UI catalogs and the official case ledger. // Fails when a module has no chapter or a native handler is unmapped. import fs from "node:fs"; import path from "node:path"; import { createHash } from "node:crypto"; import { fileURLToPath } from "node:url"; const here = path.dirname(fileURLToPath(import.meta.url)); const webRoot = path.resolve(here, "../.."); const outDir = path.join(webRoot, "manual", "data"); fs.mkdirSync(outDir, { recursive: true }); const readJson = file => JSON.parse(fs.readFileSync(file, "utf8")); const sha256 = file => createHash("sha256").update(fs.readFileSync(file)).digest("hex"); const matrixPath = path.join(webRoot, "public/api/workbench-command-matrix.json"); const routesPath = path.join(webRoot, "public/api/desktop-command-routes.json"); const registryPath = path.join(webRoot, "src/module-registry.generated.ts"); const featuresPath = path.join(webRoot, "teaching/data/features.json"); const casesPath = path.join(webRoot, "teaching/data/cases.json"); const ledgerPath = path.join(webRoot, "docs/current/freecad-official-case-ledger.json"); const matrix = readJson(matrixPath); const routes = readJson(routesPath); const features = readJson(featuresPath); // The module registry is the single owner of "which modules exist on the web". // The desktop command matrix is scraped from upstream source and keeps modules // that have been removed from the port (OpenSCAD), so counting rows there would // re-introduce a module the manual must not teach. Filter once, here. const registrySource = fs.readFileSync(registryPath, "utf8"); const webModules = new Set([...registrySource.matchAll(/\{ module: "([^"]+)"/g)].map(match => match[1])); if (!webModules.size) throw new Error(`no modules parsed from ${registryPath}`); const droppedRecords = (matrix.records ?? []).filter(record => !webModules.has(record.module)); matrix.records = (matrix.records ?? []).filter(record => webModules.has(record.module)); const cases = readJson(casesPath); const ledger = readJson(ledgerPath); const chapters = [ { id: "ch00", order: 0, level: "core", title: "导读、启动与 OPFS", moduleIds: ["Start", "Web", "Help"] }, { id: "ch01", order: 1, level: "core", title: "界面、文档与视图", moduleIds: [] }, { id: "ch02", order: 2, level: "core", title: "Part 图元与布尔", moduleIds: ["Part"] }, { id: "ch03", order: 3, level: "core", title: "Sketcher 草图", moduleIds: ["Sketcher"] }, { id: "ch04", order: 4, level: "core", title: "PartDesign 实体建模", moduleIds: ["PartDesign"] }, { id: "ch05", order: 5, level: "core", title: "Assembly 装配", moduleIds: ["Assembly"] }, { id: "ch06", order: 6, level: "core", title: "测量、检查、材质与表格", moduleIds: ["Measure", "Inspection", "Material", "Spreadsheet"] }, { id: "ch07", order: 7, level: "core", title: "导入导出与 OPFS", moduleIds: ["Import"] }, { id: "ch08", order: 8, level: "specialist", title: "网格、点、逆向与曲面", moduleIds: ["Mesh", "MeshPart", "Points", "ReverseEngineering", "Surface"] }, { id: "ch09", order: 9, level: "specialist", title: "BIM 建筑信息模型", moduleIds: ["BIM"] }, { id: "ch10", order: 10, level: "specialist", title: "CAM 加工", moduleIds: ["CAM"] }, { id: "ch11", order: 11, level: "specialist", title: "TechDraw 工程图", moduleIds: ["TechDraw"] }, { id: "ch12", order: 12, level: "specialist", title: "FEM 有限元", moduleIds: ["Fem"] }, { id: "ch13", order: 13, level: "specialist", title: "Draft、Plot、Robot 等专项工具", moduleIds: ["Draft", "Plot", "Robot", "Cloud", "JtReader", "Sandbox", "Test", "Tux", "TemplatePyMod", "Show"] }, { id: "ch14", order: 14, level: "case", title: "综合案例:27 件 GLB 产线", moduleIds: [] }, { id: "ch15", order: 15, level: "appendix", title: "附录:命令索引与缺口", moduleIds: [] }, ]; const moduleChapter = new Map(); for (const chapter of chapters) for (const moduleId of chapter.moduleIds) moduleChapter.set(moduleId, chapter.id); const moduleOfChapter = id => chapters.find(chapter => chapter.id === id); const boundaries = [ { id: "b-techdraw-worker", module: "TechDraw", match: ["TechDraw_PageDefault"], title: "TechDraw 跨 Worker 文档桥", detail: "页面 Worker 未持有目标文档,TechDraw_PageDefault 报 Document does not exist;SVG 回退不是 TechDraw 输出。", desktopRefs: ["freecad-source/src/Mod/TechDraw/App/DrawPage.cpp", "freecad-source/src/Mod/TechDraw/App/DrawViewPart.cpp"] }, { id: "b-fem-native", module: "Fem", match: ["FEM_ResultShow", "PostPipelineFromResult"], title: "FEM 原生结果对象", detail: "FemMeshObject/FemResultObject 未随包发布,结果停留在 Worker;VTK 显示为 three.js 近似。", desktopRefs: ["freecad-source/src/Mod/Fem/App/FemMeshObject.cpp", "web/docs/current/web-freecad-gaps-fem.md"] }, { id: "b-cam-opencamlib", module: "CAM", match: ["CAM_Adaptive", "CAM_Surface", "CAM_Waterline"], title: "CAM OpenCAMLib 路径", detail: "Adaptive/Surface/Waterline 依赖 OpenCAMLib 与完整 ToolBit/G-code 栈,WASM 包未提供。", desktopRefs: ["freecad-source/src/Mod/CAM/App/AppPath.cpp", "web/docs/current/cam-teaching-gap.md"] }, { id: "b-gltf-native", module: "Import", match: ["Import_Gltf"], title: "glTF 原生导入/导出", detail: "OCCT WASM 构建 USE_RAPIDJSON=OFF,RWGltf 无法读写;当前仅 three.js 查看。", desktopRefs: ["freecad-source/src/Mod/Import/App/ReaderGltf.cpp", "freecad-source/src/Mod/Import/App/WriterGltf.cpp"] }, { id: "b-cloud-remote", module: "Cloud", match: [], title: "Cloud 远程存储", detail: "Save/Restore 需要远程 S3 凭据与服务;本地 OPFS 不受影响。", desktopRefs: ["web/src/cloud.worker.ts"] }, ]; const boundaryByKey = new Map(); for (const boundary of boundaries) for (const command of boundary.match) boundaryByKey.set(`${boundary.module}:${command}`, boundary); // User-facing command catalogs (regex extraction is deliberate: these files are // the UI source of truth and are validated by capture later). const uiCommands = new Set(); for (const relative of ["src/part-workbench-ui.tsx", "src/partdesign-desktop-adapter.ts", "src/sketcher-desktop-adapter.ts"]) { const text = fs.readFileSync(path.join(webRoot, relative), "utf8"); for (const match of text.matchAll(/\b(?:Part|PartDesign|Sketcher)_[A-Za-z0-9_]+/g)) uiCommands.add(match[0]); } // Passed cases keyed by desktop command for status hints (capture still decides verified). const caseByCommand = new Map(); for (const item of cases.cases ?? []) { if (!item.command) continue; if (!(item.checks ?? []).find(check => check.flag === "acceptancePassed")?.passed) continue; caseByCommand.set(item.command, item.caseId); } const commands = []; const moduleSeen = new Set(); for (const record of matrix.records ?? []) { const chapterId = moduleChapter.get(record.module) ?? null; moduleSeen.add(record.module); const boundary = boundaryByKey.get(`${record.module}:${record.command}`) ?? null; const ui = uiCommands.has(record.command); const native = record.disposition === "native_handler"; const level = chapterId ? moduleOfChapter(chapterId)?.level : null; let status = "overview"; if (boundary) status = "boundary"; else if (level === "core" && (native || ui)) status = "detailed"; else if (level === "specialist" && (native || ui)) status = "overview"; commands.push({ key: record.id ?? `${record.module}:desktop:${record.command}`, module: record.module, command: record.command, kind: record.kind ?? "desktop", disposition: record.disposition ?? "unknown", route: record.route ?? null, chapterId, workflowId: null, status, uiSurface: ui, ledgerCaseId: caseByCommand.get(record.command) ?? null, boundaryId: boundary?.id ?? null, screenshotId: null, sourcePath: record.sourcePath ?? null, sourceLine: record.sourceLine ?? null, desktopRef: record.sourcePath ? `${record.sourcePath}:${record.sourceLine ?? ""}` : null, }); } // Registry-only routes that do not appear in the desktop matrix still belong in // the appendix (e.g. assembly.createRecompute). const matrixKeys = new Set(commands.map(item => `${item.module}:${item.command}`)); for (const module of features.modules ?? []) { const chapterId = moduleChapter.get(module.module) ?? null; if (!chapterId) throw new Error(`registry module has no chapter: ${module.module}`); for (const command of module.commands ?? []) { if (matrixKeys.has(`${module.module}:${command}`)) continue; matrixKeys.add(`${module.module}:${command}`); commands.push({ key: `${module.module}:web:${command}`, module: module.module, command, kind: "web-route", disposition: "registered", route: null, chapterId, workflowId: null, status: "overview", uiSurface: false, ledgerCaseId: null, boundaryId: null, screenshotId: null, sourcePath: null, sourceLine: null, desktopRef: null, }); } } const unknownModules = [...moduleSeen].filter(module => !moduleChapter.has(module)); if (unknownModules.length) throw new Error(`modules without chapter: ${unknownModules.join(", ")}`); const nativeUnmapped = commands.filter(item => item.disposition === "native_handler" && !item.chapterId); if (nativeUnmapped.length) throw new Error(`native handlers without chapter: ${nativeUnmapped.length}`); const stats = { modules: moduleSeen.size, commands: commands.length, desktopRecords: matrix.records?.length ?? 0, routes: routes.records?.length ?? 0, registryCommands: (features.modules ?? []).reduce((sum, module) => sum + (module.commands?.length ?? 0), 0), nativeHandlers: commands.filter(item => item.disposition === "native_handler").length, detailed: commands.filter(item => item.status === "detailed").length, overview: commands.filter(item => item.status === "overview").length, boundary: commands.filter(item => item.status === "boundary").length, verifiedCandidates: commands.filter(item => item.ledgerCaseId).length, droppedDesktopRecords: droppedRecords.length, droppedModules: [...new Set(droppedRecords.map(record => record.module))], screenshots: 0, workflows: 0, }; const coverage = { schema: "freecad-web-manual/coverage/v1", generatedAt: new Date().toISOString(), modules: moduleSeen.size, unknownModules, nativeHandlers: stats.nativeHandlers, nativeMapped: stats.nativeHandlers, desktopRecords: stats.desktopRecords, registryCommands: stats.registryCommands, routes: stats.routes, byChapter: Object.fromEntries(chapters.map(chapter => [chapter.id, commands.filter(item => item.chapterId === chapter.id).length])), byStatus: Object.fromEntries(["detailed", "overview", "boundary"].map(status => [status, commands.filter(item => item.status === status).length])), ok: unknownModules.length === 0 && stats.nativeHandlers === stats.nativeHandlers, }; const sources = [matrixPath, routesPath, registryPath, featuresPath, casesPath, ledgerPath] .map(file => ({ path: path.relative(webRoot, file), sha256: sha256(file) })); const output = { schema: "freecad-web-manual/features/v1", generatedAt: new Date().toISOString(), sources, stats, chapters, workflows: [], commands, screenshots: [], boundaries, }; fs.writeFileSync(path.join(outDir, "manual-features.json"), `${JSON.stringify(output, null, 2)}\n`); fs.writeFileSync(path.join(outDir, "inventory-snapshot.json"), `${JSON.stringify({ schema: "freecad-web-manual/inventory/v1", generatedAt: output.generatedAt, sources, }, null, 2)}\n`); fs.writeFileSync(path.join(outDir, "coverage-audit.json"), `${JSON.stringify(coverage, null, 2)}\n`); console.log(JSON.stringify({ ok: true, stats, coverage: coverage.byChapter }));