298
* or throws on non-zero exit code or timeout.
299
*/
301
>
const command = resolveEffectiveCommand(hook, OS);
302
>
if (!command) {
303
return Promise.resolve('');
304
}
306
>
const timeout = (hook.timeout ?? 30) * 1000;
307
>
const cwd = hook.cwd?.fsPath;
308
>
309
>
return new Promise<string>((resolve, reject) => {
310
>
const isWindows = OS === OperatingSystem.Windows;
311
>
const shell = isWindows ? 'cmd.exe' : '/bin/sh';
312
>
const shellArgs = isWindows ? ['/c', command] : ['-c', command];
313
>
314
>
const child = spawn(shell, shellArgs, {
315
>
cwd,
316
>
env: { ...process.env, ...hook.env },
317
>
stdio: ['pipe', 'pipe', 'pipe'],
318
>
timeout,
319
>
});
320
>
321
>
let stdout = '';
322
>
let stderr = '';
323
>
324
>
child.stdout.on('data', (data: Buffer) => { stdout += data.toString(); });
325
>
child.stderr.on('data', (data: Buffer) => { stderr += data.toString(); });
326
>
327
>
if (stdin) {
328
>
child.stdin.write(stdin);
329
>
child.stdin.end();
330
>
} else {
331
child.stdin.end();
332
}
334
>
child.on('error', reject);
335
>
child.on('close', (code) => {
336
>
if (code === 0) {
337
resolve(stdout);
339
reject(new Error(`Hook command exited with code ${code}: ${stderr || stdout}`));
340
}
342
>
});
343
>
}
344
345
/**