#!/usr/bin/env node import { parseArgsOrFail as parseArgs } from '../../../../../lib/cli-args.js' import { validate } from './validate.js' import { execute } from './execute.js' import { executeEnvelope, failExecute, printEnvelope } from '../../../../../lib/output.js' const COMMAND = 'scaffold-migration' async function main(): Promise { const { values } = parseArgs({ options: { spec: { type: 'string' }, dry_run: { type: 'boolean', default: false } }, strict: true }) if (!values.spec) { printEnvelope(failExecute(COMMAND, ['--spec is required'])); process.exit(1) } let raw: unknown try { raw = JSON.parse(values.spec) } catch { printEnvelope(failExecute(COMMAND, ['Invalid JSON'])); process.exit(1) } const v = validate(raw) if (!v.valid || v.data === undefined) { printEnvelope(failExecute(COMMAND, v.errors)); process.exit(1) } // The PARSED spec — `context` defaults to ExtensionsDbContext only after // parse; on the raw object every caller omitting it ran // `dotnet ef … --context undefined`, and nothing documented the field. const r = await execute(v.data, { dryRun: values.dry_run === true }) if (values.dry_run) { printEnvelope(executeEnvelope(COMMAND, { data: { dryRun: true, command: r.command }, report: { output: '', sqlObjectsInlined: [] }, nextSteps: ['Re-run without --dry_run to generate the migration'] })) process.exit(0) } if (r.success) { printEnvelope(executeEnvelope(COMMAND, { report: { output: r.output, sqlObjectsInlined: r.sqlObjectsInlined }, nextSteps: ['Run dotnet build'] })) } else { printEnvelope({ ...failExecute(COMMAND, ['Migration failed']), report: { output: r.output }, nextSteps: ['Fix errors and retry'] }) } process.exit(r.success ? 0 : 1) } void main()