cli: fix create-plugin tmp dir creation

This commit is contained in:
Patrik Oldsberg
2020-10-29 22:55:05 +01:00
parent ef9358c36e
commit e9c222605b
2 changed files with 10 additions and 46 deletions
@@ -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({
@@ -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(