返回 CodeWhale
check-docs.mjs
根目录 / web / scripts / check-docs.mjs
1 #!/usr/bin/env node
2 /**
3 * check-docs.mjs — drift / parity gate for website documentation.
4 *
5 * Verifies that:
6 * 1. Every doc topic in docs-map.ts points to a real repo source file.
7 * 2. Version, command snippets, and tool names referenced on the website
8 * match the current workspace state.
9 *
10 * Usage:
11 * cd web && npm run check:docs
12 *
13 * Relies on facts-lib.mjs for version / provider / tool derivation.
14 */
15 import { readFileSync, existsSync, readdirSync } from "node:fs";
16 import { resolve, dirname } from "node:path";
17 import { fileURLToPath } from "node:url";
18 import { buildInstallGuide, installAnchorErrors } from "./install-guide-lib.mjs";
19 import { readDocTopics } from "./docs-sources-lib.mjs";
20
21 const __dirname = dirname(fileURLToPath(import.meta.url));
22 const WEB_DIR = resolve(__dirname, "..");
23 const REPO_ROOT = resolve(WEB_DIR, "..");
24
25 /* ------------------------------------------------------------------ */
26 /* Parse docs-map.ts (regex — avoids ts-node dependency) */
27 /* ------------------------------------------------------------------ */
28
29 /* ------------------------------------------------------------------ */
30 /* Check 1: every repo source file exists */
31 /* ------------------------------------------------------------------ */
32
33 function checkSourcesExist(topics) {
34 const missing = [];
35 for (const t of topics) {
36 for (const src of t.repoSource) {
37 const p = resolve(REPO_ROOT, src);
38 if (!existsSync(p)) {
39 missing.push({ topic: t.id, source: src, expected: p });
40 }
41 }
42 }
43 return missing;
44 }
45
46 /* ------------------------------------------------------------------ */
47 /* Check 2: version matches Cargo.toml */
48 /* ------------------------------------------------------------------ */
49
50 function deriveVersion() {
51 const cargoPath = resolve(REPO_ROOT, "Cargo.toml");
52 if (!existsSync(cargoPath)) return null;
53 const cargo = readFileSync(cargoPath, "utf-8");
54 const m = cargo.match(/^version\s*=\s*"([^"]+)"/m);
55 return m ? m[1] : null;
56 }
57
58 function checkVersion() {
59 const version = deriveVersion();
60 return { version, ok: version != null };
61 }
62
63 /* ------------------------------------------------------------------ */
64 /* Check 3: command snippet freshness (install commands) */
65 /* ------------------------------------------------------------------ */
66
67 function checkInstallSnippets() {
68 const version = deriveVersion();
69 if (!version) return { ok: false, note: "could not derive version" };
70
71 // The page renders the verified document, including historical release
72 // examples; those versions must not be rewritten to the workspace candidate.
73 const src = readFileSync(resolve(REPO_ROOT, "docs/INSTALL.md"), "utf-8");
74 const guide = buildInstallGuide(src);
75 const stale = [];
76 function checkLinks(dir) {
77 for (const entry of readdirSync(dir, { withFileTypes: true })) {
78 const path = resolve(dir, entry.name);
79 if (entry.isDirectory()) checkLinks(path);
80 else if (/\.(tsx?|md|json)$/.test(entry.name) && !entry.name.endsWith(".test.ts")) {
81 for (const anchor of installAnchorErrors(readFileSync(path, "utf8"), guide.anchors)) {
82 stale.push({ found: anchor, expected: "an existing INSTALL.md anchor", context: path });
83 }
84 }
85 }
86 }
87 for (const dir of ["app", "components", "lib"]) checkLinks(resolve(WEB_DIR, dir));
88
89 // A clone without an explicit destination creates a directory whose name
90 // matches the repository slug exactly. Keep the following `cd` command
91 // case-correct so source installation works on case-sensitive filesystems.
92 const sourceCheckout = src.match(
93 /git clone[^\n]*https:\/\/github\.com\/codewhale-hq\/([^\s`]+)\s*\ncd\s+([^\s`]+)/,
94 );
95 const checkout = sourceCheckout
96 ? {
97 cloned: sourceCheckout[1].replace(/\.git$/, ""),
98 entered: sourceCheckout[2],
99 }
100 : null;
101 const checkoutOk = checkout !== null && checkout.cloned === checkout.entered;
102
103 return { ok: stale.length === 0 && checkoutOk, stale, checkout };
104 }
105
106 /* ------------------------------------------------------------------ */
107 /* Main */
108 /* ------------------------------------------------------------------ */
109
110 function main() {
111 const topics = readDocTopics(WEB_DIR);
112 if (topics.length === 0) {
113 console.error("[check-docs] ERROR: no topics parsed from docs-map.ts");
114 process.exit(1);
115 }
116 console.log(`[check-docs] parsed ${topics.length} doc topics`);
117
118 // Check 1: sources exist
119 const missingSources = checkSourcesExist(topics);
120 if (missingSources.length > 0) {
121 console.error("[check-docs] FAIL — missing repo source files:");
122 for (const m of missingSources) {
123 console.error(` ${m.topic}: ${m.source} → ${m.expected} (not found)`);
124 }
125 process.exit(1);
126 }
127 console.log("[check-docs] OK — all repo source files exist");
128
129 // Check 2: version
130 const ver = checkVersion();
131 if (!ver.ok) {
132 console.error("[check-docs] FAIL — could not derive version from workspace");
133 process.exit(1);
134 }
135 console.log(`[check-docs] OK — version=${ver.version}`);
136
137 // Check 3: install snippets
138 const install = checkInstallSnippets();
139 if (!install.ok && !install.note) {
140 if (install.stale.length > 0) {
141 console.error("[check-docs] FAIL — missing install-guide anchor:");
142 for (const s of install.stale) {
143 console.error(` found "${s.found}", expected "${s.expected}" in: ${s.context}`);
144 }
145 }
146 if (install.checkout === null) {
147 console.error("[check-docs] FAIL — source checkout clone/cd commands not found");
148 } else if (install.checkout.cloned !== install.checkout.entered) {
149 console.error(
150 `[check-docs] FAIL — source checkout clones "${install.checkout.cloned}" but enters "${install.checkout.entered}"`,
151 );
152 }
153 // #3770: a stale install snippet must fail the gate, not fall through to
154 // the final PASS. The same applies to source checkout copy drift.
155 process.exit(1);
156 }
157 console.log(`[check-docs] OK — install snippets${install.note ? ` (${install.note})` : ""}`);
158
159 console.log("[check-docs] PASS");
160 }
161
162 try {
163 main();
164 } catch (e) {
165 console.error("[check-docs] ERROR:", e.message);
166 process.exit(1);
167 }
168
168 lines Plain Text