cli: added migrate react-router-deps

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2022-08-22 15:35:30 +02:00
parent 31f8638f30
commit 3a975e1989
2 changed files with 66 additions and 0 deletions
+9
View File
@@ -180,6 +180,15 @@ export function registerMigrateCommand(program: Command) {
.action(
lazy(() => import('./migrate/packageLintConfigs').then(m => m.command)),
);
command
.command('react-router-deps')
.description(
'Migrates the react-router dependencies for all packages to be peer dependencies',
)
.action(
lazy(() => import('./migrate/reactRouterDeps').then(m => m.command)),
);
}
export function registerCommands(program: Command) {
@@ -0,0 +1,57 @@
/*
* 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 { getRoleFromPackage } from '../../lib/role';
const REACT_ROUTER_DEPS = ['react-router', 'react-router-dom'];
const REACT_ROUTER_RANGE = '6.0.0-beta.0 || ^6.3.0';
export async function command() {
const packages = await PackageGraph.listTargetPackages();
await Promise.all(
packages.map(async ({ dir, packageJson }) => {
const role = getRoleFromPackage(packageJson);
if (role === 'frontend') {
console.log(`Skipping ${packageJson.name}`);
return;
}
let changed = false;
if (packageJson.dependencies) {
for (const key of Object.keys(packageJson.dependencies)) {
if (REACT_ROUTER_DEPS.includes(key)) {
delete packageJson.dependencies[key];
const peerDeps = (packageJson.peerDependencies =
packageJson.peerDependencies ?? {});
peerDeps[key] = REACT_ROUTER_RANGE;
changed = true;
}
}
}
if (changed) {
console.log(`Updating dependencies for ${packageJson.name}`);
await fs.writeJson(resolvePath(dir, 'package.json'), packageJson, {
spaces: 2,
});
}
}),
);
}