astro/packages/create-astro/src/shell.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

52 lines
1.3 KiB
TypeScript
Raw Normal View History

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 { ChildProcess, StdioOptions } from 'node:child_process';
2023-08-14 19:23:14 +00:00
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
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> {
let child: ChildProcess;
let stdout = '';
let stderr = '';
try {
child = spawn(command, flags, {
cwd: opts.cwd,
shell: true,
stdio: opts.stdio,
timeout: opts.timeout,
2023-08-14 19:25:29 +00:00
});
const done = new Promise((resolve) => child.on('close', resolve));
2023-08-15 17:27:28 +00:00
[stdout, stderr] = await Promise.all([text(child.stdout), text(child.stderr)]);
await done;
} catch (e) {
throw { stdout, stderr, exitCode: 1 };
2023-08-14 19:25:29 +00:00
}
const { exitCode } = child;
if (exitCode === null) {
throw new Error('Timeout');
}
2023-08-14 19:25:29 +00:00
if (exitCode !== 0) {
throw new Error(stderr);
2023-08-14 19:25:29 +00:00
}
return { stdout, stderr, exitCode };
2023-08-14 19:23:14 +00:00
}