|
| 1 | +#!/usr/bin/env bun |
| 2 | +/** |
| 3 | + * Runs every self-contained, repo-local generator, so a contributor (or `/ship`) can bring all |
| 4 | + * committed artifacts back in sync with one command: `bun run generate`. |
| 5 | + * |
| 6 | + * The list is derived from package.json the way `run-audits.ts` derives its audits, so a new |
| 7 | + * generator is picked up by default rather than added to a list here. A generator qualifies when |
| 8 | + * it has a matching audit: every `generate:<x>` that has a `check:<x>`, plus the older names in |
| 9 | + * {@link ALIASED_PAIRS}. That pairing is the repo's convention for "this artifact is a pure |
| 10 | + * function of the tree" (`check:audits` runs the check), so it is also what makes a generator safe |
| 11 | + * to run unprompted. Name a new generator `generate:<x>` / `check:<x>` and it runs here. |
| 12 | + * |
| 13 | + * Deliberately not run: the `<x>:generate` / `<x>:check` suffix pairs (`mship*`, `trace-*`, |
| 14 | + * `metrics`, `vfs`, `billing-protocol`, `images`, `library:covers`), which need the sibling |
| 15 | + * copilot repo, a Helm render, or a per-machine renderer (see `run-audits.ts`); |
| 16 | + * `generate:pi-model-catalog` under `apps/sim`, which reads an installed package; and |
| 17 | + * `scripts/generate-docs.ts`, whose broad integration-docs rewrite has no `generate:` script. |
| 18 | + * |
| 19 | + * Generators run one at a time, in package.json order, because they are not independent: some |
| 20 | + * read what an earlier one wrote. `generate:cli-docs` reads the operation table |
| 21 | + * `generate:cli-api` emits, `generate:docs-manifest` lists the pages `generate:cli-docs` writes, |
| 22 | + * and `generate:sdk-fixture-catalog` projects through `generate:tool-metadata` and |
| 23 | + * `generate:sdk-names` output. Keep a new pair below the generators it reads. |
| 24 | + * |
| 25 | + * Exits non-zero when any generator fails; the rest still run so one failure does not hide another. |
| 26 | + * |
| 27 | + * Usage: |
| 28 | + * bun run generate |
| 29 | + */ |
| 30 | +import path from 'node:path' |
| 31 | + |
| 32 | +/** Generators whose audit predates the `generate:<x>` / `check:<x>` naming, keyed to that audit. */ |
| 33 | +const ALIASED_PAIRS: Record<string, string> = { |
| 34 | + 'skills:sync': 'check:skills', |
| 35 | +} |
| 36 | + |
| 37 | +const ROOT = path.resolve(import.meta.dir, '..') |
| 38 | + |
| 39 | +interface GeneratorResult { |
| 40 | + script: string |
| 41 | + ok: boolean |
| 42 | + durationMs: number |
| 43 | +} |
| 44 | + |
| 45 | +function generatorScripts(scripts: Record<string, string>): string[] { |
| 46 | + return Object.keys(scripts).filter((name) => { |
| 47 | + if (name.startsWith('generate:')) return `check:${name.slice('generate:'.length)}` in scripts |
| 48 | + const audit = ALIASED_PAIRS[name] |
| 49 | + return audit !== undefined && audit in scripts |
| 50 | + }) |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * Runs one generator with inherited output. |
| 55 | + * |
| 56 | + * Spawns the script directly rather than `bun run <name>`, which would start a bun process only |
| 57 | + * to have it read package.json and start a second one. |
| 58 | + */ |
| 59 | +async function runGenerator(script: string, command: string): Promise<GeneratorResult> { |
| 60 | + const startedAt = performance.now() |
| 61 | + const argv = command.replace(/^bun run /, '').split(/\s+/) |
| 62 | + const proc = Bun.spawn([process.execPath, ...argv], { |
| 63 | + cwd: ROOT, |
| 64 | + stdout: 'inherit', |
| 65 | + stderr: 'inherit', |
| 66 | + }) |
| 67 | + const exitCode = await proc.exited |
| 68 | + return { script, ok: exitCode === 0, durationMs: performance.now() - startedAt } |
| 69 | +} |
| 70 | + |
| 71 | +const manifest = await Bun.file(path.join(ROOT, 'package.json')).json() |
| 72 | +const commands = manifest.scripts as Record<string, string> |
| 73 | +const generators = generatorScripts(commands) |
| 74 | +const results: GeneratorResult[] = [] |
| 75 | + |
| 76 | +for (const script of generators) { |
| 77 | + console.log(`\n▶ ${script}`) |
| 78 | + results.push(await runGenerator(script, commands[script])) |
| 79 | +} |
| 80 | + |
| 81 | +console.log(`\n${results.length} generators:`) |
| 82 | +for (const result of results) { |
| 83 | + console.log(`${result.ok ? '✓' : '✗'} ${result.script} (${Math.round(result.durationMs)}ms)`) |
| 84 | +} |
| 85 | + |
| 86 | +const failures = results.filter((result) => !result.ok) |
| 87 | +if (failures.length > 0) { |
| 88 | + console.error( |
| 89 | + `\n${failures.length} generator(s) failed: ${failures.map((f) => f.script).join(', ')}. Their output is above; fix the cause and rerun \`bun run generate\`.` |
| 90 | + ) |
| 91 | + process.exit(1) |
| 92 | +} |
0 commit comments