diff --git a/.changeset/soft-berries-prove.md b/.changeset/soft-berries-prove.md new file mode 100644 index 000000000000..cce169649930 --- /dev/null +++ b/.changeset/soft-berries-prove.md @@ -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 `../`. diff --git a/packages/create-astro/src/actions/template.ts b/packages/create-astro/src/actions/template.ts index 8d22e95b1f92..bdb32607692b 100644 --- a/packages/create-astro/src/actions/template.ts +++ b/packages/create-astro/src/actions/template.ts @@ -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 {