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