Add install steps and codeowner prompt

Signed-off-by: Min Kim <minkimcello@gmail.com>
This commit is contained in:
Min Kim
2024-12-11 01:31:28 -05:00
parent 3044efb810
commit 565eaa6e98
3 changed files with 56 additions and 23 deletions
+37 -9
View File
@@ -16,12 +16,17 @@
import os from 'os';
import fs from 'fs-extra';
import { join as joinPath, dirname } from 'path';
import { FactoryRegistry } from '../../lib/new/FactoryRegistry';
import { join as joinPath } from 'path';
import { isMonoRepo } from '@backstage/cli-node';
import { paths } from '../../lib/paths';
import { assertError } from '@backstage/errors';
import { Task } from '../../lib/tasks';
import {
addCodeownersEntry,
getCodeownersFilePath,
} from '../../lib/codeowners';
import { resolvePackageName } from '../../lib/new/factories/common/util';
import { executePluginPackageTemplate } from '../../lib/new/factories/common/tasks';
import {
@@ -39,9 +44,12 @@ export default async () => {
const { templates, globals } = await readCliConfig(cliConfig);
const template = await verifyTemplate(await templateSelector(templates));
const codeOwnersFilePath = await getCodeownersFilePath(paths.targetRoot);
const prompts = await promptOptions({
prompts: template.prompts || [],
globals,
codeOwnersFilePath,
});
const options = await populateOptions(prompts, template);
@@ -69,7 +77,11 @@ export default async () => {
targetDir: options.targetDir,
templateDir: template.templatePath,
values: {
name: options.id,
name: resolvePackageName({
baseName: options.id,
scope: options.scope,
plugin: template.plugin ?? true,
}),
pluginVersion: options.baseVersion,
...options,
},
@@ -77,12 +89,28 @@ export default async () => {
);
// create scope prompt
// npmregistry prompt
// incorporate owners prompt
// additional actions
// add to frontend
// add to backend
// install and lint
// double check default template paths
// create additional actions
// install to app
// install to backend
// add to backend/index.ts
if (options.install) {
// 🚨 temporary
if (options.owner) {
await addCodeownersEntry(options.targetDir, options.owner);
}
await Task.forCommand('yarn install', {
cwd: options.targetDir,
optional: true,
});
await Task.forCommand('yarn lint --fix', {
cwd: options.targetDir,
optional: true,
});
}
Task.log();
Task.log(`🎉 Successfully created ${template.id}`);
+15 -9
View File
@@ -104,10 +104,12 @@ export async function verifyTemplate({
export async function promptOptions({
prompts,
globals,
codeOwnersFilePath,
}: {
prompts: ConfigurablePrompt[];
globals: Record<string, string | boolean>;
}): Promise<Record<string, string | boolean>> {
globals: Record<string, string>;
codeOwnersFilePath: string | undefined;
}): Promise<Record<string, string>> {
const answers = await inquirer.prompt(
prompts.map((prompt: ConfigurablePrompt) => {
if (typeof prompt === 'string') {
@@ -119,7 +121,7 @@ export async function promptOptions({
case 'npmregistry':
return npmRegistryPrompt();
case 'owner':
return ownerPrompt();
return ownerPrompt(codeOwnersFilePath);
default:
throw new Error(
`There is no built-in prompt with the following id: ${prompt}`,
@@ -167,6 +169,8 @@ interface Options extends Record<string, string | boolean> {
baseVersion: string;
license: string;
targetDir: string;
owner: string;
scope: string;
}
async function calculateBaseVersion(baseVersion: string) {
@@ -184,18 +188,20 @@ async function calculateBaseVersion(baseVersion: string) {
}
export async function populateOptions(
prompts: Record<string, string | boolean>,
prompts: Record<string, string>,
template: Template,
): Promise<Options> {
return {
id: prompts.id as string,
private: (prompts.private as boolean) ?? false,
baseVersion: await calculateBaseVersion(prompts.baseVersion as string),
license: (prompts.license as string) ?? 'Apache-2.0',
id: prompts.id,
private: false,
baseVersion: await calculateBaseVersion(prompts.baseVersion),
owner: prompts.owner ?? '',
license: prompts.license ?? 'Apache-2.0',
targetDir: paths.resolveTargetRoot(
template.targetPath,
prompts.targetPath ?? template.targetPath,
prompts.id as string,
),
scope: prompts.scope ?? '',
...prompts,
};
}
@@ -65,15 +65,14 @@ export function npmRegistryPrompt(): Prompt<{ npmRegistry: string }> {
};
}
export function ownerPrompt(): Prompt<{
owner?: string;
codeOwnersPath?: string;
}> {
export function ownerPrompt(
codeOwnersPath: string | undefined,
): Prompt<{ owner?: string }> {
return {
type: 'input',
name: 'owner',
message: 'Enter an owner to add to CODEOWNERS [optional]',
when: opts => Boolean(opts.codeOwnersPath),
when: Boolean(codeOwnersPath),
validate: (value: string) => {
if (!value) {
return true;