From 189c44104c05ac3a727cd3002e3813bc746a85d7 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Sun, 23 Jan 2022 13:07:21 +0100 Subject: [PATCH] cli: added migrate:package-scripts Signed-off-by: Patrik Oldsberg --- packages/cli/src/commands/index.ts | 7 ++ .../src/commands/migrate/packageScripts.ts | 70 +++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 packages/cli/src/commands/migrate/packageScripts.ts diff --git a/packages/cli/src/commands/index.ts b/packages/cli/src/commands/index.ts index 61ded0741c..ef2e5ef96a 100644 --- a/packages/cli/src/commands/index.ts +++ b/packages/cli/src/commands/index.ts @@ -238,6 +238,13 @@ export function registerCommands(program: CommanderStatic) { .description(`Add package role field to packages that don't have it`) .action(lazy(() => import('./migrate/packageRole').then(m => m.default))); + program + .command('migrate:package-scripts') + .description('Set package scripts according to each package role') + .action( + lazy(() => import('./migrate/packageScripts').then(m => m.command)), + ); + program .command('versions:bump') .option( diff --git a/packages/cli/src/commands/migrate/packageScripts.ts b/packages/cli/src/commands/migrate/packageScripts.ts new file mode 100644 index 0000000000..58a8481c81 --- /dev/null +++ b/packages/cli/src/commands/migrate/packageScripts.ts @@ -0,0 +1,70 @@ +/* + * 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 fs from 'fs-extra'; +import { resolve as resolvePath } from 'path'; +import { PackageGraph } from '../../lib/monorepo'; +import { readPackageRole, PackageRoleName } from '../../lib/role'; + +const bundledRoles: PackageRoleName[] = ['app', 'backend']; +const noStartRoles: PackageRoleName[] = ['cli', 'common-library']; + +export async function command() { + const packages = await PackageGraph.listTargetPackages(); + + await Promise.all( + packages.map(async ({ dir, packageJson }) => { + const roleInfo = readPackageRole(packageJson); + if (!roleInfo) { + return; + } + + const hasStart = !noStartRoles.includes(roleInfo.role); + const isBundled = bundledRoles.includes(roleInfo.role); + + const expectedScripts = { + ...(hasStart && { start: 'backstage-cli start' }), + ...(isBundled + ? { bundle: 'backstage-cli bundle' } + : { build: 'backstage-cli build' }), + lint: 'backstage-cli lint', + test: 'backstage-cli test', + clean: 'backstage-cli clean', + ...(!isBundled && { + postpack: 'backstage-cli postpack', + prepack: 'backstage-cli prepack', + }), + }; + + let changed = false; + const currentScripts = (packageJson.scripts = packageJson.scripts || {}); + + for (const [name, value] of Object.entries(expectedScripts)) { + if (currentScripts[name] !== value) { + changed = true; + currentScripts[name] = value; + } + } + + if (changed) { + console.log(`Updating scripts for ${packageJson.name}`); + await fs.writeJson(resolvePath(dir, 'package.json'), packageJson, { + spaces: 2, + }); + } + }), + ); +}