fix(create-astro): ignore fs errors after download fails (#8841)

Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
This commit is contained in:
Genteure 2023-10-25 00:17:14 +08:00 committed by GitHub
parent 5a3d46da1e
commit f2dd895d71
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 1 deletions

View file

@ -0,0 +1,5 @@
---
'create-astro': patch
---
No longer attempts to delete the directory after a template download fails if the path is `.`, `./` or starts with `../`.

View file

@ -93,7 +93,16 @@ export default async function copyTemplate(tmpl: string, ctx: Context) {
dir: '.',
});
} catch (err: any) {
fs.rmdirSync(ctx.cwd);
// Only remove the directory if it's most likely created by us.
if (ctx.cwd !== '.' && ctx.cwd !== './' && !ctx.cwd.startsWith('../')) {
try {
fs.rmdirSync(ctx.cwd);
} catch (_) {
// Ignore any errors from removing the directory,
// make sure we throw and display the original error.
}
}
if (err.message.includes('404')) {
throw new Error(`Template ${color.reset(tmpl)} ${color.dim('does not exist!')}`);
} else {