From 066b4b4efcde2320d29040c5bd385c67f30c701a Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Thu, 16 Feb 2023 14:28:15 -0600 Subject: [PATCH] Improve `create-astro` error handling (#6266) * fix(create-astro): improve error handling for tasks that use spinner display * refactor: timeout after 60s * chore: remove unused file --- .changeset/tall-geese-end.md | 5 +++++ packages/create-astro/grubby-group | 1 - packages/create-astro/src/actions/dependencies.ts | 12 +++++++++--- packages/create-astro/src/actions/git.ts | 8 ++++++-- packages/create-astro/src/actions/template.ts | 6 +++++- packages/create-astro/src/actions/typescript.ts | 6 +++++- 6 files changed, 30 insertions(+), 8 deletions(-) create mode 100644 .changeset/tall-geese-end.md delete mode 160000 packages/create-astro/grubby-group diff --git a/.changeset/tall-geese-end.md b/.changeset/tall-geese-end.md new file mode 100644 index 000000000000..ba9c934816f8 --- /dev/null +++ b/.changeset/tall-geese-end.md @@ -0,0 +1,5 @@ +--- +'create-astro': patch +--- + +Improve error handling during tasks that display a spinner diff --git a/packages/create-astro/grubby-group b/packages/create-astro/grubby-group deleted file mode 160000 index 9a401ddf2e78..000000000000 --- a/packages/create-astro/grubby-group +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 9a401ddf2e7896d7928eea910c61b5d5a29481a1 diff --git a/packages/create-astro/src/actions/dependencies.ts b/packages/create-astro/src/actions/dependencies.ts index 5991e958f8f8..7195c81a5965 100644 --- a/packages/create-astro/src/actions/dependencies.ts +++ b/packages/create-astro/src/actions/dependencies.ts @@ -1,7 +1,7 @@ import type { Context } from './context'; import { execa } from 'execa'; -import { info, spinner, title } from '../messages.js'; +import { info, error, spinner, title } from '../messages.js'; export async function dependencies( ctx: Pick @@ -25,7 +25,12 @@ export async function dependencies( await spinner({ start: `Dependencies installing with ${ctx.pkgManager}...`, end: 'Dependencies installed', - while: () => install({ pkgManager: ctx.pkgManager, cwd: ctx.cwd }), + while: () => + install({ pkgManager: ctx.pkgManager, cwd: ctx.cwd }).catch((e) => { + // eslint-disable-next-line no-console + error('error', e); + process.exit(1); + }), }); } else { await info( @@ -38,7 +43,8 @@ export async function dependencies( async function install({ pkgManager, cwd }: { pkgManager: string; cwd: string }) { const installExec = execa(pkgManager, ['install'], { cwd }); return new Promise((resolve, reject) => { - installExec.on('error', (error) => reject(error)); + setTimeout(() => reject(`Request timed out after one minute`), 60_000); + installExec.on('error', (e) => reject(e)); installExec.on('close', () => resolve()); }); } diff --git a/packages/create-astro/src/actions/git.ts b/packages/create-astro/src/actions/git.ts index 51a7e09c8ba9..6949346fba1c 100644 --- a/packages/create-astro/src/actions/git.ts +++ b/packages/create-astro/src/actions/git.ts @@ -4,7 +4,7 @@ import type { Context } from './context'; import { color } from '@astrojs/cli-kit'; import { execa } from 'execa'; -import { info, spinner, title } from '../messages.js'; +import { info, spinner, error, title } from '../messages.js'; export async function git(ctx: Pick) { if (fs.existsSync(path.join(ctx.cwd, '.git'))) { @@ -29,7 +29,11 @@ export async function git(ctx: Pick init({ cwd: ctx.cwd }), + while: () => init({ cwd: ctx.cwd }).catch((e) => { + // eslint-disable-next-line no-console + error('error', e); + process.exit(1); + }), }); } else { await info( diff --git a/packages/create-astro/src/actions/template.ts b/packages/create-astro/src/actions/template.ts index e809005a2db5..164e3354275d 100644 --- a/packages/create-astro/src/actions/template.ts +++ b/packages/create-astro/src/actions/template.ts @@ -34,7 +34,11 @@ export async function template( await spinner({ start: 'Template copying...', end: 'Template copied', - while: () => copyTemplate(ctx.template!, ctx as Context), + while: () => copyTemplate(ctx.template!, ctx as Context).catch((e) => { + // eslint-disable-next-line no-console + error('error', e); + process.exit(1); + }), }); } else { ctx.exit(1); diff --git a/packages/create-astro/src/actions/typescript.ts b/packages/create-astro/src/actions/typescript.ts index 24e9e8362025..2cbbcf72a9ca 100644 --- a/packages/create-astro/src/actions/typescript.ts +++ b/packages/create-astro/src/actions/typescript.ts @@ -61,7 +61,11 @@ export async function typescript( await spinner({ start: 'TypeScript customizing...', end: 'TypeScript customized', - while: () => setupTypeScript(ts!, { cwd: ctx.cwd }), + while: () => setupTypeScript(ts!, { cwd: ctx.cwd }).catch((e) => { + // eslint-disable-next-line no-console + error('error', e); + process.exit(1); + }), }); } else { }