( handler: CommandHookHandler, input: HookInput, cwd: string, )
| 30 | } |
| 31 | |
| 32 | function executeCommandHook( |
| 33 | handler: CommandHookHandler, |
| 34 | input: HookInput, |
| 35 | cwd: string, |
| 36 | ): Promise<HookExecutionResult> { |
| 37 | const timeoutMs = (handler.timeout ?? DEFAULT_COMMAND_TIMEOUT_SECONDS) * 1000; |
| 38 | |
| 39 | return new Promise((resolve) => { |
| 40 | const env = { |
| 41 | ...process.env, |
| 42 | CLAUDE_PROJECT_DIR: cwd, |
| 43 | CONTINUE_PROJECT_DIR: cwd, |
| 44 | }; |
| 45 | |
| 46 | // Use shell to execute the command (matches Claude Code behavior) |
| 47 | const shell = process.platform === "win32" ? "cmd.exe" : "/bin/sh"; |
| 48 | const shellArgs = |
| 49 | process.platform === "win32" |
| 50 | ? ["/c", handler.command] |
| 51 | : ["-c", handler.command]; |
| 52 | |
| 53 | const child = execFile(shell, shellArgs, { |
| 54 | cwd, |
| 55 | env, |
| 56 | timeout: timeoutMs, |
| 57 | maxBuffer: 10 * 1024 * 1024, // 10MB |
| 58 | }); |
| 59 | |
| 60 | let stdout = ""; |
| 61 | let stderr = ""; |
| 62 | |
| 63 | child.stdout?.setEncoding("utf8"); |
| 64 | child.stdout?.on("data", (data: string) => { |
| 65 | stdout += data; |
| 66 | }); |
| 67 | |
| 68 | child.stderr?.setEncoding("utf8"); |
| 69 | child.stderr?.on("data", (data: string) => { |
| 70 | stderr += data; |
| 71 | }); |
| 72 | |
| 73 | // Send JSON input on stdin |
| 74 | if (child.stdin) { |
| 75 | child.stdin.on("error", (error: NodeJS.ErrnoException) => { |
| 76 | if (error.code !== "EPIPE") { |
| 77 | logger.warn( |
| 78 | `Failed writing hook input to stdin: ${handler.command}`, |
| 79 | error, |
| 80 | ); |
| 81 | } |
| 82 | }); |
| 83 | child.stdin.write(JSON.stringify(input)); |
| 84 | child.stdin.end(); |
| 85 | } |
| 86 | |
| 87 | child.on("close", (code) => { |
| 88 | const exitCode = code ?? 0; |
| 89 | const output = tryParseJson(stdout); |
no test coverage detected