-
Notifications
You must be signed in to change notification settings - Fork 12.4k
Expand file tree
/
Copy pathdocs-stats.mjs
More file actions
187 lines (162 loc) · 8.14 KB
/
Copy pathdocs-stats.mjs
File metadata and controls
187 lines (162 loc) · 8.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env node
/**
* docs-stats — single source of truth for the capability counts quoted in docs.
*
* Default mode : recompute every stat from code and write docs/generated/stats.json.
* --check mode : recompute, then assert that every registered doc claim still
* matches the live number. Exits non-zero on drift (CI gate).
*
* Why this exists: capability counts (map layers, services, protos, locales,
* workflows, freshness sources, feeds) were hand-maintained across README,
* ARCHITECTURE.md, and docs/*.mdx and drifted independently. Every number a doc
* quotes must be derivable here and registered in CLAIMS below.
*
* Stats are parsed from source text (no TS execution / import-graph / env deps)
* so this runs anywhere Node runs, including bare CI.
*/
import { readFileSync, readdirSync, writeFileSync, mkdirSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
import { dirname, join } from 'node:path';
const ROOT = join(dirname(fileURLToPath(import.meta.url)), '..');
const read = (p) => readFileSync(join(ROOT, p), 'utf8');
const dirsIn = (p) =>
readdirSync(join(ROOT, p), { withFileTypes: true }).filter((e) => e.isDirectory()).map((e) => e.name);
const filesIn = (p) =>
readdirSync(join(ROOT, p), { withFileTypes: true }).filter((e) => e.isFile()).map((e) => e.name);
function walk(rel, out = []) {
for (const e of readdirSync(join(ROOT, rel), { withFileTypes: true })) {
const child = `${rel}/${e.name}`;
if (e.isDirectory()) walk(child, out);
else out.push(child);
}
return out;
}
function computeStats() {
// ---- Map layers (src/config/map-layer-definitions.ts) ----
const mld = read('src/config/map-layer-definitions.ts');
const registryBlock = mld.slice(mld.indexOf('LAYER_REGISTRY'), mld.indexOf('VARIANT_LAYER_ORDER'));
const layerDefinitions = (registryBlock.match(/^\s+\w+:\s+def\(/gm) || []).length;
const variantBlock = mld.slice(mld.indexOf('VARIANT_LAYER_ORDER'), mld.indexOf('export function getLayersForVariant'));
const variantLayers = {};
for (const m of variantBlock.matchAll(/(\w+):\s*\[([^\]]*)\]/g)) {
variantLayers[m[1]] = (m[2].match(/'[^']+'/g) || []).length;
}
// ---- Protos & services (proto/**) ----
const protoFiles = walk('proto').filter((f) => f.endsWith('.proto'));
const protoServices = protoFiles
.map((f) => (read(f).match(/^service\s+\w+/gm) || []).length)
.reduce((a, b) => a + b, 0);
const protoDomainFolders = dirsIn('proto/worldmonitor').length;
// ---- Generated OpenAPI service specs (docs/api/*Service.openapi.yaml) ----
const openapiServiceSpecs = filesIn('docs/api').filter((f) => /Service\.openapi\.yaml$/.test(f)).length;
// ---- Server domain handlers (server/worldmonitor/*/) ----
const serverDomains = dirsIn('server/worldmonitor').length;
// ---- User-facing locales (src/locales/*.json, excluding shell fragments) ----
const locales = filesIn('src/locales').filter((f) => f.endsWith('.json') && !f.endsWith('.shell.json')).length;
// ---- CI workflows (.github/workflows/*.yml) ----
const workflows = filesIn('.github/workflows').filter((f) => f.endsWith('.yml') || f.endsWith('.yaml')).sort();
// ---- Freshness-tracked sources (src/services/data-freshness.ts) ----
const dfs = read('src/services/data-freshness.ts');
const dfsStart = dfs.indexOf('const SOURCE_METADATA');
const dfsClass = dfs.indexOf('class ', dfsStart);
const metaBlock = dfs.slice(dfsStart, dfsClass >= 0 ? dfsClass : dfs.length);
const freshnessSources = (metaBlock.match(/^\s+\w+:\s*\{\s*name:/gm) || []).length;
const freshnessRequiredForRisk = (metaBlock.match(/requiredForRisk:\s*true/g) || []).length;
// ---- Feed definitions (src/config/feeds.ts) — floor metric ----
const feedDefinitions = (read('src/config/feeds.ts').match(/name:\s*'/g) || []).length;
return {
_generated: 'scripts/docs-stats.mjs — do not edit by hand; run `npm run docs:stats`',
layerDefinitions,
variantLayers,
protoFiles: protoFiles.length,
protoServices,
protoDomainFolders,
openapiServiceSpecs,
serverDomains,
locales,
workflows,
workflowCount: workflows.length,
freshnessSources,
freshnessRequiredForRisk,
feedDefinitions,
};
}
/**
* Registered doc claims. Each entry pins one number in one doc to a live stat.
* `value` returns the expected number; `min:true` treats the doc number as a
* floor (doc says "500+" → live must be >= 500). The regex must capture the
* number in group 1 and be unique enough to match the intended sentence.
*/
function claims(s) {
return [
{ file: 'README.md', re: /(\d+)\s+map layer types/, value: s.layerDefinitions },
{ file: 'README.md', re: /Protocol Buffers \((\d+)\s+protos/, value: s.protoFiles },
{ file: 'README.md', re: /(\d+)\s+services\)/, value: s.protoServices },
{ file: 'README.md', re: /(\d+)\s+languages/, value: s.locales },
{ file: 'README.md', re: /(\d+)\+\s+curated news feeds/, value: s.feedDefinitions, min: true },
{ file: 'docs/overview.mdx', re: /(\d+)\+\s+curated news feeds/, value: s.feedDefinitions, min: true },
{ file: 'docs/architecture.mdx', re: /(\d+)\s+service domains, and (?:\d+)\s+map layers/, value: s.protoServices },
{ file: 'docs/architecture.mdx', re: /(\d+)\s+map layers\./, value: s.layerDefinitions },
{ file: 'docs/architecture.mdx', re: /\*\*(\d+)\s+service domains\*\* cover/, value: s.protoServices },
{ file: 'docs/architecture.mdx', re: /All (\d+)\s+map layer toggle definitions/, value: s.layerDefinitions },
{ file: 'docs/map-engine.mdx', re: /\*\*(\d+)\s+data layers\*\*/, value: s.layerDefinitions },
{ file: 'docs/map-engine.mdx', re: /full \((\d+)\b/, value: s.variantLayers.full },
{ file: 'docs/map-engine.mdx', re: /tech \((\d+)\b/, value: s.variantLayers.tech },
{ file: 'docs/map-engine.mdx', re: /finance \((\d+)\b/, value: s.variantLayers.finance },
{ file: 'docs/map-engine.mdx', re: /happy \((\d+)\b/, value: s.variantLayers.happy },
{ file: 'docs/map-engine.mdx', re: /commodity \((\d+)\b/, value: s.variantLayers.commodity },
{ file: 'docs/map-engine.mdx', re: /energy \((\d+)\b/, value: s.variantLayers.energy },
{ file: 'docs/features.mdx', re: /(\d+)\s+data layers/, value: s.layerDefinitions },
{ file: 'docs/agent-discovery.mdx', re: /all (\d+)\s+services/, value: s.protoServices },
{ file: 'docs/api-reference.mdx', re: /all (\d+)\s+services/, value: s.protoServices },
{ file: 'docs/data-sources.mdx', re: /monitors (\d+)\s+data sources/, value: s.freshnessSources },
];
}
function main() {
const check = process.argv.includes('--check');
const stats = computeStats();
if (!check) {
mkdirSync(join(ROOT, 'docs/generated'), { recursive: true });
writeFileSync(join(ROOT, 'docs/generated/stats.json'), JSON.stringify(stats, null, 2) + '\n');
console.log('docs/generated/stats.json written:');
console.log(JSON.stringify(stats, null, 2));
return;
}
const failures = [];
// Every CI workflow must be documented in ARCHITECTURE.md's CI/CD table.
const arch = read('ARCHITECTURE.md');
for (const wf of stats.workflows) {
if (!arch.includes('`' + wf + '`')) {
failures.push(`ARCHITECTURE.md: CI workflow \`${wf}\` is not listed in the CI/CD table`);
}
}
for (const c of claims(stats)) {
let text;
try {
text = read(c.file);
} catch {
failures.push(`${c.file}: file not found`);
continue;
}
const m = text.match(c.re);
if (!m) {
failures.push(`${c.file}: claim pattern ${c.re} not found (expected ${c.value})`);
continue;
}
const found = Number(m[1]);
const ok = c.min ? found <= c.value : found === c.value;
if (!ok) {
failures.push(
`${c.file}: doc says ${found}, code says ${c.value}${c.min ? ' (floor)' : ''} — pattern ${c.re}`,
);
}
}
if (failures.length) {
console.error(`docs-stats --check FAILED (${failures.length}):`);
for (const f of failures) console.error(' ✗ ' + f);
console.error('\nFix the doc number, or run `npm run docs:stats` if the code total legitimately changed.');
process.exit(1);
}
console.log(`docs-stats --check OK — ${claims(stats).length} doc claims match code.`);
}
main();