( command: string, input: Record<string, unknown>, options: RunHookOptions, )
| 40 | }); |
| 41 | |
| 42 | export async function runHook( |
| 43 | command: string, |
| 44 | input: Record<string, unknown>, |
| 45 | options: RunHookOptions, |
| 46 | ): Promise<HookResult> { |
| 47 | let child: ChildProcessWithoutNullStreams; |
| 48 | try { |
| 49 | child = spawn(command, { |
| 50 | shell: true, |
| 51 | cwd: options.cwd, |
| 52 | stdio: 'pipe', |
| 53 | detached: process.platform !== 'win32', |
| 54 | env: options.env ? { ...process.env, ...options.env } : undefined, |
| 55 | }); |
| 56 | } catch (error) { |
| 57 | return allowResult({ stderr: errorMessage(error) }); |
| 58 | } |
| 59 | |
| 60 | return new Promise<HookResult>((resolve) => { |
| 61 | let stdout = ''; |
| 62 | let stderr = ''; |
| 63 | let settled = false; |
| 64 | const timeoutMs = timeoutSeconds(options.timeout) * 1000; |
| 65 | |
| 66 | const cleanup = () => { |
| 67 | clearTimeout(timeout); |
| 68 | options.signal?.removeEventListener('abort', onAbort); |
| 69 | }; |
| 70 | |
| 71 | const settle = (result: HookResult): void => { |
| 72 | if (settled) return; |
| 73 | settled = true; |
| 74 | cleanup(); |
| 75 | resolve(result); |
| 76 | }; |
| 77 | |
| 78 | const timeout = setTimeout(() => { |
| 79 | killProcess(child); |
| 80 | settle(allowResult({ stdout, stderr, timedOut: true })); |
| 81 | }, timeoutMs); |
| 82 | |
| 83 | const onAbort = (): void => { |
| 84 | killProcess(child); |
| 85 | settle(allowResult({ stdout, stderr })); |
| 86 | }; |
| 87 | |
| 88 | options.signal?.addEventListener('abort', onAbort, { once: true }); |
| 89 | if (options.signal?.aborted === true) { |
| 90 | onAbort(); |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | child.stdout.setEncoding('utf8'); |
| 95 | child.stderr.setEncoding('utf8'); |
| 96 | child.stdout.on('data', (chunk: string) => { |
| 97 | stdout += chunk; |
| 98 | }); |
| 99 | child.stderr.on('data', (chunk: string) => { |
no test coverage detected