From d6996fcb6647fd70920870896c93a6e0992e9128 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Fri, 7 Feb 2025 15:30:14 +0100 Subject: [PATCH] cli/new: use separate role definitions for templates Signed-off-by: Patrik Oldsberg --- .../lib/new/preparation/loadTemplate.test.ts | 8 +++---- .../src/lib/new/preparation/loadTemplate.ts | 24 ++++++------------- packages/cli/src/lib/new/types.ts | 19 ++++++++++++--- 3 files changed, 27 insertions(+), 24 deletions(-) diff --git a/packages/cli/src/lib/new/preparation/loadTemplate.test.ts b/packages/cli/src/lib/new/preparation/loadTemplate.test.ts index 8380110535..c653370cdc 100644 --- a/packages/cli/src/lib/new/preparation/loadTemplate.test.ts +++ b/packages/cli/src/lib/new/preparation/loadTemplate.test.ts @@ -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`, + )}'`, ); }); }); diff --git a/packages/cli/src/lib/new/preparation/loadTemplate.ts b/packages/cli/src/lib/new/preparation/loadTemplate.ts index 30ecd1b91b..eaa73e1e83 100644 --- a/packages/cli/src/lib/new/preparation/loadTemplate.ts +++ b/packages/cli/src/lib/new/preparation/loadTemplate.ts @@ -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 }; } diff --git a/packages/cli/src/lib/new/types.ts b/packages/cli/src/lib/new/types.ts index dd035c4dc1..4a91e708cd 100644 --- a/packages/cli/src/lib/new/types.ts +++ b/packages/cli/src/lib/new/types.ts @@ -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[]; }