diff --git a/packages/cli/src/commands/new/new.ts b/packages/cli/src/commands/new/new.ts index 8acb93327e..321c643b45 100644 --- a/packages/cli/src/commands/new/new.ts +++ b/packages/cli/src/commands/new/new.ts @@ -20,26 +20,26 @@ import { join as joinPath } from 'path'; import camelCase from 'lodash/camelCase'; import upperFirst from 'lodash/upperFirst'; import { isMonoRepo } from '@backstage/cli-node'; -import { paths } from '../../lib/paths'; import { assertError } from '@backstage/errors'; +import { paths } from '../../lib/paths'; import { Task } from '../../lib/tasks'; import { addCodeownersEntry, getCodeownersFilePath, } from '../../lib/codeowners'; import { resolvePackageName } from '../../lib/new/util'; +import { promptOptions } from '../../lib/new/prompts'; +import { runAdditionalActions } from '../../lib/new/additionalActions'; import { executePluginPackageTemplate } from '../../lib/new/executePluginPackageTemplate'; import { readCliConfig, templateSelector, verifyTemplate, - promptOptions, populateOptions, createDirName, } from '../../lib/new/utils'; -import { runAdditionalActions } from '../../lib/new/additionalActions'; export default async () => { const pkgJson = await fs.readJson(paths.resolveTargetRoot('package.json')); diff --git a/packages/cli/src/lib/new/prompts.ts b/packages/cli/src/lib/new/prompts.ts index 0bc4f9205d..d4b433818f 100644 --- a/packages/cli/src/lib/new/prompts.ts +++ b/packages/cli/src/lib/new/prompts.ts @@ -14,7 +14,8 @@ * limitations under the License. */ -import { Prompt } from './types'; +import inquirer from 'inquirer'; +import { Prompt, ConfigurablePrompt } from './types'; import { parseOwnerIds } from '../codeowners'; export function pluginIdPrompt(): Prompt<{ id: string }> { @@ -87,3 +88,65 @@ export function ownerPrompt( }, }; } + +export async function promptOptions({ + prompts, + globals, + codeOwnersFilePath, +}: { + prompts: ConfigurablePrompt[]; + globals: Record; + codeOwnersFilePath: string | undefined; +}): Promise> { + const answers = await inquirer.prompt( + prompts.map((prompt: ConfigurablePrompt) => { + if (typeof prompt === 'string') { + switch (prompt) { + case 'id': + return pluginIdPrompt(); + case 'moduleid': + return moduleIdIdPrompt(); + case 'npmregistry': + return npmRegistryPrompt(); + case 'owner': + return ownerPrompt(codeOwnersFilePath); + default: + throw new Error( + `There is no built-in prompt with the following id: ${prompt}`, + ); + } + } + return { + type: 'input', + name: prompt.id, + message: prompt.prompt, + default: + globals[prompt.id] !== undefined + ? globals[prompt.id] + : prompt.default, + validate: (value: string) => { + if (!value) { + return `Please provide a value for ${prompt.id}`; + } else if (prompt.validate) { + let valid: boolean; + let message: string; + switch (prompt.validate) { + case 'backstage-id': + valid = /^[a-z0-9]+(-[a-z0-9]+)*$/.test(value); + message = + 'Value must be lowercase and contain only letters, digits, and dashes.'; + break; + default: + throw new Error( + `There is no built-in validator with the following id: ${prompt.validate}`, + ); + } + return valid || message; + } + return true; + }, + }; + }), + ); + return { ...globals, ...answers }; +} diff --git a/packages/cli/src/lib/new/utils.ts b/packages/cli/src/lib/new/utils.ts index cc1cdab90a..2fce001105 100644 --- a/packages/cli/src/lib/new/utils.ts +++ b/packages/cli/src/lib/new/utils.ts @@ -19,16 +19,10 @@ import { parse } from 'yaml'; import fs from 'fs-extra'; import { paths } from '../paths'; -import { - pluginIdPrompt, - moduleIdIdPrompt, - npmRegistryPrompt, - ownerPrompt, -} from './prompts'; import defaultTemplates from '../../../templates/all-default-templates'; -import { Template, TemplateLocation, ConfigurablePrompt } from './types'; +import { Template, TemplateLocation } from './types'; export async function readCliConfig( cliConfig: @@ -102,68 +96,6 @@ export async function verifyTemplate({ return { id, templatePath, ...template }; } -export async function promptOptions({ - prompts, - globals, - codeOwnersFilePath, -}: { - prompts: ConfigurablePrompt[]; - globals: Record; - codeOwnersFilePath: string | undefined; -}): Promise> { - const answers = await inquirer.prompt( - prompts.map((prompt: ConfigurablePrompt) => { - if (typeof prompt === 'string') { - switch (prompt) { - case 'id': - return pluginIdPrompt(); - case 'moduleid': - return moduleIdIdPrompt(); - case 'npmregistry': - return npmRegistryPrompt(); - case 'owner': - return ownerPrompt(codeOwnersFilePath); - default: - throw new Error( - `There is no built-in prompt with the following id: ${prompt}`, - ); - } - } - return { - type: 'input', - name: prompt.id, - message: prompt.prompt, - default: - globals[prompt.id] !== undefined - ? globals[prompt.id] - : prompt.default, - validate: (value: string) => { - if (!value) { - return `Please provide a value for ${prompt.id}`; - } else if (prompt.validate) { - let valid: boolean; - let message: string; - switch (prompt.validate) { - case 'backstage-id': - valid = /^[a-z0-9]+(-[a-z0-9]+)*$/.test(value); - message = - 'Value must be lowercase and contain only letters, digits, and dashes.'; - break; - default: - throw new Error( - `There is no built-in validator with the following id: ${prompt.validate}`, - ); - } - return valid || message; - } - return true; - }, - }; - }), - ); - return { ...globals, ...answers }; -} - interface Options extends Record { id: string; private: boolean;