summaryrefslogtreecommitdiff
path: root/packages/create-astro/src/shell.ts
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--packages/create-astro/src/shell.ts65
1 files changed, 35 insertions, 30 deletions
diff --git a/packages/create-astro/src/shell.ts b/packages/create-astro/src/shell.ts
index 4941d788c..d2d7ef033 100644
--- a/packages/create-astro/src/shell.ts
+++ b/packages/create-astro/src/shell.ts
@@ -3,42 +3,47 @@
import type { StdioOptions } from 'node:child_process';
import type { Readable } from 'node:stream';
-import { text as textFromStream } from 'node:stream/consumers';
import { spawn } from 'node:child_process';
+import { text as textFromStream } from 'node:stream/consumers';
import { setTimeout as sleep } from 'node:timers/promises';
export interface ExecaOptions {
- cwd?: string | URL;
- stdio?: StdioOptions;
- timeout?: number;
+ cwd?: string | URL;
+ stdio?: StdioOptions;
+ timeout?: number;
}
export interface Output {
- stdout: string;
- stderr: string;
- exitCode: number;
+ stdout: string;
+ stderr: string;
+ exitCode: number;
}
-const text = (stream: NodeJS.ReadableStream | Readable | null) => stream ? textFromStream(stream).then(t => t.trimEnd()) : '';
+const text = (stream: NodeJS.ReadableStream | Readable | null) =>
+ stream ? textFromStream(stream).then((t) => t.trimEnd()) : '';
-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 }
+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 };
}