cli/new: inline prompt factories and move to param collection to preparation

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2025-02-07 16:15:08 +01:00
parent cd3e94e9fc
commit 80ec7909e4
4 changed files with 55 additions and 71 deletions
@@ -1,66 +0,0 @@
/*
* Copyright 2025 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import inquirer from 'inquirer';
import { getCodeownersFilePath } from '../../codeowners';
import { paths } from '../../paths';
import { PortableTemplateConfig } from '../types';
import { customPrompt, getPromptsForRole, ownerPrompt } from './prompts';
import { PortableTemplate } from '../types';
type CollectTemplateParamsOptions = {
config: PortableTemplateConfig;
template: PortableTemplate;
prefilledParams: Record<string, string | number | boolean>;
};
export async function collectPortableTemplateParams(
options: CollectTemplateParamsOptions,
): Promise<Record<string, string | number | boolean>> {
const { config, template, prefilledParams } = options;
const codeOwnersFilePath = await getCodeownersFilePath(paths.targetRoot);
const prompts = getPromptsForRole(template.role);
if (codeOwnersFilePath) {
prompts.push(ownerPrompt());
}
if (template.prompts) {
prompts.push(...template.prompts.map(customPrompt));
}
const needsAnswer = [];
const prefilledAnswers = {} as Record<string, string | number | boolean>;
for (const prompt of prompts) {
if (prompt.name && prefilledParams[prompt.name] !== undefined) {
prefilledAnswers[prompt.name] = prefilledParams[prompt.name];
} else {
needsAnswer.push(prompt);
}
}
const promptAnswers = await inquirer.prompt<
Record<string, string | number | boolean>
>(needsAnswer);
return {
...config.globals,
...prefilledAnswers,
...promptAnswers,
targetPath: template.targetPath,
};
}
+1 -1
View File
@@ -14,7 +14,7 @@
* limitations under the License.
*/
import { collectPortableTemplateParams } from './collection/collectPortableTemplateParams';
import { collectPortableTemplateParams } from './preparation/collectPortableTemplateParams';
import { loadPortableTemplateConfig } from './preparation/loadPortableTemplateConfig';
import { executePortableTemplate } from './execution/executePortableTemplate';
import { selectTemplateInteractively } from './preparation/selectTemplateInteractively';
@@ -1,5 +1,5 @@
/*
* Copyright 2021 The Backstage Authors
* Copyright 2025 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,9 +14,59 @@
* limitations under the License.
*/
import { DistinctQuestion } from 'inquirer';
import { PortableTemplatePrompt, PortableTemplateRole } from '../types';
import { parseOwnerIds } from '../../codeowners';
import inquirer, { DistinctQuestion } from 'inquirer';
import { getCodeownersFilePath, parseOwnerIds } from '../../codeowners';
import { paths } from '../../paths';
import {
PortableTemplateConfig,
PortableTemplatePrompt,
PortableTemplateRole,
} from '../types';
import { PortableTemplate } from '../types';
type CollectTemplateParamsOptions = {
config: PortableTemplateConfig;
template: PortableTemplate;
prefilledParams: Record<string, string | number | boolean>;
};
export async function collectPortableTemplateParams(
options: CollectTemplateParamsOptions,
): Promise<Record<string, string | number | boolean>> {
const { config, template, prefilledParams } = options;
const codeOwnersFilePath = await getCodeownersFilePath(paths.targetRoot);
const prompts = getPromptsForRole(template.role);
if (codeOwnersFilePath) {
prompts.push(ownerPrompt());
}
if (template.prompts) {
prompts.push(...template.prompts.map(customPrompt));
}
const needsAnswer = [];
const prefilledAnswers = {} as Record<string, string | number | boolean>;
for (const prompt of prompts) {
if (prompt.name && prefilledParams[prompt.name] !== undefined) {
prefilledAnswers[prompt.name] = prefilledParams[prompt.name];
} else {
needsAnswer.push(prompt);
}
}
const promptAnswers = await inquirer.prompt<
Record<string, string | number | boolean>
>(needsAnswer);
return {
...config.globals,
...prefilledAnswers,
...promptAnswers,
targetPath: template.targetPath,
};
}
export function namePrompt(): DistinctQuestion {
return {