create-app: create unique temp directory for each execution

Signed-off-by: Johan Haals <johan.haals@gmail.com>
This commit is contained in:
Johan Haals
2023-09-26 14:00:41 +02:00
parent 50a67bab14
commit c8ec0dea4a
5 changed files with 10 additions and 51 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/create-app': patch
---
Create unique temp directory for each `create-app` execution.
+1 -6
View File
@@ -36,10 +36,6 @@ const templatingMock = jest.spyOn(tasks, 'templatingTask');
const checkAppExistsMock = jest.spyOn(tasks, 'checkAppExistsTask');
const tryInitGitRepositoryMock = jest.spyOn(tasks, 'tryInitGitRepository');
const readGitConfig = jest.spyOn(tasks, 'readGitConfig');
const createTemporaryAppFolderMock = jest.spyOn(
tasks,
'createTemporaryAppFolderTask',
);
const moveAppMock = jest.spyOn(tasks, 'moveAppTask');
const buildAppMock = jest.spyOn(tasks, 'buildAppTask');
@@ -73,7 +69,6 @@ describe('command entrypoint', () => {
const cmd = {} as unknown as Command;
await createApp(cmd);
expect(checkAppExistsMock).toHaveBeenCalled();
expect(createTemporaryAppFolderMock).toHaveBeenCalled();
expect(tryInitGitRepositoryMock).toHaveBeenCalled();
expect(templatingMock).toHaveBeenCalled();
expect(templatingMock.mock.lastCall?.[0]).toEqual(
@@ -85,7 +80,7 @@ describe('command entrypoint', () => {
'default-app',
),
);
expect(templatingMock.mock.lastCall?.[1]).toEqual(
expect(templatingMock.mock.lastCall?.[1]).toContain(
path.join(tmpdir(), 'MyApp'),
);
expect(moveAppMock).toHaveBeenCalled();
+4 -4
View File
@@ -20,12 +20,12 @@ import inquirer, { Answers } from 'inquirer';
import { resolve as resolvePath } from 'path';
import { findPaths } from '@backstage/cli-common';
import os from 'os';
import fs from 'fs-extra';
import {
Task,
buildAppTask,
checkAppExistsTask,
checkPathExistsTask,
createTemporaryAppFolderTask,
moveAppTask,
templatingTask,
tryInitGitRepository,
@@ -37,7 +37,6 @@ const DEFAULT_BRANCH = 'master';
export default async (opts: OptionValues): Promise<void> => {
/* eslint-disable-next-line no-restricted-syntax */
const paths = findPaths(__dirname);
const answers: Answers = await inquirer.prompt([
{
type: 'input',
@@ -68,7 +67,6 @@ export default async (opts: OptionValues): Promise<void> => {
const templateDir = opts.templatePath
? paths.resolveTarget(opts.templatePath)
: paths.resolveOwn('templates/default-app');
const tempDir = resolvePath(os.tmpdir(), answers.name);
// Use `--path` argument as application directory when specified, otherwise
// create a directory using `answers.name`
@@ -100,7 +98,9 @@ export default async (opts: OptionValues): Promise<void> => {
await checkAppExistsTask(paths.targetDir, answers.name);
Task.section('Creating a temporary app directory');
await createTemporaryAppFolderTask(tempDir);
const tempDir = await fs.mkdtemp(
resolvePath(resolvePath(os.tmpdir(), answers.name)),
);
Task.section('Preparing files');
await templatingTask(templateDir, tempDir, {
-25
View File
@@ -24,7 +24,6 @@ import {
buildAppTask,
checkAppExistsTask,
checkPathExistsTask,
createTemporaryAppFolderTask,
moveAppTask,
templatingTask,
tryInitGitRepository,
@@ -163,30 +162,6 @@ describe('tasks', () => {
});
});
describe('createTemporaryAppFolderTask', () => {
it('should create a directory at a given path', async () => {
const tempDir = 'projects/tmpFolder';
await expect(
createTemporaryAppFolderTask(tempDir),
).resolves.not.toThrow();
expect(fs.existsSync(tempDir)).toBe(true);
});
it('should fail if a directory of the same name exists', async () => {
const tempDir = 'projects/dir';
await expect(createTemporaryAppFolderTask(tempDir)).rejects.toThrow(
'file already exists',
);
});
it('should fail if a file of the same name exists', async () => {
const tempDir = 'projects/dir/my-file.txt';
await expect(createTemporaryAppFolderTask(tempDir)).rejects.toThrow(
'file already exists',
);
});
});
describe('buildAppTask', () => {
it('should change to `appDir` and run `yarn install` and `yarn tsc`', async () => {
const mockChdir = jest.spyOn(process, 'chdir');
-16
View File
@@ -178,22 +178,6 @@ export async function checkPathExistsTask(path: string) {
});
}
/**
* Create a folder to store templated files
*
* @param tempDir - target temporary directory
* @throws if `fs.mkdir` fails
*/
export async function createTemporaryAppFolderTask(tempDir: string) {
await Task.forItem('creating', 'temporary directory', async () => {
try {
await fs.mkdir(tempDir);
} catch (error) {
throw new Error(`Failed to create temporary app directory, ${error}`);
}
});
}
/**
* Run `yarn install` and `run tsc` in application directory
*