diff --git a/.changeset/tender-garlics-report.md b/.changeset/tender-garlics-report.md new file mode 100644 index 0000000000..919af4d4f8 --- /dev/null +++ b/.changeset/tender-garlics-report.md @@ -0,0 +1,5 @@ +--- +'@backstage/cli': patch +--- + +Removed the experimental `package fix` command that was used to automatically add dependencies to `package.json`, but has since been replaced by the `no-undeclared-imports` rule from `@backstage/eslint-plugin`. diff --git a/packages/cli/src/commands/fix.ts b/packages/cli/src/commands/fix.ts deleted file mode 100644 index 263d171fd6..0000000000 --- a/packages/cli/src/commands/fix.ts +++ /dev/null @@ -1,142 +0,0 @@ -/* - * 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 { paths } from '../lib/paths'; -import { ESLint } from 'eslint'; -import { join as joinPath, basename } from 'path'; -import fs from 'fs-extra'; -import { isChildPath } from '@backstage/cli-common'; -import { PackageGraph } from '@backstage/cli-node'; - -function isTestPath(filePath: string) { - if (!isChildPath(joinPath(paths.targetDir, 'src'), filePath)) { - return true; - } - const name = basename(filePath); - return ( - name.startsWith('setupTests.') || - name.includes('.test.') || - name.includes('.stories.') - ); -} - -export async function command() { - const pkgJsonPath = paths.resolveTarget('package.json'); - const pkg = await fs.readJson(pkgJsonPath); - if (pkg.workspaces) { - throw new Error( - 'Adding dependencies to the workspace root is not supported', - ); - } - - const packages = await PackageGraph.listTargetPackages(); - const localPackageVersions = new Map( - packages.map(p => [p.packageJson.name, p.packageJson.version]), - ); - - const eslint = new ESLint({ - cwd: paths.targetDir, - overrideConfig: { - plugins: ['monorepo'], - rules: { - 'import/no-extraneous-dependencies': [ - 'error', - { - devDependencies: [ - `!${joinPath(paths.targetDir, 'src/**')}`, - joinPath(paths.targetDir, 'src/**/*.test.*'), - joinPath(paths.targetDir, 'src/**/*.stories.*'), - joinPath(paths.targetDir, 'src/setupTests.*'), - ], - optionalDependencies: true, - peerDependencies: true, - bundledDependencies: true, - }, - ], - }, - }, - extensions: ['jsx', 'ts', 'tsx', 'mjs', 'cjs'], - }); - - const results = await eslint.lintFiles(['.']); - - const addedDeps = new Set(); - const addedDevDeps = new Set(); - const removedDevDeps = new Set(); - - for (const result of results) { - for (const message of result.messages) { - // Just in case - if (message.ruleId !== 'import/no-extraneous-dependencies') { - continue; - } - - const match = message.message.match(/^'([^']*)' should be listed/); - if (!match) { - continue; - } - const packageName = match[1]; - if (!localPackageVersions.has(packageName)) { - continue; - } - - if (message.message.endsWith('not devDependencies.')) { - addedDeps.add(packageName); - removedDevDeps.add(packageName); - } else if (isTestPath(result.filePath)) { - addedDevDeps.add(packageName); - } else { - addedDeps.add(packageName); - } - } - } - - if (addedDeps.size || addedDevDeps.size || removedDevDeps.size) { - for (const name of addedDeps) { - if (!pkg.dependencies) { - pkg.dependencies = {}; - } - pkg.dependencies[name] = `^${localPackageVersions.get(name)}`; - } - for (const name of addedDevDeps) { - if (!pkg.devDependencies) { - pkg.devDependencies = {}; - } - pkg.devDependencies[name] = `^${localPackageVersions.get(name)}`; - } - for (const name of removedDevDeps) { - delete pkg.devDependencies[name]; - } - if (Object.keys(pkg.devDependencies).length === 0) { - delete pkg.devDependencies; - } - - if (pkg.dependencies) { - pkg.dependencies = Object.fromEntries( - Object.entries(pkg.dependencies).sort(([a], [b]) => a.localeCompare(b)), - ); - } - if (pkg.devDependencies) { - pkg.devDependencies = Object.fromEntries( - Object.entries(pkg.devDependencies).sort(([a], [b]) => - a.localeCompare(b), - ), - ); - } - - await fs.writeJson(pkgJsonPath, pkg, { spaces: 2 }); - } -} diff --git a/packages/cli/src/commands/index.ts b/packages/cli/src/commands/index.ts index a7f65588a5..0a461ba78c 100644 --- a/packages/cli/src/commands/index.ts +++ b/packages/cli/src/commands/index.ts @@ -159,12 +159,6 @@ export function registerScriptCommand(program: Command) { .description('Run tests, forwarding args to Jest, defaulting to watch mode') .action(lazy(() => import('./test').then(m => m.default))); - command - .command('fix', { hidden: true }) - .description('Applies automated fixes to the package. [EXPERIMENTAL]') - .option('--deps', 'Only fix monorepo dependencies in package.json') - .action(lazy(() => import('./fix').then(m => m.command))); - command .command('clean') .description('Delete cache directories')