Move all prompt-related helpers into one file
Signed-off-by: Min Kim <minkimcello@gmail.com>
This commit is contained in:
@@ -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'));
|
||||
|
||||
@@ -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<string, string>;
|
||||
codeOwnersFilePath: string | undefined;
|
||||
}): Promise<Record<string, string>> {
|
||||
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 };
|
||||
}
|
||||
|
||||
@@ -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<string, string>;
|
||||
codeOwnersFilePath: string | undefined;
|
||||
}): Promise<Record<string, string>> {
|
||||
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<string, string | boolean> {
|
||||
id: string;
|
||||
private: boolean;
|
||||
|
||||
Reference in New Issue
Block a user