From 4bfe2110fdb59dbc4892de10a6bb0813ae0d3dac Mon Sep 17 00:00:00 2001 From: Min Kim Date: Tue, 10 Dec 2024 19:39:16 -0500 Subject: [PATCH] Reorganize code Signed-off-by: Min Kim --- packages/cli/src/commands/new/new.ts | 296 +++++------------- packages/cli/src/commands/new/types.ts | 38 +++ packages/cli/src/commands/new/util.ts | 201 ++++++++++++ packages/cli/templates/index.ts | 2 +- .../cli/templates/web-library-package.yaml | 3 - 5 files changed, 317 insertions(+), 223 deletions(-) create mode 100644 packages/cli/src/commands/new/types.ts create mode 100644 packages/cli/src/commands/new/util.ts diff --git a/packages/cli/src/commands/new/new.ts b/packages/cli/src/commands/new/new.ts index 8ef9fb9a1e..2336befb94 100644 --- a/packages/cli/src/commands/new/new.ts +++ b/packages/cli/src/commands/new/new.ts @@ -17,164 +17,20 @@ import os from 'os'; import fs from 'fs-extra'; import { join as joinPath, dirname } from 'path'; -import { OptionValues } from 'commander'; -import inquirer from 'inquirer'; -import { parse } from 'yaml'; import { FactoryRegistry } from '../../lib/new/FactoryRegistry'; import { isMonoRepo } from '@backstage/cli-node'; import { paths } from '../../lib/paths'; import { assertError } from '@backstage/errors'; import { Task } from '../../lib/tasks'; + +import { executePluginPackageTemplate } from '../../lib/new/factories/common/tasks'; import { - pluginIdPrompt, - moduleIdIdPrompt, - // ownerPrompt, // 🚨 WIP -} from '../../lib/new/factories/common/prompts'; -import defaultTemplates from '../../../templates'; - -type ConfigurablePrompt = - | { - id: string; - prompt: string; - type?: string; - validate?: string; - default?: string | boolean; - } - | string; - -interface Template { - description?: string; - template: string; - targetPath: string; - prompts?: ConfigurablePrompt[]; - additionalActions?: string[]; -} - -interface TemplateLocation { - id: string; - target: string; -} - -async function readCliConfig( - cliConfig: - | { - defaults?: boolean; - templates?: TemplateLocation[]; - globals?: Record; - } - | undefined, -) { - let templates: TemplateLocation[] = []; - const cliTemplates = cliConfig?.templates; - - if (!cliConfig || cliConfig?.defaults) { - templates = defaultTemplates; - } - if (cliTemplates?.length) { - cliTemplates.forEach((template: TemplateLocation) => { - templates.push({ - id: template.id, - target: template.target, - }); - }); - } - return { - templates, - globals: { ...cliConfig?.globals }, - }; -} - -async function templateSelector( - templates: TemplateLocation[], -): Promise { - const answer = await inquirer.prompt<{ name: TemplateLocation }>([ - { - type: 'list', - name: 'name', - message: 'What do you want to create?', - choices: templates.map(template => { - return { - name: template.id, - value: template, - }; - }), - }, - ]); - return answer.name; -} - -async function verifyTemplate({ target }: TemplateLocation): Promise