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