|
| 1 | +#!/usr/bin/env bun |
| 2 | +import { readFile } from 'node:fs/promises'; |
| 3 | +import path from 'node:path'; |
| 4 | + |
| 5 | +const repoRoot = path.resolve(import.meta.dirname, '..'); |
| 6 | +const schemaPath = 'skills-data/agentv-eval-writer/references/eval.schema.json'; |
| 7 | +const schemaAbsolutePath = path.join(repoRoot, schemaPath); |
| 8 | + |
| 9 | +async function runOrExit(args: string[], options: { readonly cwd: string }): Promise<void> { |
| 10 | + const proc = Bun.spawn(args, { |
| 11 | + cwd: options.cwd, |
| 12 | + stdout: 'inherit', |
| 13 | + stderr: 'inherit', |
| 14 | + }); |
| 15 | + const exitCode = await proc.exited; |
| 16 | + if (exitCode !== 0) { |
| 17 | + process.exit(exitCode); |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +async function printSchemaDiff(): Promise<void> { |
| 22 | + await runOrExit(['git', 'diff', '--', schemaPath], { cwd: repoRoot }); |
| 23 | +} |
| 24 | + |
| 25 | +async function readSchema(): Promise<string | null> { |
| 26 | + try { |
| 27 | + return await readFile(schemaAbsolutePath, 'utf8'); |
| 28 | + } catch (error) { |
| 29 | + if (error instanceof Error && 'code' in error && error.code === 'ENOENT') { |
| 30 | + return null; |
| 31 | + } |
| 32 | + throw error; |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +const before = await readSchema(); |
| 37 | +await runOrExit([process.execPath, 'packages/core/scripts/generate-eval-schema.ts'], { |
| 38 | + cwd: repoRoot, |
| 39 | +}); |
| 40 | +const after = await readSchema(); |
| 41 | + |
| 42 | +if (before === after) { |
| 43 | + console.log(`OK: ${schemaPath} is generated from the Zod source`); |
| 44 | + process.exit(0); |
| 45 | +} |
| 46 | + |
| 47 | +const message = |
| 48 | + 'Generated eval schema is out of sync. This command regenerated eval.schema.json from packages/core/src/evaluation/validation/eval-file.schema.ts; review and commit the generated diff instead of editing the JSON manually.'; |
| 49 | +if (process.env.GITHUB_ACTIONS === 'true') { |
| 50 | + console.error(`::error file=${schemaPath}::${message}`); |
| 51 | +} |
| 52 | +console.error(`[schema] ERROR: ${message}`); |
| 53 | +console.error(' Run locally: bun run validate:eval-schema'); |
| 54 | +console.error(` Generated file: ${schemaPath}`); |
| 55 | +await printSchemaDiff(); |
| 56 | +process.exit(1); |
0 commit comments