|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import { spawnSync } from 'node:child_process'; |
| 4 | +import { existsSync, readdirSync, readFileSync } from 'node:fs'; |
| 5 | +import path from 'node:path'; |
| 6 | +import { fileURLToPath } from 'node:url'; |
| 7 | + |
| 8 | +const scriptDir = path.dirname(fileURLToPath(import.meta.url)); |
| 9 | +const repoRoot = path.resolve(scriptDir, '..'); |
| 10 | +const cliPath = path.join(repoRoot, 'build', 'cli.js'); |
| 11 | + |
| 12 | +function fail(message, detail) { |
| 13 | + console.error(`\n❌ ${message}`); |
| 14 | + if (detail) { |
| 15 | + console.error(detail); |
| 16 | + } |
| 17 | + process.exit(1); |
| 18 | +} |
| 19 | + |
| 20 | +function loadToolCatalog() { |
| 21 | + if (!existsSync(cliPath)) { |
| 22 | + fail( |
| 23 | + 'Missing build artifact: build/cli.js', |
| 24 | + 'Run `npm run build:tsup` before `npm run docs:check`.', |
| 25 | + ); |
| 26 | + } |
| 27 | + |
| 28 | + const result = spawnSync(process.execPath, [cliPath, 'tools', '--json'], { |
| 29 | + cwd: repoRoot, |
| 30 | + encoding: 'utf8', |
| 31 | + }); |
| 32 | + |
| 33 | + if (result.status !== 0) { |
| 34 | + fail('Failed to load CLI tool catalog from build artifact.', result.stderr || result.stdout); |
| 35 | + } |
| 36 | + |
| 37 | + try { |
| 38 | + return JSON.parse(result.stdout); |
| 39 | + } catch (error) { |
| 40 | + const message = error instanceof Error ? error.message : 'Unknown JSON parse error'; |
| 41 | + fail('Could not parse JSON from `node build/cli.js tools --json`.', message); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +function getConsumerDocs() { |
| 46 | + const docsDir = path.join(repoRoot, 'docs'); |
| 47 | + const docsFiles = readdirSync(docsDir, { withFileTypes: true }) |
| 48 | + .filter((entry) => entry.isFile() && entry.name.endsWith('.md')) |
| 49 | + .map((entry) => path.join(docsDir, entry.name)) |
| 50 | + .sort(); |
| 51 | + |
| 52 | + return [path.join(repoRoot, 'README.md'), path.join(repoRoot, 'CHANGELOG.md'), ...docsFiles]; |
| 53 | +} |
| 54 | + |
| 55 | +function buildValidationSets(catalog) { |
| 56 | + const validPairs = new Set(); |
| 57 | + const validWorkflows = new Set(); |
| 58 | + |
| 59 | + if (!Array.isArray(catalog.workflows)) { |
| 60 | + fail('Tool catalog does not contain a workflows array.'); |
| 61 | + } |
| 62 | + |
| 63 | + for (const workflow of catalog.workflows) { |
| 64 | + if (!workflow || typeof workflow.workflow !== 'string') { |
| 65 | + continue; |
| 66 | + } |
| 67 | + |
| 68 | + validWorkflows.add(workflow.workflow); |
| 69 | + |
| 70 | + if (!Array.isArray(workflow.tools)) { |
| 71 | + continue; |
| 72 | + } |
| 73 | + |
| 74 | + for (const tool of workflow.tools) { |
| 75 | + if (tool && typeof tool.name === 'string') { |
| 76 | + validPairs.add(`${workflow.workflow} ${tool.name}`); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + return { validPairs, validWorkflows }; |
| 82 | +} |
| 83 | + |
| 84 | +function findInvalidCommands(files, validPairs, validWorkflows) { |
| 85 | + const validTopLevel = new Set(['mcp', 'tools', 'daemon']); |
| 86 | + const validDaemonActions = new Set(['status', 'start', 'stop', 'restart', 'list']); |
| 87 | + const findings = []; |
| 88 | + |
| 89 | + const commandRegex = |
| 90 | + /(?:^|[^a-z0-9-])xcodebuildmcp(?!-)\s+([a-z][a-z0-9-]*)(?:\s+([a-z][a-z0-9-]*))?/g; |
| 91 | + |
| 92 | + for (const absoluteFilePath of files) { |
| 93 | + const relativePath = path.relative(repoRoot, absoluteFilePath) || absoluteFilePath; |
| 94 | + const content = readFileSync(absoluteFilePath, 'utf8'); |
| 95 | + const lines = content.split(/\r?\n/u); |
| 96 | + |
| 97 | + for (let lineNumber = 1; lineNumber <= lines.length; lineNumber += 1) { |
| 98 | + const line = lines[lineNumber - 1]; |
| 99 | + commandRegex.lastIndex = 0; |
| 100 | + let match = commandRegex.exec(line); |
| 101 | + |
| 102 | + while (match) { |
| 103 | + const first = match[1]; |
| 104 | + const second = match[2]; |
| 105 | + const command = second ? `${first} ${second}` : first; |
| 106 | + |
| 107 | + let valid = false; |
| 108 | + |
| 109 | + if (!second) { |
| 110 | + valid = validTopLevel.has(first) || validWorkflows.has(first); |
| 111 | + } else if (first === 'daemon') { |
| 112 | + valid = validDaemonActions.has(second); |
| 113 | + } else { |
| 114 | + valid = validPairs.has(command); |
| 115 | + } |
| 116 | + |
| 117 | + if (!valid) { |
| 118 | + findings.push(`${relativePath}:${lineNumber}: ${command}`); |
| 119 | + } |
| 120 | + |
| 121 | + match = commandRegex.exec(line); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + return findings; |
| 127 | +} |
| 128 | + |
| 129 | +function main() { |
| 130 | + const catalog = loadToolCatalog(); |
| 131 | + const files = getConsumerDocs(); |
| 132 | + const { validPairs, validWorkflows } = buildValidationSets(catalog); |
| 133 | + const findings = findInvalidCommands(files, validPairs, validWorkflows); |
| 134 | + |
| 135 | + if (findings.length > 0) { |
| 136 | + fail( |
| 137 | + 'Found invalid CLI command references in consumer docs.', |
| 138 | + `${findings.join('\n')}\n\nRun \`node build/cli.js tools\` to inspect valid commands.`, |
| 139 | + ); |
| 140 | + } |
| 141 | + |
| 142 | + console.log('✅ Docs CLI command check passed (README.md + CHANGELOG.md + docs/*.md).'); |
| 143 | +} |
| 144 | + |
| 145 | +main(); |
0 commit comments