|
| 1 | +/// <reference types="bun-types-no-globals/lib/index.d.ts" /> |
| 2 | + |
| 3 | +import { stdin } from 'bun'; |
| 4 | + |
| 5 | +const MAX_OUTPUT_CHARS = 8000; |
| 6 | + |
| 7 | +interface StopHookInput { |
| 8 | + status: string; |
| 9 | + loop_count: number; |
| 10 | +} |
| 11 | + |
| 12 | +async function main(): Promise<number> { |
| 13 | + try { |
| 14 | + const input = JSON.parse(await stdin.text()) as StopHookInput; |
| 15 | + |
| 16 | + if (input.status !== 'completed') { |
| 17 | + console.log(JSON.stringify({})); |
| 18 | + return 0; |
| 19 | + } |
| 20 | + |
| 21 | + const proc = Bun.spawn(['bun', 'vet'], { |
| 22 | + stdout: 'pipe', |
| 23 | + stderr: 'pipe', |
| 24 | + }); |
| 25 | + |
| 26 | + const [stdout, stderr, exitCode] = await Promise.all([ |
| 27 | + new Response(proc.stdout).text(), |
| 28 | + new Response(proc.stderr).text(), |
| 29 | + proc.exited, |
| 30 | + ]); |
| 31 | + |
| 32 | + if (exitCode === 0) { |
| 33 | + console.log(JSON.stringify({})); |
| 34 | + return 0; |
| 35 | + } |
| 36 | + |
| 37 | + const combined = [stdout, stderr] |
| 38 | + .filter((chunk) => chunk.length > 0) |
| 39 | + .join('\n') |
| 40 | + .trim(); |
| 41 | + const output = |
| 42 | + combined.length > MAX_OUTPUT_CHARS |
| 43 | + ? combined.slice(-MAX_OUTPUT_CHARS) |
| 44 | + : combined; |
| 45 | + |
| 46 | + console.log( |
| 47 | + JSON.stringify({ |
| 48 | + followup_message: [ |
| 49 | + 'Quality gate failed: `bun vet` must exit 0 before this session can end.', |
| 50 | + 'Fix every error and warning, then run `bun vet` again.', |
| 51 | + output.length > 0 ? `\n\`\`\`\n${output}\n\`\`\`` : '', |
| 52 | + ].join(' '), |
| 53 | + }), |
| 54 | + ); |
| 55 | + return 0; |
| 56 | + } catch (error) { |
| 57 | + console.error('[vet-stop] failed', error); |
| 58 | + console.log( |
| 59 | + JSON.stringify({ |
| 60 | + followup_message: |
| 61 | + 'Quality gate error: the vet stop hook crashed. Run `bun vet` manually, fix failures, and retry.', |
| 62 | + }), |
| 63 | + ); |
| 64 | + return 0; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +const exitCode = await main(); |
| 69 | +process.exit(exitCode); |
0 commit comments