From e0833d2fd86109b6bffdfd0ba65ca4fd081f9b97 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Thu, 6 Feb 2025 23:42:05 +0100 Subject: [PATCH] cli/commands/new: restore ability to pre-fill options and set globals via args Signed-off-by: Patrik Oldsberg --- packages/cli/src/commands/index.ts | 20 ++++++++++++++ packages/cli/src/commands/new/new.ts | 39 +++++++++++++++++++++++++--- packages/cli/src/lib/new/utils.ts | 21 ++++++++------- 3 files changed, 68 insertions(+), 12 deletions(-) diff --git a/packages/cli/src/commands/index.ts b/packages/cli/src/commands/index.ts index c5784524d8..f1119053ec 100644 --- a/packages/cli/src/commands/index.ts +++ b/packages/cli/src/commands/index.ts @@ -251,6 +251,26 @@ export function registerCommands(program: Command) { '--select ', 'Select the thing you want to be creating upfront', ) + .option( + '--option =', + 'Pre-fill options for the creation process', + (opt, arr: string[]) => [...arr, opt], + [], + ) + .option('--scope ', 'The scope to use for new packages') + .option( + '--npm-registry ', + 'The package registry to use for new packages', + ) + .option( + '--baseVersion ', + 'The version to use for any new packages (default: 0.1.0)', + ) + .option( + '--license ', + 'The license to use for any new packages (default: Apache-2.0)', + ) + .option('--no-private', 'Do not mark new packages as private') .action(lazy(() => import('./new/new'), 'default')); registerConfigCommands(program); diff --git a/packages/cli/src/commands/new/new.ts b/packages/cli/src/commands/new/new.ts index b611e76786..c2e12e4edf 100644 --- a/packages/cli/src/commands/new/new.ts +++ b/packages/cli/src/commands/new/new.ts @@ -43,6 +43,23 @@ import { executePluginPackageTemplate } from '../../lib/new/executeTemplate'; import { TemporaryDirectoryManager } from './TemporaryDirectoryManager'; import { OptionValues } from 'commander'; +function parseOptions(optionStrings: string[]): Record { + const options: Record = {}; + + for (const str of optionStrings) { + const [key] = str.split('=', 1); + const value = str.slice(key.length + 1); + if (!key || str[key.length] !== '=') { + throw new Error( + `Invalid option '${str}', must be of the format =`, + ); + } + options[key] = value; + } + + return options; +} + export default async (opts: OptionValues) => { const pkgJson = await fs.readJson(paths.resolveTargetRoot('package.json')); const cliConfig = pkgJson.backstage?.cli; @@ -54,12 +71,28 @@ export default async (opts: OptionValues) => { const codeOwnersFilePath = await getCodeownersFilePath(paths.targetRoot); - const prompts = await promptOptions({ - prompts: template.prompts || [], + const legacyOpts = parseOptions(opts.option); + const prefilledAnswers = Object.fromEntries( + (template.prompts ?? []).flatMap(prompt => { + const id = typeof prompt === 'string' ? prompt : prompt.id; + const answer = legacyOpts[id]; + return answer ? [[id, answer]] : []; + }), + ); + const promptAnswers = await promptOptions({ + prompts: + template.prompts?.filter( + prompt => + !Object.hasOwn( + prefilledAnswers, + typeof prompt === 'string' ? prompt : prompt.id, + ), + ) ?? [], globals, codeOwnersFilePath, }); - const options = populateOptions(prompts, template); + const answers = { ...prefilledAnswers, ...promptAnswers }; + const options = populateOptions(answers, template, opts); const tmpDirManager = TemporaryDirectoryManager.create(); diff --git a/packages/cli/src/lib/new/utils.ts b/packages/cli/src/lib/new/utils.ts index 51448a5a80..47069b3d13 100644 --- a/packages/cli/src/lib/new/utils.ts +++ b/packages/cli/src/lib/new/utils.ts @@ -13,6 +13,7 @@ * 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 { @@ -50,19 +51,21 @@ export const resolvePackageName = (options: { }; export function populateOptions( - prompts: Record, + options: Record, template: Template, + argOpts?: OptionValues, ): Options { return { - id: prompts.id ?? '', - private: !!prompts.private, - baseVersion: prompts.baseVersion ?? '0.1.0', - owner: prompts.owner ?? '', - license: prompts.license ?? 'Apache-2.0', + id: '', + owner: '', + license: 'Apache-2.0', + scope: '', + moduleId: '', + baseVersion: '0.1.0', + private: true, + ...options, + ...argOpts, targetPath: template.targetPath, - scope: prompts.scope ?? '', - moduleId: prompts.moduleId ?? '', - ...prompts, }; }