cli/new: use separate role definitions for templates
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
@@ -108,9 +108,9 @@ describe('loadTemplate', () => {
|
||||
target: mockDir.resolve('path/to/template1.yaml'),
|
||||
}),
|
||||
).rejects.toThrow(
|
||||
`Failed to load template contents from '${mockDir.resolve(
|
||||
'path/to/template1',
|
||||
)}'; caused by Error: Unknown package role 'invalid-role'`,
|
||||
`Invalid template definition at '${mockDir.resolve(
|
||||
'path/to/template1.yaml',
|
||||
)}'; caused by Validation error: Invalid enum value`,
|
||||
);
|
||||
});
|
||||
|
||||
@@ -133,7 +133,7 @@ describe('loadTemplate', () => {
|
||||
).rejects.toThrow(
|
||||
`Failed to load template contents from '${mockDir.resolve(
|
||||
'path/to/template1',
|
||||
)}'; caused by Error: Template directory does not exist`,
|
||||
)}'`,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -20,18 +20,17 @@ import { resolve as resolvePath } from 'path';
|
||||
import { dirname } from 'node:path';
|
||||
import { parse as parseYaml } from 'yaml';
|
||||
import { paths } from '../../paths';
|
||||
import { NewTemplatePointer } from '../types';
|
||||
import { NewTemplatePointer, TEMPLATE_ROLES } from '../types';
|
||||
import { NewTemplate } from '../types';
|
||||
import { ForwardedError } from '@backstage/errors';
|
||||
import { fromZodError } from 'zod-validation-error';
|
||||
import { PackageRoles } from '@backstage/cli-node';
|
||||
|
||||
const templateDefinitionSchema = z
|
||||
.object({
|
||||
description: z.string().optional(),
|
||||
template: z.string(),
|
||||
targetPath: z.string(),
|
||||
role: z.string(),
|
||||
role: z.enum(TEMPLATE_ROLES),
|
||||
prompts: z
|
||||
.array(
|
||||
z.union([
|
||||
@@ -74,20 +73,11 @@ export async function loadTemplate({
|
||||
);
|
||||
}
|
||||
|
||||
const { template, role: rawRole, ...templateData } = parsed.data;
|
||||
const { template, ...templateData } = parsed.data;
|
||||
|
||||
const templatePath = resolvePath(dirname(target), template);
|
||||
|
||||
try {
|
||||
const { role } = PackageRoles.getRoleInfo(rawRole);
|
||||
|
||||
if (!fs.existsSync(templatePath)) {
|
||||
throw new Error('Template directory does not exist');
|
||||
}
|
||||
return { id, templatePath, role, ...templateData };
|
||||
} catch (error) {
|
||||
throw new ForwardedError(
|
||||
`Failed to load template contents from '${templatePath}'`,
|
||||
error,
|
||||
);
|
||||
if (!fs.existsSync(templatePath)) {
|
||||
throw new Error(`Failed to load template contents from '${templatePath}'`);
|
||||
}
|
||||
return { id, templatePath, ...templateData };
|
||||
}
|
||||
|
||||
@@ -14,8 +14,6 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { PackageRole } from '@backstage/cli-node';
|
||||
|
||||
export type NewConfig = {
|
||||
/**
|
||||
* The pointers to templates that can be used.
|
||||
@@ -49,12 +47,27 @@ export type NewTemplatePrompt =
|
||||
}
|
||||
| string;
|
||||
|
||||
export const TEMPLATE_ROLES = [
|
||||
'web-library',
|
||||
'node-library',
|
||||
'common-library',
|
||||
'plugin-web-library',
|
||||
'plugin-node-library',
|
||||
'plugin-common-library',
|
||||
'frontend-plugin',
|
||||
'frontend-plugin-module',
|
||||
'backend-plugin',
|
||||
'backend-plugin-module',
|
||||
] as const;
|
||||
|
||||
export type TemplateRole = (typeof TEMPLATE_ROLES)[number];
|
||||
|
||||
export interface NewTemplate {
|
||||
id: string;
|
||||
description?: string;
|
||||
templatePath: string;
|
||||
targetPath: string;
|
||||
role: PackageRole;
|
||||
role: TemplateRole;
|
||||
prompts?: NewTemplatePrompt[];
|
||||
additionalActions?: string[];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user