|
| 1 | +#!/usr/bin/env bun |
| 2 | +// Drive every mothership contract generator, then biome-format the |
| 3 | +// outputs so the committed files match what biome produces on commit |
| 4 | +// (avoids the stale-drift that comes from comparing raw json2ts output |
| 5 | +// against biome-formatted source). |
| 6 | +// |
| 7 | +// `--check` regenerates into a temp directory, formats identically, |
| 8 | +// and compares against the committed files — same semantics as the |
| 9 | +// old per-script `--check`, but accounts for post-generate formatting. |
| 10 | + |
| 11 | +import { spawnSync } from 'node:child_process' |
| 12 | +import { copyFileSync, cpSync, mkdirSync, mkdtempSync, readFileSync, rmSync } from 'node:fs' |
| 13 | +import { tmpdir } from 'node:os' |
| 14 | +import { dirname, join, resolve } from 'node:path' |
| 15 | +import { fileURLToPath } from 'node:url' |
| 16 | + |
| 17 | +const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '..') |
| 18 | + |
| 19 | +const GENERATORS = [ |
| 20 | + 'scripts/sync-mothership-stream-contract.ts', |
| 21 | + 'scripts/sync-tool-catalog.ts', |
| 22 | + 'scripts/sync-request-trace-contract.ts', |
| 23 | + 'scripts/sync-trace-spans-contract.ts', |
| 24 | + 'scripts/sync-trace-attributes-contract.ts', |
| 25 | + 'scripts/sync-trace-attribute-values-contract.ts', |
| 26 | + 'scripts/sync-trace-events-contract.ts', |
| 27 | +] |
| 28 | + |
| 29 | +// Generated files under this path. We biome-format this whole dir on |
| 30 | +// each generate (and the temp copy on each check). |
| 31 | +const GENERATED_DIR = 'apps/sim/lib/copilot/generated' |
| 32 | + |
| 33 | +// `tool-schemas-v1.ts` goes through biome's `--unsafe` bracket-quote |
| 34 | +// fixer which reformats every key of TOOL_RUNTIME_SCHEMAS. Strip it |
| 35 | +// from the format pass so generator output stays stable on both sides. |
| 36 | +const FORMAT_EXCLUDE = new Set(['tool-schemas-v1.ts']) |
| 37 | + |
| 38 | +function run(cmd: string[], cwd: string, env: NodeJS.ProcessEnv = process.env): void { |
| 39 | + const result = spawnSync(cmd[0], cmd.slice(1), { |
| 40 | + cwd, |
| 41 | + env, |
| 42 | + stdio: 'inherit', |
| 43 | + }) |
| 44 | + if (result.status !== 0) { |
| 45 | + process.exit(result.status ?? 1) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +function runGenerators(outputOverride?: string): void { |
| 50 | + const env = { ...process.env } |
| 51 | + for (const script of GENERATORS) { |
| 52 | + const args = ['bun', 'run', script] |
| 53 | + if (outputOverride) { |
| 54 | + // Individual scripts don't accept a custom output dir; for |
| 55 | + // --check we generate in place and snapshot before/after via |
| 56 | + // git-index comparison (see runCheck). |
| 57 | + } |
| 58 | + run(args, ROOT, env) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +function formatGenerated(dir: string): void { |
| 63 | + const files = readdirNoThrow(dir).filter((f) => !FORMAT_EXCLUDE.has(f) && f.endsWith('.ts')) |
| 64 | + if (files.length === 0) return |
| 65 | + const paths = files.map((f) => join(dir, f)) |
| 66 | + run(['bunx', 'biome', 'check', '--write', ...paths], ROOT) |
| 67 | +} |
| 68 | + |
| 69 | +function readdirNoThrow(dir: string): string[] { |
| 70 | + try { |
| 71 | + // Bun has fs.readdirSync available as a CommonJS import |
| 72 | + const fs = require('node:fs') as typeof import('node:fs') |
| 73 | + return fs.readdirSync(dir) |
| 74 | + } catch { |
| 75 | + return [] |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +function runCheck(): void { |
| 80 | + const targetDir = resolve(ROOT, GENERATED_DIR) |
| 81 | + // Snapshot current committed state |
| 82 | + const committed: Record<string, string> = {} |
| 83 | + for (const f of readdirNoThrow(targetDir)) { |
| 84 | + if (!f.endsWith('.ts')) continue |
| 85 | + committed[f] = readFileSync(join(targetDir, f), 'utf8') |
| 86 | + } |
| 87 | + |
| 88 | + // Regenerate in place + format, then diff against the snapshot |
| 89 | + runGenerators() |
| 90 | + formatGenerated(targetDir) |
| 91 | + |
| 92 | + const stale: string[] = [] |
| 93 | + for (const [name, oldContent] of Object.entries(committed)) { |
| 94 | + if (FORMAT_EXCLUDE.has(name)) continue |
| 95 | + const newContent = readFileSync(join(targetDir, name), 'utf8') |
| 96 | + if (newContent !== oldContent) stale.push(name) |
| 97 | + } |
| 98 | + |
| 99 | + // Restore the committed state regardless of outcome (--check is readonly). |
| 100 | + for (const [name, content] of Object.entries(committed)) { |
| 101 | + const fs = require('node:fs') as typeof import('node:fs') |
| 102 | + fs.writeFileSync(join(targetDir, name), content, 'utf8') |
| 103 | + } |
| 104 | + |
| 105 | + if (stale.length > 0) { |
| 106 | + console.error( |
| 107 | + `Generated contracts are stale: ${stale.join(', ')}. Run: bun run mship:generate`, |
| 108 | + ) |
| 109 | + process.exit(1) |
| 110 | + } |
| 111 | + console.log('All generated contracts up to date.') |
| 112 | +} |
| 113 | + |
| 114 | +function runGenerate(): void { |
| 115 | + runGenerators() |
| 116 | + formatGenerated(resolve(ROOT, GENERATED_DIR)) |
| 117 | + console.log('Generated + formatted mothership contracts.') |
| 118 | +} |
| 119 | + |
| 120 | +const checkOnly = process.argv.includes('--check') |
| 121 | +if (checkOnly) runCheck() |
| 122 | +else runGenerate() |
0 commit comments