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] Error on --template that does not exist #6677

Merged
merged 2 commits into from
Mar 28, 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/bright-apricots-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'create-astro': patch
---

Fix: Log an error when passing a `--template` that does not exist
22 changes: 17 additions & 5 deletions packages/create-astro/src/actions/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,13 @@ export async function template(ctx: Pick<Context, 'template' | 'prompt' | 'dryRu
while: () =>
copyTemplate(ctx.template!, ctx as Context).catch((e) => {
// eslint-disable-next-line no-console
error('error', e);
process.exit(1);
if (e instanceof Error) {
error('error', e.message);
process.exit(1);
} else {
error('error', 'Unable to clone template.');
process.exit(1);
}
}),
});
} else {
Expand Down Expand Up @@ -81,11 +86,18 @@ export default async function copyTemplate(tmpl: string, ctx: Context) {
} catch (err: any) {
fs.rmdirSync(ctx.cwd);
if (err.message.includes('404')) {
await error('Error', `Template ${color.reset(tmpl)} ${color.dim('does not exist!')}`);
throw new Error(`Template ${color.reset(tmpl)} ${color.dim('does not exist!')}`);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This used to log the error and close the spinner before ctx.exit could be called. This throws so the higher scope can catch and log appropriately.

} else {
console.error(err.message);
throw new Error(err.message);
}
ctx.exit(1);
}

// It's possible the repo exists (ex. `withastro/astro`),
// But the template route is invalid (ex. `withastro/astro/examples/DNE`).
// `giget` doesn't throw for this case,
// so check if the directory is still empty as a heuristic.
if (fs.readdirSync(ctx.cwd).length === 0) {
throw new Error(`Template ${color.reset(tmpl)} ${color.dim('is empty!')}`);
}

// Post-process in parallel
Expand Down