From 84936dbcbe9fac0244c71ca90da7404a5e5d2a78 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Fri, 12 Nov 2021 12:03:23 +0100 Subject: [PATCH] cli: cmd options and discovery for additional creation context Signed-off-by: Patrik Oldsberg --- packages/cli/src/commands/create/create.ts | 37 +++++++++++++++++-- packages/cli/src/commands/index.ts | 6 +++ .../lib/create/factories/frontendPlugin.ts | 8 ++-- packages/cli/src/lib/create/types.ts | 15 +++++++- 4 files changed, 59 insertions(+), 7 deletions(-) diff --git a/packages/cli/src/commands/create/create.ts b/packages/cli/src/commands/create/create.ts index 4c1ca68557..07378579d2 100644 --- a/packages/cli/src/commands/create/create.ts +++ b/packages/cli/src/commands/create/create.ts @@ -14,8 +14,11 @@ * limitations under the License. */ +import fs from 'fs-extra'; import { Command } from 'commander'; import { FactoryRegistry } from '../../lib/create/FactoryRegistry'; +import { paths } from '../../lib/paths'; +import { assertError } from '@backstage/errors'; function parseOptions(optionStrings: string[]): Record { const options: Record = {}; @@ -35,13 +38,41 @@ function parseOptions(optionStrings: string[]): Record { } export default async (cmd: Command) => { - const factory = await FactoryRegistry.interactiveSelect(cmd.opts().select); + const cmdOpts = cmd.opts(); - const providedOptions = parseOptions(cmd.opts().option); + const factory = await FactoryRegistry.interactiveSelect(cmdOpts.select); + + const providedOptions = parseOptions(cmdOpts.option); const options = await FactoryRegistry.populateOptions( factory, providedOptions, ); - await factory.create(options); + const rootPackageJson = await fs.readJson( + paths.resolveTargetRoot('package.json'), + ); + const isMonoRepo = Boolean(rootPackageJson.workspaces); + + let defaultVersion = '0.1.0'; + try { + const rootLernaJson = await fs.readJson( + paths.resolveTargetRoot('lerna.json'), + ); + if (rootLernaJson.version) { + defaultVersion = rootLernaJson.version; + } + } catch (error) { + assertError(error); + if (error.code !== 'ENOENT') { + throw error; + } + } + + await factory.create(options, { + isMonoRepo, + defaultVersion, + scope: cmdOpts.scope.replace(/^@/, ''), + npmRegistry: cmdOpts.npmRegistry, + private: Boolean(cmdOpts.private), + }); }; diff --git a/packages/cli/src/commands/index.ts b/packages/cli/src/commands/index.ts index 06832c466b..a37d5e9ffe 100644 --- a/packages/cli/src/commands/index.ts +++ b/packages/cli/src/commands/index.ts @@ -91,6 +91,12 @@ export function registerCommands(program: CommanderStatic) { (opt, arr: string[]) => [...arr, opt], [], ) + .option('--scope ', 'The scope to use for new packages') + .option( + '--npm-registry ', + 'The package registry to use for new packages', + ) + .option('--no-private', 'Do not mark new packages as private') .action(lazy(() => import('./create/create').then(m => m.default))); program diff --git a/packages/cli/src/lib/create/factories/frontendPlugin.ts b/packages/cli/src/lib/create/factories/frontendPlugin.ts index 35e18b9e09..1b13cff13d 100644 --- a/packages/cli/src/lib/create/factories/frontendPlugin.ts +++ b/packages/cli/src/lib/create/factories/frontendPlugin.ts @@ -16,7 +16,7 @@ import { paths } from '../../../lib/paths'; import { getCodeownersFilePath, parseOwnerIds } from '../../../lib/codeowners'; -import { createFactory } from '../types'; +import { createFactory, CreateContext } from '../types'; type Options = { id: string; @@ -63,9 +63,11 @@ export const frontendPlugin = createFactory({ }, }, ], - async create(options: Options) { + async create(options: Options, context: CreateContext) { console.log( - `Creating ${this.name} with options ${JSON.stringify(options)}`, + `Creating ${this.name} with options ${JSON.stringify( + options, + )} and context ${JSON.stringify(context)}`, ); }, }); diff --git a/packages/cli/src/lib/create/types.ts b/packages/cli/src/lib/create/types.ts index a797664e37..82bdd4978a 100644 --- a/packages/cli/src/lib/create/types.ts +++ b/packages/cli/src/lib/create/types.ts @@ -16,6 +16,19 @@ import { DistinctQuestion } from 'inquirer'; +export interface CreateContext { + /** The package scope to use for new packages */ + scope?: string; + /** The NPM registry to use for new packages */ + npmRegistry?: string; + /** Whether new packages should be marked as private */ + private: boolean; + /** Whether we are creating something in a monorepo or not */ + isMonoRepo: boolean; + /** The default version to use for new packages */ + defaultVersion: string; +} + export type AnyOptions = Record; export interface Factory { @@ -23,7 +36,7 @@ export interface Factory { description: string; optionsDiscovery?(): Promise>; optionsPrompts?: ReadonlyArray & { name: string }>; - create(options: Options): Promise; + create(options: Options, context?: CreateContext): Promise; } export type AnyFactory = Factory;