Template directly to specified path otherwise use temp dir

When the `--path` argument is specified, then perform the create-app templating
directly to that path. Otherwise, template to a temporary directory, and move
the files to a directory based on the project name.

Removed the `cleanUp` method in favor of removing temporary files directly in
the `moveApp` method in a `finally` block.

Reverted `moveApp` method to use the move operation instead ofe recursively
copying files.

Signed-off-by: Colton Padden <colton.padden@fastmail.com>
This commit is contained in:
Colton Padden
2021-10-21 14:53:29 -04:00
parent 0a788da873
commit c671c38279
+26 -44
View File
@@ -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<void> => {
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<void> => {
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);
}