cli/new: start collecting package role from templates
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
@@ -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`,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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 };
|
||||
}
|
||||
|
||||
@@ -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[];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user