From e9c222605bb1ae1fccfd0990a645ad576124796f Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Thu, 29 Oct 2020 22:55:05 +0100 Subject: [PATCH] cli: fix create-plugin tmp dir creation --- .../create-plugin/createPlugin.test.ts | 27 +---------------- .../commands/create-plugin/createPlugin.ts | 29 ++++++------------- 2 files changed, 10 insertions(+), 46 deletions(-) diff --git a/packages/cli/src/commands/create-plugin/createPlugin.test.ts b/packages/cli/src/commands/create-plugin/createPlugin.test.ts index a08bb0da87..660caec189 100644 --- a/packages/cli/src/commands/create-plugin/createPlugin.test.ts +++ b/packages/cli/src/commands/create-plugin/createPlugin.test.ts @@ -17,9 +17,7 @@ import fs from 'fs-extra'; import path from 'path'; import mockFs from 'mock-fs'; -import os from 'os'; -import del from 'del'; -import { createTemporaryPluginFolder, movePlugin } from './createPlugin'; +import { movePlugin } from './createPlugin'; const id = 'testPluginMock'; @@ -28,29 +26,6 @@ describe('createPlugin', () => { mockFs.restore(); }); - describe('createPluginFolder', () => { - it('should create a temporary plugin directory in the correct place', async () => { - const tempDir = path.join(os.tmpdir(), id); - try { - await createTemporaryPluginFolder(tempDir); - await expect(fs.pathExists(tempDir)).resolves.toBe(true); - expect(tempDir).toMatch(id); - } finally { - await del(tempDir, { force: true }); - } - }); - - it('should not create a temporary plugin directory if it already exists', async () => { - mockFs({ - [id]: {}, - }); - - await expect(createTemporaryPluginFolder(id)).rejects.toThrow( - /Failed to create temporary plugin directory/, - ); - }); - }); - describe('movePlugin', () => { it('should move the temporary plugin directory to its final place', async () => { mockFs({ diff --git a/packages/cli/src/commands/create-plugin/createPlugin.ts b/packages/cli/src/commands/create-plugin/createPlugin.ts index f54a62f613..c9f8b3a81d 100644 --- a/packages/cli/src/commands/create-plugin/createPlugin.ts +++ b/packages/cli/src/commands/create-plugin/createPlugin.ts @@ -19,7 +19,7 @@ import { promisify } from 'util'; import chalk from 'chalk'; import inquirer, { Answers, Question } from 'inquirer'; import { exec as execCb } from 'child_process'; -import { resolve as resolvePath } from 'path'; +import { resolve as resolvePath, join as joinPath } from 'path'; import os from 'os'; import { Command } from 'commander'; import { @@ -46,18 +46,6 @@ async function checkExists(destination: string) { }); } -export async function createTemporaryPluginFolder(tempDir: string) { - await Task.forItem('creating', 'temporary directory', async () => { - try { - await fs.mkdir(tempDir); - } catch (error) { - throw new Error( - `Failed to create temporary plugin directory: ${error.message}`, - ); - } - }); -} - const sortObjectByKeys = (obj: { [name in string]: string }) => { return Object.keys(obj) .sort() @@ -238,7 +226,6 @@ export default async (cmd: Command) => { ? 'templates/default-backend-plugin' : 'templates/default-plugin', ); - const tempDir = resolvePath(os.tmpdir(), pluginId); const pluginDir = isMonoRepo ? paths.resolveTargetRoot('plugins', pluginId) : paths.resolveTargetRoot(pluginId); @@ -250,13 +237,15 @@ export default async (cmd: Command) => { Task.log(); Task.log('Creating the plugin...'); + Task.section('Checking if the plugin ID is available'); + await checkExists(pluginDir); + + Task.section('Creating a temporary plugin directory'); + const tempDir = await fs.mkdtemp( + joinPath(os.tmpdir(), `backstage-plugin-${pluginId}`), + ); + try { - Task.section('Checking if the plugin ID is available'); - await checkExists(pluginDir); - - Task.section('Creating a temporary plugin directory'); - await createTemporaryPluginFolder(tempDir); - Task.section('Preparing files'); await templatingTask(