From c9e843547efbf73320f01aa7c3c3115dc30d4322 Mon Sep 17 00:00:00 2001 From: Colton Padden Date: Wed, 27 Oct 2021 14:03:28 -0400 Subject: [PATCH] Create `--path` directory if it does not already exist Instead of failing when the specified path does not exist, create it using `mkdirs`. This ensures that that either the directory already exists, or it's created. Signed-off-by: Colton Padden --- packages/create-app/src/createApp.ts | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/packages/create-app/src/createApp.ts b/packages/create-app/src/createApp.ts index d833f0c4d8..270ed3fd54 100644 --- a/packages/create-app/src/createApp.ts +++ b/packages/create-app/src/createApp.ts @@ -27,7 +27,7 @@ import { Task, templatingTask } from './lib/tasks'; const exec = promisify(execCb); -async function checkAppPathDoesntExist(rootDir: string, name: string) { +async function checkAppExists(rootDir: string, name: string) { await Task.forItem('checking', name, async () => { const destination = resolvePath(rootDir, name); @@ -40,18 +40,13 @@ async function checkAppPathDoesntExist(rootDir: string, name: string) { }); } -async function checkPathExistsAndIsDirectory(path: string) { +async function checkPathExists(path: string) { await Task.forItem('checking', path, async () => { - if (await fs.pathExists(path)) { - if (!fs.lstatSync(path).isDirectory()) { - throw new Error( - 'Directory specified with --path argument is a file\nPlease ensure target path is a directory', - ); - } - } else { - throw new Error( - 'Directory specified with --path argument does not exist\nPlease try again with a different path', - ); + try { + await fs.mkdirs(path); + } catch (error) { + // will fail if a file already exists at given `path` + throw new Error(`Failed to create app directory: ${error.message}`); } }); } @@ -150,7 +145,7 @@ export default async (cmd: Command): Promise => { // Template directly to specified path Task.section('Checking that supplied path exists'); - await checkPathExistsAndIsDirectory(appDir); + await checkPathExists(appDir); Task.section('Preparing files'); await templatingTask(templateDir, cmd.path, answers); @@ -158,7 +153,7 @@ export default async (cmd: Command): Promise => { // Template to temporary location, and then move files Task.section('Checking if the directory is available'); - await checkAppPathDoesntExist(paths.targetDir, answers.name); + await checkAppExists(paths.targetDir, answers.name); Task.section('Creating a temporary app directory'); await createTemporaryAppFolder(tempDir);