#!/usr/bin/env node // Derives web/manual/data/final-status.json from the real pipeline outputs so // the summary never drifts from the generated manual: // // manual-features.json commands / modules / boundaries / chapters // gap-ledger.json verified case count and known boundaries // page-check.json last browser check (overflow, images, sections) // capture/*.json screenshot capture reports // manual-assets/** actual PNG files on disk // // node tools/manual/generate-final-status.mjs // node tools/manual/generate-final-status.mjs --check # exit 1 on drift 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 dataDir = path.join(webRoot, "manual", "data"); const read = name => JSON.parse(fs.readFileSync(path.join(dataDir, name), "utf8")); const args = process.argv.slice(2); const check = args.includes("--check"); const outPath = path.join(dataDir, "final-status.json"); const features = read("manual-features.json"); const ledger = read("gap-ledger.json"); const pageCheck = read("page-check.json"); const captureDir = path.join(dataDir, "capture"); const captureReports = fs.existsSync(captureDir) ? fs.readdirSync(captureDir).filter(name => name.endsWith(".json")).sort() : []; let screenshotsCaptured = 0; for (const name of captureReports) { const report = JSON.parse(fs.readFileSync(path.join(captureDir, name), "utf8")); screenshotsCaptured += (report.shots ?? []).length; } const assetsDir = path.join(webRoot, "manual", "assets"); const countPngs = dir => fs.readdirSync(dir, { withFileTypes: true }).reduce((total, entry) => { const full = path.join(dir, entry.name); if (entry.isDirectory()) return total + countPngs(full); return total + (entry.name.endsWith(".png") ? 1 : 0); }, 0); const screenshots = fs.existsSync(assetsDir) ? countPngs(assetsDir) : 0; const appChapters = features.chapters.filter(chapter => chapter.level !== "appendix").length; const desktop = pageCheck.viewports.find(view => view.viewport.width >= 900) ?? pageCheck.viewports[0] ?? null; const mobile = pageCheck.viewports.find(view => view.viewport.width < 900) ?? null; const gapRegisterPath = path.join(webRoot, "..", "docs/current/ui-design/data/gap-register.json"); const gapRegister = fs.existsSync(gapRegisterPath) ? JSON.parse(fs.readFileSync(gapRegisterPath, "utf8")) : null; const state = pageCheck.ok && (gapRegister ? gapRegister.ok : false) ? "complete" : "in-progress"; const status = { schema: "freecad-web-manual/final-status/v1", generatedAt: new Date().toISOString(), state, chapters: appChapters, chaptersWithAppendix: features.chapters.length, screenshots, commands: features.stats.commands, modules: features.stats.modules, nativeHandlers: features.stats.nativeHandlers, verifiedCommands: ledger.verified, workflows: captureReports.length, boundaries: (features.boundaries ?? []).length, pageCheck: { ok: pageCheck.ok, desktopOverflow: desktop?.overflow ?? null, mobileOverflow: mobile?.overflow ?? null, brokenImages: desktop?.brokenImages ?? [], searchHits: desktop?.searchHits ?? 0, images: desktop?.images ?? 0, sections: desktop?.sections ?? 0, }, entry: "web/teaching/manual.html", url: pageCheck.url, assets: "web/teaching/manual-assets", captureReports: "web/manual/data/capture", gapLedger: "web/manual/data/gap-ledger.json", knownBoundaries: (ledger.boundaries ?? []).map(item => item.title), notes: [ "由 generate-final-status.mjs 自动派生,不要手改。", `UI 表面清单:${gapRegister ? gapRegister.counts.surfaces : "未生成"} 项,缺口 ${gapRegister ? gapRegister.issues.filter(issue => issue.problem === "ui-gap").length : "未知"} 项。`, ], screenshotsCaptured, }; const text = `${JSON.stringify(status, null, 2)}\n`; if (check) { const previous = fs.existsSync(outPath) ? fs.readFileSync(outPath, "utf8") : ""; const strip = value => JSON.stringify({ ...JSON.parse(value), generatedAt: "__stable__" }); if (!previous || strip(previous) !== strip(text)) { console.error("[generate-final-status] --check: final-status.json is stale"); process.exit(1); } console.log(JSON.stringify({ ok: true, checked: "web/manual/data/final-status.json" })); process.exit(0); } fs.writeFileSync(outPath, text); console.log(JSON.stringify({ ok: true, out: "web/manual/data/final-status.json", state, chapters: features.chapters.length, screenshots, commands: features.stats.commands }));