diff --git a/packages/cli/src/commands/new/new.ts b/packages/cli/src/commands/new/new.ts index 2336befb94..320b1f5772 100644 --- a/packages/cli/src/commands/new/new.ts +++ b/packages/cli/src/commands/new/new.ts @@ -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}`); diff --git a/packages/cli/src/commands/new/util.ts b/packages/cli/src/commands/new/util.ts index e41f2eaf44..5c0918f743 100644 --- a/packages/cli/src/commands/new/util.ts +++ b/packages/cli/src/commands/new/util.ts @@ -104,10 +104,12 @@ export async function verifyTemplate({ export async function promptOptions({ prompts, globals, + codeOwnersFilePath, }: { prompts: ConfigurablePrompt[]; - globals: Record; -}): Promise> { + globals: Record; + codeOwnersFilePath: string | undefined; +}): Promise> { 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 { 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, + prompts: Record, template: Template, ): Promise { 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, }; } diff --git a/packages/cli/src/lib/new/factories/common/prompts.ts b/packages/cli/src/lib/new/factories/common/prompts.ts index 45c933af30..5663ee2c21 100644 --- a/packages/cli/src/lib/new/factories/common/prompts.ts +++ b/packages/cli/src/lib/new/factories/common/prompts.ts @@ -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;