| @@ -0,0 +1,283 @@ |
| 1 | +#!/usr/bin/env bun |
| 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 | +import { Glob } from 'bun' |
| 34 | + |
| 35 | +const ROOT = new URL('..', import.meta.url).pathname |
| 36 | + |
| 37 | + |
| 38 | +interface Problem { |
| 39 | + file: string |
| 40 | + line: number |
| 41 | + message: string |
| 42 | +} |
| 43 | + |
| 44 | + |
| 45 | +const GLOBAL_FLAGS = ['--config', '--help', '-h', '--version'] |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | +const PROSE_AFTER_BUDDY = new Set([ |
| 52 | + 'bot', 'is', 'can', 'will', 'handles', 'uses', 'scans', 'runs', 'does', |
| 53 | + 'and', 'or', 'to', 'the', 'a', 'as', 'in', 'on', 'for', 'with', 'from', |
| 54 | + 'creates', 'opens', 'posts', 'reads', 'writes', 'needs', 'supports', |
| 55 | +]) |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | +async function readCliSurface(): Promise<Map<string, Set<string>>> { |
| 66 | + const lines = (await Bun.file(`${ROOT}bin/cli.ts`).text()).split('\n') |
| 67 | + const commands = new Map<string, Set<string>>() |
| 68 | + const marks: Array<{ line: number, name: string }> = [] |
| 69 | + |
| 70 | + lines.forEach((line, i) => { |
| 71 | + const match = line.match(/^\s*\.command\((["'])(.+?)\1\s*,/) |
| 72 | + if (!match) |
| 73 | + return |
| 74 | + |
| 75 | + const name = match[2].split(' ')[0] |
| 76 | + marks.push({ line: i, name }) |
| 77 | + if (!commands.has(name)) |
| 78 | + commands.set(name, new Set(GLOBAL_FLAGS)) |
| 79 | + }) |
| 80 | + |
| 81 | + lines.forEach((line, i) => { |
| 82 | + const match = line.match(/^\s*\.option\((["'])(.+?)\1\s*,/) |
| 83 | + if (!match) |
| 84 | + return |
| 85 | + |
| 86 | + let owner: string | null = null |
| 87 | + for (const mark of marks) { |
| 88 | + if (mark.line < i) |
| 89 | + owner = mark.name |
| 90 | + } |
| 91 | + if (!owner) |
| 92 | + return |
| 93 | + |
| 94 | + |
| 95 | + for (const token of match[2].split(',').map(part => part.trim().split(/[ <[]/)[0]).filter(Boolean)) |
| 96 | + commands.get(owner)!.add(token) |
| 97 | + }) |
| 98 | + |
| 99 | + return commands |
| 100 | +} |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | +const CONFIG_TYPE_FILES = ['src/types.ts', 'src/rules/engine.ts'] |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | +async function readConfigKeys(): Promise<{ keys: Set<string>, openMaps: Set<string> }> { |
| 123 | + const keys = new Set<string>() |
| 124 | + const openMaps = new Set<string>() |
| 125 | + |
| 126 | + for (const file of CONFIG_TYPE_FILES) { |
| 127 | + const text = await Bun.file(`${ROOT}${file}`).text() |
| 128 | + |
| 129 | + for (const [, , key] of text.matchAll(/^\s*(?:readonly\s+)?(['"]?)([A-Za-z_$][\w$]*)\1\??\s*:/gm)) |
| 130 | + keys.add(key) |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | + for (const [, , key] of text.matchAll(/^\s*(['"]?)([A-Za-z_$][\w$]*)\1\??\s*:\s*Record</gm)) |
| 135 | + openMaps.add(key) |
| 136 | + } |
| 137 | + |
| 138 | + return { keys, openMaps } |
| 139 | +} |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | + |
| 147 | + |
| 148 | +function checkCliClaims(files: Array<{ path: string, text: string }>, commands: Map<string, Set<string>>): Problem[] { |
| 149 | + const problems: Problem[] = [] |
| 150 | + |
| 151 | + for (const { path, text } of files) { |
| 152 | + text.split('\n').forEach((line, i) => { |
| 153 | + |
| 154 | + |
| 155 | + const match = line.match(/(?:^|\$\s*|`)buddy\s+([a-z][a-z-]*)((?:\s+[^`\n]*)?)/) |
| 156 | + if (!match) |
| 157 | + return |
| 158 | + |
| 159 | + const [, name, rest = ''] = match |
| 160 | + const flags = commands.get(name) |
| 161 | + |
| 162 | + if (!flags) { |
| 163 | + if (!PROSE_AFTER_BUDDY.has(name)) |
| 164 | + problems.push({ file: path, line: i + 1, message: `unknown command: buddy ${name}` }) |
| 165 | + return |
| 166 | + } |
| 167 | + |
| 168 | + for (const [, flag] of rest.matchAll(/(?:^|\s)(--?[a-z][a-z-]*)/g)) { |
| 169 | + if (!flags.has(flag) && !flags.has(`--no-${flag.replace(/^--/, '')}`)) |
| 170 | + problems.push({ file: path, line: i + 1, message: `buddy ${name}: unknown flag ${flag}` }) |
| 171 | + } |
| 172 | + }) |
| 173 | + } |
| 174 | + |
| 175 | + return problems |
| 176 | +} |
| 177 | + |
| 178 | + |
| 179 | + |
| 180 | + |
| 181 | + |
| 182 | + |
| 183 | + |
| 184 | + |
| 185 | + |
| 186 | + |
| 187 | + |
| 188 | + |
| 189 | +function checkConfigClaims( |
| 190 | + files: Array<{ path: string, text: string }>, |
| 191 | + known: Set<string>, |
| 192 | + openMaps: Set<string>, |
| 193 | +): Problem[] { |
| 194 | + const problems: Problem[] = [] |
| 195 | + const configPages = /docs\/(config|api\/configuration)\.md$/ |
| 196 | + const codeLang = /^(ts|typescript|js|javascript|json)$/ |
| 197 | + |
| 198 | + for (const { path, text } of files) { |
| 199 | + const lines = text.split('\n') |
| 200 | + let fenceStart = -1 |
| 201 | + let fenceLang = '' |
| 202 | + |
| 203 | + lines.forEach((line, i) => { |
| 204 | + const fence = line.match(/^```(\w*)/) |
| 205 | + if (!fence) |
| 206 | + return |
| 207 | + |
| 208 | + if (fenceStart === -1) { |
| 209 | + fenceStart = i |
| 210 | + fenceLang = fence[1] |
| 211 | + return |
| 212 | + } |
| 213 | + |
| 214 | + const body = lines.slice(fenceStart + 1, i) |
| 215 | + const source = body.join('\n') |
| 216 | + |
| 217 | + |
| 218 | + const declaresTypes = /^\s*(?:export\s+)?(?:interface|type)\s/m.test(source) |
| 219 | + const isConfig = codeLang.test(fenceLang) |
| 220 | + && !declaresTypes |
| 221 | + && (/BuddyConfig|buddy\.config/.test(source) || configPages.test(path)) |
| 222 | + |
| 223 | + if (isConfig) { |
| 224 | + |
| 225 | + const stack: Array<{ indent: number, key: string }> = [] |
| 226 | + |
| 227 | + body.forEach((bodyLine, offset) => { |
| 228 | + const match = bodyLine.match(/^(\s*)(['"]?)([A-Za-z_$][\w$@/-]*)\2\s*:/) |
| 229 | + if (!match) |
| 230 | + return |
| 231 | + |
| 232 | + const [, indent, , key] = match |
| 233 | + while (stack.length > 0 && stack[stack.length - 1].indent >= indent.length) |
| 234 | + stack.pop() |
| 235 | + |
| 236 | + const insideOpenMap = stack.some(entry => openMaps.has(entry.key)) |
| 237 | + if (!insideOpenMap && !known.has(key)) |
| 238 | + problems.push({ file: path, line: fenceStart + offset + 2, message: `unknown config key: ${key}` }) |
| 239 | + |
| 240 | + if (bodyLine.trimEnd().endsWith('{')) |
| 241 | + stack.push({ indent: indent.length, key }) |
| 242 | + }) |
| 243 | + } |
| 244 | + |
| 245 | + fenceStart = -1 |
| 246 | + fenceLang = '' |
| 247 | + }) |
| 248 | + } |
| 249 | + |
| 250 | + return problems |
| 251 | +} |
| 252 | + |
| 253 | +const [commands, { keys: configKeys, openMaps }] = await Promise.all([readCliSurface(), readConfigKeys()]) |
| 254 | + |
| 255 | +const files: Array<{ path: string, text: string }> = [] |
| 256 | +for (const pattern of ['README.md', 'CLAUDE.md', 'docs/**/*.md']) { |
| 257 | + for await (const path of new Glob(pattern).scan(ROOT)) |
| 258 | + files.push({ path, text: await Bun.file(`${ROOT}${path}`).text() }) |
| 259 | +} |
| 260 | + |
| 261 | +const problems = [ |
| 262 | + ...checkCliClaims(files, commands), |
| 263 | + ...checkConfigClaims(files, configKeys, openMaps), |
| 264 | +] |
| 265 | + |
| 266 | +if (problems.length === 0) { |
| 267 | + console.log(`✓ ${files.length} pages agree with ${commands.size} commands and ${configKeys.size} config keys`) |
| 268 | + process.exit(0) |
| 269 | +} |
| 270 | + |
| 271 | +const byFile = new Map<string, Problem[]>() |
| 272 | +for (const problem of problems) |
| 273 | + byFile.set(problem.file, [...(byFile.get(problem.file) ?? []), problem]) |
| 274 | + |
| 275 | +for (const [file, found] of [...byFile].sort()) { |
| 276 | + console.error(`\n${file}`) |
| 277 | + for (const { line, message } of found) |
| 278 | + console.error(` ${line}: ${message}`) |
| 279 | +} |
| 280 | + |
| 281 | +console.error(`\n✗ ${problems.length} claim(s) in ${byFile.size} file(s) do not match the source.`) |
| 282 | +console.error(' Fix the documentation, or the code it describes — do not silence this check.') |
| 283 | +process.exit(1) |