diff --git a/packages/cli/src/commands/index.ts b/packages/cli/src/commands/index.ts index aca238a32b..def16900fa 100644 --- a/packages/cli/src/commands/index.ts +++ b/packages/cli/src/commands/index.ts @@ -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) { diff --git a/packages/cli/src/commands/migrate/reactRouterDeps.ts b/packages/cli/src/commands/migrate/reactRouterDeps.ts new file mode 100644 index 0000000000..d8fa100c50 --- /dev/null +++ b/packages/cli/src/commands/migrate/reactRouterDeps.ts @@ -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, + }); + } + }), + ); +}