From 9e8d2e5bfac759f83f63df46d12ac20e3d0b244c Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Fri, 7 Feb 2025 15:06:18 +0100 Subject: [PATCH] cli/new: start collecting package role from templates Signed-off-by: Patrik Oldsberg --- .../lib/new/preparation/loadTemplate.test.ts | 42 +++++++++++++++---- .../src/lib/new/preparation/loadTemplate.ts | 24 +++++++---- packages/cli/src/lib/new/types.ts | 6 +-- 3 files changed, 53 insertions(+), 19 deletions(-) diff --git a/packages/cli/src/lib/new/preparation/loadTemplate.test.ts b/packages/cli/src/lib/new/preparation/loadTemplate.test.ts index f8188f2fc9..8380110535 100644 --- a/packages/cli/src/lib/new/preparation/loadTemplate.test.ts +++ b/packages/cli/src/lib/new/preparation/loadTemplate.test.ts @@ -22,9 +22,10 @@ describe('loadTemplate', () => { const mockDir = createMockDirectory({ content: { 'path/to/template1.yaml': ` -template: template1 -targetPath: plugins -`, + template: template1 + targetPath: plugins + role: frontend-plugin + `, 'path/to/template1/hello.txt': 'hello world', }, }); @@ -38,6 +39,7 @@ targetPath: plugins id: 'template1', templatePath: mockDir.resolve('path/to/template1'), targetPath: 'plugins', + role: 'frontend-plugin', }); }); @@ -89,13 +91,14 @@ targetPath: plugins ).rejects.toThrow('Remote templates are not supported yet'); }); - it('should throw an error if template directory does not exist', async () => { + it('should throw an error if the package role is invalid', async () => { const mockDir = createMockDirectory({ content: { 'path/to/template1.yaml': ` - template: template1 - targetPath: plugins - `, + template: template1 + targetPath: plugins + role: invalid-role + `, }, }); @@ -107,7 +110,30 @@ targetPath: plugins ).rejects.toThrow( `Failed to load template contents from '${mockDir.resolve( 'path/to/template1', - )}'`, + )}'; caused by Error: Unknown package role 'invalid-role'`, + ); + }); + + it('should throw an error if template directory does not exist', async () => { + const mockDir = createMockDirectory({ + content: { + 'path/to/template1.yaml': ` + template: template1 + targetPath: plugins + role: frontend-plugin + `, + }, + }); + + await expect( + loadTemplate({ + id: 'template1', + target: mockDir.resolve('path/to/template1.yaml'), + }), + ).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 bb61acdd2b..30ecd1b91b 100644 --- a/packages/cli/src/lib/new/preparation/loadTemplate.ts +++ b/packages/cli/src/lib/new/preparation/loadTemplate.ts @@ -24,15 +24,14 @@ import { NewTemplatePointer } 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(), - plugin: z.boolean().optional(), - backendModulePrefix: z.boolean().optional(), - suffix: z.string().optional(), + role: z.string(), prompts: z .array( z.union([ @@ -75,11 +74,20 @@ export async function loadTemplate({ ); } - const { template, ...templateData } = parsed.data; - + const { template, role: rawRole, ...templateData } = parsed.data; const templatePath = resolvePath(dirname(target), template); - if (!fs.existsSync(templatePath)) { - throw new Error(`Failed to load template contents from '${templatePath}'`); + + 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, + ); } - return { id, templatePath, ...templateData }; } diff --git a/packages/cli/src/lib/new/types.ts b/packages/cli/src/lib/new/types.ts index d6a8350c4b..dd035c4dc1 100644 --- a/packages/cli/src/lib/new/types.ts +++ b/packages/cli/src/lib/new/types.ts @@ -14,6 +14,8 @@ * limitations under the License. */ +import { PackageRole } from '@backstage/cli-node'; + export type NewConfig = { /** * The pointers to templates that can be used. @@ -52,9 +54,7 @@ export interface NewTemplate { description?: string; templatePath: string; targetPath: string; - plugin?: boolean; - backendModulePrefix?: boolean; - suffix?: string; + role: PackageRole; prompts?: NewTemplatePrompt[]; additionalActions?: string[]; }