Skip to content

Commit

Permalink
Improve create-astro error handling (#6266)
Browse files Browse the repository at this point in the history
* fix(create-astro): improve error handling for tasks that use spinner display

* refactor: timeout after 60s

* chore: remove unused file
  • Loading branch information
natemoo-re authored Feb 16, 2023
1 parent fb4e79b commit 066b4b4
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/tall-geese-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'create-astro': patch
---

Improve error handling during tasks that display a spinner
1 change: 0 additions & 1 deletion packages/create-astro/grubby-group
Submodule grubby-group deleted from 9a401d
12 changes: 9 additions & 3 deletions packages/create-astro/src/actions/dependencies.ts
Original file line number Diff line number Diff line change
@@ -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<Context, 'install' | 'yes' | 'prompt' | 'pkgManager' | 'cwd' | 'dryRun'>
Expand All @@ -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(
Expand All @@ -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<void>((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());
});
}
8 changes: 6 additions & 2 deletions packages/create-astro/src/actions/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Context, 'cwd' | 'git' | 'yes' | 'prompt' | 'dryRun'>) {
if (fs.existsSync(path.join(ctx.cwd, '.git'))) {
Expand All @@ -29,7 +29,11 @@ export async function git(ctx: Pick<Context, 'cwd' | 'git' | 'yes' | 'prompt' |
await spinner({
start: 'Git initializing...',
end: 'Git initialized',
while: () => 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(
Expand Down
6 changes: 5 additions & 1 deletion packages/create-astro/src/actions/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 5 additions & 1 deletion packages/create-astro/src/actions/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
}
Expand Down

0 comments on commit 066b4b4

Please sign in to comment.