cli/new: merge global overrides into config

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2025-02-07 14:37:18 +01:00
parent 8355dd9b92
commit 616be453eb
5 changed files with 18 additions and 17 deletions
@@ -30,7 +30,6 @@ describe('collectTemplateParams', () => {
templatePath: '/test',
targetPath: '/example',
},
globals: {},
prefilledParams: {},
};
@@ -49,7 +48,10 @@ describe('collectTemplateParams', () => {
it('should include all non-standard global and prompt values', async () => {
await expect(
collectTemplateParams({ ...baseOptions, globals: { foo: 'bar' } }),
collectTemplateParams({
...baseOptions,
config: { ...baseOptions.config, globals: { foo: 'bar' } },
}),
).resolves.toEqual({
id: '',
private: true,
@@ -24,7 +24,6 @@ import { Options } from '../execution/utils';
type CollectTemplateParamsOptions = {
config: NewConfig;
template: NewTemplate;
globals: Record<string, string | number | boolean>;
prefilledParams: Record<string, string | number | boolean>;
};
@@ -41,7 +40,7 @@ const defaultParams = {
export async function collectTemplateParams(
options: CollectTemplateParamsOptions,
): Promise<Options> {
const { config, template, globals, prefilledParams } = options;
const { config, template, prefilledParams } = options;
const codeOwnersFilePath = await getCodeownersFilePath(paths.targetRoot);
@@ -64,13 +63,12 @@ export async function collectTemplateParams(
typeof prompt === 'string' ? prompt : prompt.id,
),
) ?? [],
globals: config.globals,
codeOwnersFilePath,
});
return {
...defaultParams,
...globals,
...config.globals,
...prefilledAnswers,
...promptAnswers,
targetPath: template.targetPath,
@@ -95,11 +95,9 @@ export function ownerPrompt(
export async function promptOptions({
prompts,
globals,
codeOwnersFilePath,
}: {
prompts: NewTemplatePrompt[];
globals: { [name in string]?: string | boolean | number };
codeOwnersFilePath: string | undefined;
}): Promise<Record<string, string>> {
const answers = await inquirer.prompt(
@@ -124,10 +122,6 @@ export async function promptOptions({
type: 'input',
name: prompt.id,
message: prompt.prompt,
default:
globals[prompt.id] !== undefined
? globals[prompt.id]
: prompt.default,
validate: (value: string) => {
if (!value) {
return `Please provide a value for ${prompt.id}`;
@@ -152,5 +146,5 @@ export async function promptOptions({
};
}),
);
return { ...globals, ...answers };
return { ...answers };
}
+3 -2
View File
@@ -33,7 +33,9 @@ export type CreateNewPackageOptions = {
};
export async function createNewPackage(options: CreateNewPackageOptions) {
const config = await loadConfig();
const config = await loadConfig({
globalOverrides: options.globals,
});
const selectedTemplate = await selectTemplateInteractively(
config,
@@ -44,7 +46,6 @@ export async function createNewPackage(options: CreateNewPackageOptions) {
const params = await collectTemplateParams({
config,
template,
globals: options.globals,
prefilledParams: options.prefilledParams,
});
@@ -47,7 +47,13 @@ const pkgJsonWithNewConfigSchema = z.object({
.optional(),
});
export async function loadConfig(): Promise<NewConfig> {
type LoadConfigOptions = {
globalOverrides: Record<string, string | number | boolean>;
};
export async function loadConfig(
options: LoadConfigOptions,
): Promise<NewConfig> {
const pkgPath = paths.resolveTargetRoot('package.json');
const pkgJson = await fs.readJson(pkgPath);
@@ -64,6 +70,6 @@ export async function loadConfig(): Promise<NewConfig> {
return {
isUsingDefaultTemplates: !newConfig?.templates,
templatePointers: newConfig?.templates ?? defaultTemplates,
globals: newConfig?.globals ?? {},
globals: { ...newConfig?.globals, ...options.globalOverrides },
};
}