diff --git a/packages/create-app/src/createApp.ts b/packages/create-app/src/createApp.ts index 8f054319e0..d833f0c4d8 100644 --- a/packages/create-app/src/createApp.ts +++ b/packages/create-app/src/createApp.ts @@ -20,11 +20,10 @@ import chalk from 'chalk'; import { Command } from 'commander'; import inquirer, { Answers, Question } from 'inquirer'; import { exec as execCb } from 'child_process'; -import { resolve as resolvePath, relative as relativePath } from 'path'; +import { resolve as resolvePath } from 'path'; import { findPaths } from '@backstage/cli-common'; import os from 'os'; import { Task, templatingTask } from './lib/tasks'; -import recursive from 'recursive-readdir'; const exec = promisify(execCb); @@ -69,12 +68,6 @@ async function createTemporaryAppFolder(tempDir: string) { }); } -async function cleanUp(tempDir: string) { - await Task.forItem('remove', 'temporary directory', async () => { - await fs.remove(tempDir); - }); -} - async function buildApp(appDir: string) { const runCmd = async (cmd: string) => { await Task.forItem('executing', cmd, async () => { @@ -94,28 +87,17 @@ async function buildApp(appDir: string) { async function moveApp(tempDir: string, destination: string, id: string) { await Task.forItem('moving', id, async () => { - // Get the temporary files relative path in relation to the `tempDir` so - // that it can be used in resolving destination file path - const relativeTempFiles = await recursive(tempDir) - .then(files => { - return files.map(file => { - return relativePath(tempDir, file); - }); - }) + await fs + .move(tempDir, destination) .catch(error => { - throw new Error(`Failed to read temporary directory, ${error}`); - }); - - for (const relativeTempFile of relativeTempFiles) { - const tempFile = resolvePath(tempDir, relativeTempFile); - const destFile = resolvePath(destination, relativeTempFile); - - await fs.copy(tempFile, destFile).catch(error => { throw new Error( - `Failed to move file from ${tempFile} to ${destFile}, ${error}`, + `Failed to move app from ${tempDir} to ${destination}: ${error.message}`, ); + }) + .finally(() => { + // remove temporary files on both success and failure + fs.removeSync(tempDir); }); - } }); } @@ -164,27 +146,30 @@ export default async (cmd: Command): Promise => { Task.log('Creating the app...'); try { - Task.section('Checking if the directory is available'); if (cmd.path) { + // Template directly to specified path + + Task.section('Checking that supplied path exists'); await checkPathExistsAndIsDirectory(appDir); + + Task.section('Preparing files'); + await templatingTask(templateDir, cmd.path, answers); } else { - // Directory must not already exist when using consructed path from - // `answers.name` + // Template to temporary location, and then move files + + Task.section('Checking if the directory is available'); await checkAppPathDoesntExist(paths.targetDir, answers.name); + + Task.section('Creating a temporary app directory'); + await createTemporaryAppFolder(tempDir); + + Task.section('Preparing files'); + await templatingTask(templateDir, tempDir, answers); + + Task.section('Moving to final location'); + await moveApp(tempDir, appDir, answers.name); } - Task.section('Creating a temporary app directory'); - await createTemporaryAppFolder(tempDir); - - Task.section('Preparing files'); - await templatingTask(templateDir, tempDir, answers); - - Task.section('Moving to final location'); - await moveApp(tempDir, appDir, answers.name); - - Task.section('Cleanup'); - await cleanUp(tempDir); - if (!cmd.skipInstall) { Task.section('Building the app'); await buildApp(appDir); @@ -207,10 +192,7 @@ export default async (cmd: Command): Promise => { Task.error(error.message); Task.log('It seems that something went wrong when creating the app 🤔'); - Task.log('We are going to clean up, and then you can try again.'); - Task.section('Cleanup'); - await cleanUp(tempDir); Task.error('🔥 Failed to create app!'); Task.exit(1); }