|
| 1 | +/// <reference types="node" /> |
| 2 | +import { spawnSync } from "node:child_process"; |
| 3 | + |
| 4 | +type StopHookInput = { |
| 5 | + status?: "completed" | "aborted" | "error"; |
| 6 | +}; |
| 7 | + |
| 8 | +const COMMAND = { |
| 9 | + tool: "phpstan", |
| 10 | + cmd: "./vendor/bin/phpstan analyse --memory-limit=512M", |
| 11 | +}; |
| 12 | + |
| 13 | +async function parseInput(): Promise<StopHookInput> { |
| 14 | + const chunks: Buffer[] = []; |
| 15 | + for await (const chunk of process.stdin) { |
| 16 | + chunks.push(typeof chunk === "string" ? Buffer.from(chunk) : chunk); |
| 17 | + } |
| 18 | + const text = Buffer.concat(chunks).toString("utf8"); |
| 19 | + try { |
| 20 | + return JSON.parse(text) as StopHookInput; |
| 21 | + } catch { |
| 22 | + return {}; |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +function hasGitChanges(): boolean { |
| 27 | + const result = spawnSync("git", [ |
| 28 | + "status", |
| 29 | + "--porcelain", |
| 30 | + "--untracked-files", |
| 31 | + ]); |
| 32 | + return result.stdout.toString().trim().length > 0; |
| 33 | +} |
| 34 | + |
| 35 | +function runCommand( |
| 36 | + _name: string, |
| 37 | + cmd: string, |
| 38 | +): { ok: boolean; output: string } { |
| 39 | + const result = spawnSync(cmd, { |
| 40 | + stdio: ["inherit", "pipe", "pipe"], |
| 41 | + shell: true, |
| 42 | + }); |
| 43 | + const out = result.stdout?.toString() ?? ""; |
| 44 | + const err = result.stderr?.toString() ?? ""; |
| 45 | + const output = [out, err].filter(Boolean).join("\n").trim(); |
| 46 | + if (output) process.stderr.write(output); |
| 47 | + return { ok: result.status === 0, output }; |
| 48 | +} |
| 49 | + |
| 50 | +async function main(): Promise<void> { |
| 51 | + const input = await parseInput(); |
| 52 | + |
| 53 | + if (input.status !== "completed") { |
| 54 | + process.stdout.write(JSON.stringify({}) + "\n"); |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + if (!hasGitChanges()) { |
| 59 | + process.stdout.write(JSON.stringify({}) + "\n"); |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + const MAX_ERROR_CHARS = 1000; |
| 64 | + |
| 65 | + const { ok, output } = runCommand(COMMAND.tool, COMMAND.cmd); |
| 66 | + const failures = ok ? [] : [{ tool: COMMAND.tool, output }]; |
| 67 | + |
| 68 | + const truncatedOutput = |
| 69 | + output && output.length > MAX_ERROR_CHARS |
| 70 | + ? output.slice(0, MAX_ERROR_CHARS) + "\n… (truncated)" |
| 71 | + : output || "(no output)"; |
| 72 | + |
| 73 | + const result = |
| 74 | + failures.length > 0 |
| 75 | + ? { |
| 76 | + followup_message: [ |
| 77 | + `Please fix the errors reported by ${COMMAND.tool}.`, |
| 78 | + "Don't do any other investigation.", |
| 79 | + "", |
| 80 | + `<${COMMAND.tool}-errors>\n${truncatedOutput}\n</${COMMAND.tool}-errors>`, |
| 81 | + ].join("\n"), |
| 82 | + } |
| 83 | + : {}; |
| 84 | + process.stdout.write(JSON.stringify(result) + "\n"); |
| 85 | +} |
| 86 | + |
| 87 | +main().catch((err) => { |
| 88 | + console.error("[run-phpstan-on-stop]", err); |
| 89 | + process.stdout.write(JSON.stringify({}) + "\n"); |
| 90 | +}); |
0 commit comments