Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[create-astro] update shell logic #8089

Merged
merged 4 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/angry-balloons-argue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'create-astro': patch
---

Fix install step to avoid unscrutable errors
36 changes: 19 additions & 17 deletions packages/create-astro/src/shell.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// 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 { ChildProcess, StdioOptions } from 'node:child_process';
import type { Readable } from 'node:stream';

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;
Expand All @@ -25,25 +24,28 @@ export async function shell(
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 };
let child: ChildProcess;
let stdout = '';
let stderr = '';
try {
child = spawn(command, flags, {
cwd: opts.cwd,
shell: true,
stdio: opts.stdio,
timeout: opts.timeout,
});
const done = new Promise((resolve) => child.on('close', resolve));
([stdout, stderr] = await Promise.all([text(child.stdout), text(child.stderr)]));
await done;
} catch (e) {
throw { stdout, stderr, exitCode: 1 };
}
await new Promise((resolve) => child.on('exit', resolve));
const { exitCode } = child;
if (exitCode === null) {
throw new Error('Timeout');
}
if (exitCode !== 0) {
throw { stdout, stderr, exitCode };
throw new Error(stderr);
}
return { stdout, stderr, exitCode };
}