cli/new: remove ability to define custom prompts

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2025-02-09 13:22:52 +01:00
parent f31a530c42
commit 72bada65ef
4 changed files with 31 additions and 57 deletions
@@ -23,13 +23,21 @@ describe('collectTemplateParams', () => {
config: {
isUsingDefaultTemplates: false,
templatePointers: [],
globals: {},
globals: {
baseVersion: '0.1.0',
license: 'Apache-2.0',
private: true,
packagePrefix: '@internal/',
pluginInfix: 'plugin-',
},
} satisfies PortableTemplateConfig,
template: {
id: 'test',
templatePath: '/test',
targetPath: '/example',
role: 'frontend-plugin' as const,
files: [],
templateValues: {},
},
prefilledParams: {
pluginId: 'test',
@@ -46,11 +54,19 @@ describe('collectTemplateParams', () => {
builtInParams: {
owner: 'me',
},
packageParams: {
packageName: '@internal/plugin-test',
packagePath: 'plugins/test',
},
params: {
license: 'Apache-2.0',
packageName: '@internal/plugin-test',
packageVersion: '0.1.0',
privatePackage: true,
pluginId: 'test',
owner: 'me',
},
globals: {},
globals: baseOptions.config.globals,
});
});
@@ -70,11 +86,19 @@ describe('collectTemplateParams', () => {
builtInParams: {
owner: undefined,
},
packageParams: {
packageName: '@internal/plugin-other',
packagePath: 'plugins/other',
},
params: {
license: 'Apache-2.0',
packageName: '@internal/plugin-other',
packageVersion: '0.1.0',
privatePackage: true,
pluginId: 'other',
owner: undefined,
},
globals: {},
globals: baseOptions.config.globals,
});
});
});
@@ -23,14 +23,11 @@ import {
PortableTemplateInputBuiltInParams,
PortableTemplateInputRoleParams,
PortableTemplateParams,
PortableTemplatePrompt,
PortableTemplateRole,
} from '../types';
import { PortableTemplate } from '../types';
import { resolvePackageParams } from './resolvePackageParams';
const RESERVED_PROMPT_NAMES = ['name', 'pluginId', 'moduleId', 'owner'];
type CollectTemplateParamsOptions = {
config: PortableTemplateConfig;
template: PortableTemplate;
@@ -44,13 +41,11 @@ export async function collectPortableTemplateInput(
const codeOwnersFilePath = await getCodeownersFilePath(paths.targetRoot);
const rolePrompts = getPromptsForRole(template.role);
const prompts = getPromptsForRole(template.role);
const buildInPrompts = codeOwnersFilePath ? [ownerPrompt()] : [];
const templatePrompts = template.prompts?.map(customPrompt) ?? [];
const prompts = [...rolePrompts, ...buildInPrompts, ...templatePrompts];
if (codeOwnersFilePath) {
prompts.push(ownerPrompt());
}
const needsAnswer = [];
const prefilledAnswers = {} as PortableTemplateParams;
@@ -186,37 +181,3 @@ export function ownerPrompt(): DistinctQuestion {
},
};
}
export function customPrompt(prompt: PortableTemplatePrompt): DistinctQuestion {
if (RESERVED_PROMPT_NAMES.includes(prompt.id)) {
throw new Error(
`Prompt ID '${prompt.id}' is reserved and cannot be used in a template`,
);
}
return {
type: 'input',
name: prompt.id,
message: prompt.prompt,
validate: (value: string) => {
if (!value) {
return `Please provide a value for ${prompt.id}`;
} else if (prompt.validate) {
let valid: boolean;
let message: string;
switch (prompt.validate) {
case 'backstage-id':
valid = /^[a-z0-9]+(-[a-z0-9]+)*$/.test(value);
message =
'Value must be lowercase and contain only letters, digits, and dashes.';
break;
default:
throw new Error(
`There is no built-in validator with the following id: ${prompt.validate}`,
);
}
return valid || message;
}
return true;
},
};
}
@@ -36,16 +36,6 @@ const templateDefinitionSchema = z
template: z.string(),
targetPath: z.string(),
role: z.enum(TEMPLATE_ROLES),
prompts: z
.array(
z.object({
id: z.string(),
prompt: z.string(),
validate: z.string().optional(),
default: z.union([z.string(), z.boolean(), z.number()]).optional(),
}),
)
.optional(),
additionalActions: z.array(z.string()).optional(),
templateValues: z.record(z.string()).optional(),
})
-1
View File
@@ -69,7 +69,6 @@ export type PortableTemplate = {
description?: string;
targetPath: string;
role: PortableTemplateRole;
prompts?: PortableTemplatePrompt[];
additionalActions?: string[];
files: PortableTemplateFile[];
templateValues: Record<string, string>;