diff options
Diffstat (limited to 'packages/create-astro/src/index.ts')
-rw-r--r-- | packages/create-astro/src/index.ts | 53 |
1 files changed, 38 insertions, 15 deletions
diff --git a/packages/create-astro/src/index.ts b/packages/create-astro/src/index.ts index ec54d250c..2286c13f5 100644 --- a/packages/create-astro/src/index.ts +++ b/packages/create-astro/src/index.ts @@ -28,6 +28,10 @@ export function mkdirp(dir: string) { } } +function isEmpty(dirPath: string) { + return !fs.existsSync(dirPath) || fs.readdirSync(dirPath).length === 0; +} + const { version } = JSON.parse( fs.readFileSync(new URL('../package.json', import.meta.url), 'utf-8') ); @@ -47,22 +51,41 @@ export async function main() { spinner.succeed(); - const cwd = (args['_'][2] as string) || '.'; - if (fs.existsSync(cwd)) { - if (fs.readdirSync(cwd).length > 0) { - const response = await prompts({ - type: 'confirm', - name: 'forceOverwrite', - message: 'Directory not empty. Continue [force overwrite]?', - initial: false, - }); - if (!response.forceOverwrite) { - process.exit(1); - } - mkdirp(cwd); + let cwd = args['_'][2] as string; + + if (cwd && isEmpty(cwd)) { + let acknowledgeProjectDir = ora({ + color: 'green', + text: `Using ${bold(cwd)} as project directory.`, + }); + acknowledgeProjectDir.succeed(); + } + + if (!cwd || !isEmpty(cwd)) { + const notEmptyMsg = (dirPath: string) => + `"${bold(dirPath)}" is not empty. Please clear contents or choose a different path.`; + + if (!isEmpty(cwd)) { + let rejectProjectDir = ora({ color: 'red', text: notEmptyMsg(cwd) }); + rejectProjectDir.fail(); } - } else { - mkdirp(cwd); + const dirResponse = await prompts({ + type: 'text', + name: 'directory', + message: 'Where would you like to create your app?', + initial: './my-astro-site', + validate(value) { + if (!isEmpty(value)) { + return notEmptyMsg(value); + } + return true; + }, + }); + cwd = dirResponse.directory; + } + + if (!cwd) { + process.exit(1); } const options = await prompts([ |