From e6535e0f263f2ce52561206a5a36ec7aa3c0a0cb Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Thu, 6 Feb 2025 23:07:52 +0100 Subject: [PATCH] cli/commands/new: restore ability to preselect template Signed-off-by: Patrik Oldsberg --- packages/cli/src/commands/index.ts | 4 ++++ packages/cli/src/commands/new/new.ts | 7 +++++-- packages/cli/src/lib/new/templateSelector.ts | 8 ++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/packages/cli/src/commands/index.ts b/packages/cli/src/commands/index.ts index 58892d4835..c5784524d8 100644 --- a/packages/cli/src/commands/index.ts +++ b/packages/cli/src/commands/index.ts @@ -247,6 +247,10 @@ export function registerCommands(program: Command) { .description( 'Open up an interactive guide to creating new things in your app', ) + .option( + '--select ', + 'Select the thing you want to be creating upfront', + ) .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 6a8477f487..b611e76786 100644 --- a/packages/cli/src/commands/new/new.ts +++ b/packages/cli/src/commands/new/new.ts @@ -41,13 +41,16 @@ import { import { runAdditionalActions } from '../../lib/new/additionalActions'; import { executePluginPackageTemplate } from '../../lib/new/executeTemplate'; import { TemporaryDirectoryManager } from './TemporaryDirectoryManager'; +import { OptionValues } from 'commander'; -export default async () => { +export default async (opts: OptionValues) => { 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)); + const template = verifyTemplate( + await templateSelector(templates, opts.select), + ); const codeOwnersFilePath = await getCodeownersFilePath(paths.targetRoot); diff --git a/packages/cli/src/lib/new/templateSelector.ts b/packages/cli/src/lib/new/templateSelector.ts index 53bc00bbd8..7b963ad2e6 100644 --- a/packages/cli/src/lib/new/templateSelector.ts +++ b/packages/cli/src/lib/new/templateSelector.ts @@ -47,7 +47,15 @@ export function readCliConfig(cliConfig: CliConfig) { export async function templateSelector( templates: TemplateLocation[], + selected?: string, ): Promise { + if (selected) { + const template = templates.find(t => t.id === selected); + if (!template) { + throw new Error(`Template '${selected}' not found`); + } + return template; + } const answer = await inquirer.prompt<{ name: TemplateLocation }>([ { type: 'list',