cli: add migrate:package-role command
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
@@ -233,6 +233,11 @@ export function registerCommands(program: CommanderStatic) {
|
||||
.description('Print configuration schema')
|
||||
.action(lazy(() => import('./config/schema').then(m => m.default)));
|
||||
|
||||
program
|
||||
.command('migrate:package-role')
|
||||
.description(`Add package role field to packages that don't have it`)
|
||||
.action(lazy(() => import('./migrate/packageRole').then(m => m.default)));
|
||||
|
||||
program
|
||||
.command('versions:bump')
|
||||
.option(
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* 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 { getPackages } from '@manypkg/get-packages';
|
||||
import { paths } from '../../lib/paths';
|
||||
import { readPackageRole, detectPackageRole } from '../../lib/role';
|
||||
|
||||
export default async () => {
|
||||
const { packages } = await getPackages(paths.targetDir);
|
||||
|
||||
await Promise.all(
|
||||
packages.map(async ({ dir, packageJson: pkg }) => {
|
||||
const { name } = pkg;
|
||||
const existingRole = readPackageRole(pkg);
|
||||
if (existingRole) {
|
||||
return;
|
||||
}
|
||||
|
||||
const detectedRole = detectPackageRole(pkg);
|
||||
if (!detectedRole) {
|
||||
console.error(`No role detected for package ${name}`);
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`Detected package role of ${name} as ${detectedRole.role}`);
|
||||
|
||||
let newPkg = pkg as any;
|
||||
|
||||
const pkgKeys = Object.keys(pkg);
|
||||
if (pkgKeys.includes('backstage')) {
|
||||
newPkg.backstage = {
|
||||
...newPkg.backstage,
|
||||
role: detectedRole.role,
|
||||
};
|
||||
} else {
|
||||
// We insert the backstage field after one of these fields, otherwise at the end
|
||||
const index =
|
||||
Math.max(
|
||||
pkgKeys.indexOf('version'),
|
||||
pkgKeys.indexOf('private'),
|
||||
pkgKeys.indexOf('publishConfig'),
|
||||
) + 1 || pkgKeys.length;
|
||||
|
||||
const pkgEntries = Object.entries(pkg);
|
||||
pkgEntries.splice(index, 0, ['backstage', { role: detectedRole.role }]);
|
||||
newPkg = Object.fromEntries(pkgEntries);
|
||||
}
|
||||
|
||||
await fs.writeJson(resolvePath(dir, 'package.json'), newPkg, {
|
||||
spaces: 2,
|
||||
});
|
||||
}),
|
||||
);
|
||||
};
|
||||
Reference in New Issue
Block a user