| 54 | } |
| 55 | |
| 56 | function failWithError( |
| 57 | error: unknown, |
| 58 | json?: { enabled: boolean | undefined; payload?: Record<string, unknown>; fallbackCode?: string } |
| 59 | ): void { |
| 60 | // The agent contract: every --json failure leaves exactly one JSON |
| 61 | // document on stdout (the command's null-shape plus a status array). |
| 62 | if (json?.enabled) { |
| 63 | console.log( |
| 64 | JSON.stringify( |
| 65 | { ...(json.payload ?? {}), status: [asStatus(error, json.fallbackCode ?? 'command_error')] }, |
| 66 | null, |
| 67 | 2 |
| 68 | ) |
| 69 | ); |
| 70 | process.exitCode = 1; |
| 71 | return; |
| 72 | } |
| 73 | ora().fail(`Error: ${(error as Error).message}`); |
| 74 | // Resolution and store errors carry a pasteable fix - never drop it. |
| 75 | const fix = (error as { diagnostic?: { fix?: string } }).diagnostic?.fix; |
| 76 | if (fix) { |
| 77 | console.error(`Fix: ${fix}`); |
| 78 | } |
| 79 | process.exitCode = process.exitCode ?? 1; |
| 80 | } |
| 81 | |
| 82 | const program = new Command(); |
| 83 | const require = createRequire(import.meta.url); |