From 38963aa860feced0673f514d00d901667d3c6d9b Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Sun, 23 Jan 2022 15:47:53 +0100 Subject: [PATCH] cli: add new role-based build command Signed-off-by: Patrik Oldsberg --- packages/cli/src/commands/build/command.ts | 52 +++++++++++++++++++ packages/cli/src/commands/build/index.ts | 17 ++++++ packages/cli/src/commands/index.ts | 9 +++- .../src/commands/{build.ts => oldBuild.ts} | 0 4 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 packages/cli/src/commands/build/command.ts create mode 100644 packages/cli/src/commands/build/index.ts rename packages/cli/src/commands/{build.ts => oldBuild.ts} (100%) diff --git a/packages/cli/src/commands/build/command.ts b/packages/cli/src/commands/build/command.ts new file mode 100644 index 0000000000..733faa9480 --- /dev/null +++ b/packages/cli/src/commands/build/command.ts @@ -0,0 +1,52 @@ +/* + * Copyright 2020 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { Command } from 'commander'; +import { buildPackage, Output } from '../../lib/builder'; +import { PackageRoleName, readRoleForCommand } from '../../lib/role'; + +const bundledRoles: PackageRoleName[] = ['app', 'backend']; + +const esmPlatforms = ['web', 'common']; +const cjsPlatforms = ['node', 'common']; + +export async function command(cmd: Command): Promise { + const roleInfo = await readRoleForCommand(cmd); + + if (bundledRoles.includes(roleInfo.role)) { + throw new Error( + `Build command is not supported for package role '${roleInfo.role}'`, + ); + } + + const outputs = new Set(); + + if (cjsPlatforms.includes(roleInfo.platform)) { + outputs.add(Output.cjs); + } + if (esmPlatforms.includes(roleInfo.platform)) { + outputs.add(Output.esm); + } + if (roleInfo.role !== 'cli') { + outputs.add(Output.types); + } + + await buildPackage({ + outputs, + minify: Boolean(cmd.minify), + useApiExtractor: Boolean(cmd.experimentalTypeBuild), + }); +} diff --git a/packages/cli/src/commands/build/index.ts b/packages/cli/src/commands/build/index.ts new file mode 100644 index 0000000000..680fe9e11d --- /dev/null +++ b/packages/cli/src/commands/build/index.ts @@ -0,0 +1,17 @@ +/* + * Copyright 2022 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export { command } from './command'; diff --git a/packages/cli/src/commands/index.ts b/packages/cli/src/commands/index.ts index caeee1557c..ea0f7dec58 100644 --- a/packages/cli/src/commands/index.ts +++ b/packages/cli/src/commands/index.ts @@ -131,7 +131,7 @@ export function registerCommands(program: CommanderStatic) { .option('--outputs ', 'List of formats to output [types,cjs,esm]') .option('--minify', 'Minify the generated code') .option('--experimental-type-build', 'Enable experimental type build') - .action(lazy(() => import('./build').then(m => m.default))); + .action(lazy(() => import('./oldBuild').then(m => m.default))); program .command('lint') @@ -224,6 +224,13 @@ export function registerCommands(program: CommanderStatic) { ) .action(lazy(() => import('./bundle').then(m => m.command))); + script + .command('build') + .description('Build a package for publishing') + .option('--minify', 'Minify the generated code') + .option('--experimental-type-build', 'Enable experimental type build') + .action(lazy(() => import('./build').then(m => m.command))); + script .command('start') .description('Start a package for local development') diff --git a/packages/cli/src/commands/build.ts b/packages/cli/src/commands/oldBuild.ts similarity index 100% rename from packages/cli/src/commands/build.ts rename to packages/cli/src/commands/oldBuild.ts