cli/new: cleanup template execution
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
@@ -23,11 +23,10 @@ import {
|
||||
} from 'path';
|
||||
|
||||
import { paths } from '../../paths';
|
||||
import { Task } from '../../tasks';
|
||||
import { PortableTemplate, PortableTemplateInput } from '../types';
|
||||
import { ForwardedError } from '@backstage/errors';
|
||||
import { TemporaryDirectoryManager } from './TemporaryDirectoryManager';
|
||||
import { isMonoRepo } from '@backstage/cli-node';
|
||||
import { isMonoRepo as getIsMonoRepo } from '@backstage/cli-node';
|
||||
import { PortableTemplater } from './PortableTemplater';
|
||||
|
||||
export async function executePluginPackageTemplate(
|
||||
@@ -36,27 +35,50 @@ export async function executePluginPackageTemplate(
|
||||
): Promise<{ targetDir: string }> {
|
||||
const targetDir = paths.resolveTargetRoot(input.packageParams.packagePath);
|
||||
|
||||
Task.section('Checking Prerequisites');
|
||||
const shortPluginDir = relativePath(paths.targetRoot, targetDir);
|
||||
await Task.forItem('availability', shortPluginDir, async () => {
|
||||
if (await fs.pathExists(targetDir)) {
|
||||
throw new Error(
|
||||
`A package with the same plugin ID already exists at ${chalk.cyan(
|
||||
shortPluginDir,
|
||||
)}. Please try again with a different ID.`,
|
||||
);
|
||||
}
|
||||
});
|
||||
if (await fs.pathExists(targetDir)) {
|
||||
throw new Error(
|
||||
`A package with the same plugin ID already exists at ${chalk.cyan(
|
||||
shortPluginDir,
|
||||
)}. Please try again with a different ID.`,
|
||||
);
|
||||
}
|
||||
|
||||
const tmpDirManager = TemporaryDirectoryManager.create();
|
||||
|
||||
try {
|
||||
const tempDir = await Task.forItem('creating', 'temp dir', async () => {
|
||||
return tmpDirManager.createDir('backstage-create');
|
||||
});
|
||||
const tempDir = await tmpDirManager.createDir('backstage-create');
|
||||
const isMonoRepo = await getIsMonoRepo();
|
||||
const templater = await PortableTemplater.create();
|
||||
|
||||
Task.section('Executing Template');
|
||||
await templatingTask(tempDir, template, input, await isMonoRepo());
|
||||
const templatedValues = templater.templateRecord(
|
||||
template.templateValues,
|
||||
input.params,
|
||||
);
|
||||
|
||||
for (const file of template.files) {
|
||||
if (isMonoRepo && file.path === 'tsconfig.json') {
|
||||
continue;
|
||||
}
|
||||
|
||||
const destPath = resolvePath(tempDir, file.path);
|
||||
await fs.ensureDir(dirname(destPath));
|
||||
|
||||
if (file.syntax === 'handlebars') {
|
||||
let content = file.content;
|
||||
|
||||
if (file.syntax === 'handlebars') {
|
||||
content = templater.template(file.content, {
|
||||
...input.params,
|
||||
...templatedValues,
|
||||
});
|
||||
}
|
||||
|
||||
await fs.writeFile(destPath, content).catch(error => {
|
||||
throw new ForwardedError(`Failed to copy file to ${destPath}`, error);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Format package.json if it exists
|
||||
const pkgJsonPath = resolvePath(tempDir, 'package.json');
|
||||
@@ -65,13 +87,10 @@ export async function executePluginPackageTemplate(
|
||||
await fs.writeJson(pkgJsonPath, pkgJson, { spaces: 2 });
|
||||
}
|
||||
|
||||
Task.section('Installing');
|
||||
await Task.forItem('moving', shortPluginDir, async () => {
|
||||
await fs.move(tempDir, targetDir).catch(error => {
|
||||
throw new Error(
|
||||
`Failed to move package from ${tempDir} to ${targetDir}, ${error.message}`,
|
||||
);
|
||||
});
|
||||
await fs.move(tempDir, targetDir).catch(error => {
|
||||
throw new Error(
|
||||
`Failed to move package from ${tempDir} to ${targetDir}, ${error.message}`,
|
||||
);
|
||||
});
|
||||
|
||||
return { targetDir };
|
||||
@@ -79,50 +98,3 @@ export async function executePluginPackageTemplate(
|
||||
tmpDirManager.cleanup();
|
||||
}
|
||||
}
|
||||
|
||||
export async function templatingTask(
|
||||
destinationDir: string,
|
||||
template: PortableTemplate,
|
||||
input: PortableTemplateInput,
|
||||
inMonoRepo: boolean,
|
||||
) {
|
||||
const templater = await PortableTemplater.create();
|
||||
|
||||
const templatedValues = templater.templateRecord(
|
||||
template.templateValues,
|
||||
input.params,
|
||||
);
|
||||
|
||||
for (const file of template.files) {
|
||||
if (inMonoRepo && file.path === 'tsconfig.json') {
|
||||
continue;
|
||||
}
|
||||
|
||||
const destPath = resolvePath(destinationDir, file.path);
|
||||
await fs.ensureDir(dirname(destPath));
|
||||
|
||||
if (file.syntax === 'handlebars') {
|
||||
await Task.forItem(
|
||||
file.syntax ? 'templating' : 'copying',
|
||||
file.path,
|
||||
async () => {
|
||||
let content = file.content;
|
||||
|
||||
if (file.syntax === 'handlebars') {
|
||||
content = templater.template(file.content, {
|
||||
...input.params,
|
||||
...templatedValues,
|
||||
});
|
||||
}
|
||||
|
||||
await fs.writeFile(destPath, content).catch(error => {
|
||||
throw new ForwardedError(
|
||||
`Failed to copy file to ${destPath}`,
|
||||
error,
|
||||
);
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user