cli/new: refactor arg options parsing + fixes

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2025-02-07 10:03:51 +01:00
parent 0adc2ec5ea
commit 47a257f09d
3 changed files with 24 additions and 13 deletions
+19 -5
View File
@@ -41,7 +41,6 @@ import {
import { runAdditionalActions } from '../../lib/new/additionalActions';
import { executePluginPackageTemplate } from '../../lib/new/executeTemplate';
import { TemporaryDirectoryManager } from './TemporaryDirectoryManager';
import { OptionValues } from 'commander';
function parseOptions(optionStrings: string[]): Record<string, string> {
const options: Record<string, string> = {};
@@ -60,18 +59,33 @@ function parseOptions(optionStrings: string[]): Record<string, string> {
return options;
}
export default async (opts: OptionValues) => {
type ArgOptions = {
option: string[];
select?: string;
private?: boolean;
npmRegistry?: string;
scope?: string;
license?: string;
baseVersion?: string;
};
export default async ({
option: argOptions,
select: preselectedTemplateId,
...argGlobals
}: ArgOptions) => {
const pkgJson = await fs.readJson(paths.resolveTargetRoot('package.json'));
const cliConfig = pkgJson.backstage?.cli;
const { templates, globals } = readCliConfig(cliConfig);
const template = verifyTemplate(
await templateSelector(templates, opts.select),
await templateSelector(templates, preselectedTemplateId),
);
const codeOwnersFilePath = await getCodeownersFilePath(paths.targetRoot);
const legacyOpts = parseOptions(opts.option);
const legacyOpts = parseOptions(argOptions);
const prefilledAnswers = Object.fromEntries(
(template.prompts ?? []).flatMap(prompt => {
const id = typeof prompt === 'string' ? prompt : prompt.id;
@@ -92,7 +106,7 @@ export default async (opts: OptionValues) => {
codeOwnersFilePath,
});
const answers = { ...prefilledAnswers, ...promptAnswers };
const options = populateOptions(answers, template, opts);
const options = populateOptions({ ...answers, ...argGlobals }, template);
const tmpDirManager = TemporaryDirectoryManager.create();
+3 -3
View File
@@ -88,7 +88,7 @@ describe('populateOptions', () => {
expect(populateOptions({}, { targetPath: '/example' } as Template)).toEqual(
{
id: '',
private: false,
private: true,
baseVersion: '0.1.0',
owner: '',
license: 'Apache-2.0',
@@ -106,7 +106,7 @@ describe('populateOptions', () => {
} as Template),
).toEqual({
id: '',
private: false,
private: true,
baseVersion: '0.1.0',
owner: '',
license: 'Apache-2.0',
@@ -124,7 +124,7 @@ describe('populateOptions', () => {
} as Template),
).toEqual({
id: '',
private: false,
private: true,
baseVersion: '0.1.0',
owner: '',
license: 'Apache-2.0',
+2 -5
View File
@@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { OptionValues } from 'commander';
import { Template } from './types';
export interface Options extends Record<string, string | boolean> {
@@ -51,9 +50,8 @@ export const resolvePackageName = (options: {
};
export function populateOptions(
options: Record<string, string>,
options: { [name in string]?: string | boolean },
template: Template,
argOpts?: OptionValues,
): Options {
return {
id: '',
@@ -63,9 +61,8 @@ export function populateOptions(
moduleId: '',
baseVersion: '0.1.0',
private: true,
...options,
...argOpts,
targetPath: template.targetPath,
...options,
};
}