2023-08-14 19:23:14 +00:00
|
|
|
// This is an extremely simplified version of [`execa`](https://github.com/sindresorhus/execa)
|
|
|
|
// intended to keep our dependency size down
|
|
|
|
import type { StdioOptions } from 'node:child_process';
|
|
|
|
import type { Readable } from 'node:stream';
|
|
|
|
|
|
|
|
import { spawn } from 'node:child_process';
|
2023-08-14 19:25:29 +00:00
|
|
|
import { text as textFromStream } from 'node:stream/consumers';
|
2023-08-14 19:23:14 +00:00
|
|
|
import { setTimeout as sleep } from 'node:timers/promises';
|
|
|
|
|
|
|
|
export interface ExecaOptions {
|
2023-08-14 19:25:29 +00:00
|
|
|
cwd?: string | URL;
|
|
|
|
stdio?: StdioOptions;
|
|
|
|
timeout?: number;
|
2023-08-14 19:23:14 +00:00
|
|
|
}
|
|
|
|
export interface Output {
|
2023-08-14 19:25:29 +00:00
|
|
|
stdout: string;
|
|
|
|
stderr: string;
|
|
|
|
exitCode: number;
|
2023-08-14 19:23:14 +00:00
|
|
|
}
|
2023-08-14 19:25:29 +00:00
|
|
|
const text = (stream: NodeJS.ReadableStream | Readable | null) =>
|
|
|
|
stream ? textFromStream(stream).then((t) => t.trimEnd()) : '';
|
2023-08-14 19:23:14 +00:00
|
|
|
|
2023-08-14 19:25:29 +00:00
|
|
|
export async function shell(
|
|
|
|
command: string,
|
|
|
|
flags: string[],
|
|
|
|
opts: ExecaOptions = {}
|
|
|
|
): Promise<Output> {
|
|
|
|
const controller = opts.timeout ? new AbortController() : undefined;
|
|
|
|
const child = spawn(command, flags, {
|
|
|
|
cwd: opts.cwd,
|
|
|
|
shell: true,
|
|
|
|
stdio: opts.stdio,
|
|
|
|
signal: controller?.signal,
|
|
|
|
});
|
|
|
|
const stdout = await text(child.stdout);
|
|
|
|
const stderr = await text(child.stderr);
|
|
|
|
if (opts.timeout) {
|
|
|
|
sleep(opts.timeout).then(() => {
|
|
|
|
controller!.abort();
|
|
|
|
throw { stdout, stderr, exitCode: 1 };
|
|
|
|
});
|
|
|
|
}
|
|
|
|
await new Promise((resolve) => child.on('exit', resolve));
|
|
|
|
const { exitCode } = child;
|
|
|
|
if (exitCode !== 0) {
|
|
|
|
throw { stdout, stderr, exitCode };
|
|
|
|
}
|
|
|
|
return { stdout, stderr, exitCode };
|
2023-08-14 19:23:14 +00:00
|
|
|
}
|