diff --git a/packages/cli/cli-report.md b/packages/cli/cli-report.md index 32918caad2..00f3ce937d 100644 --- a/packages/cli/cli-report.md +++ b/packages/cli/cli-report.md @@ -200,10 +200,10 @@ Commands: start [options] build [options] test - lint [options] [directories...] clean prepack postpack + lint [options] [directories...] help [command] ``` diff --git a/packages/cli/src/commands/index.ts b/packages/cli/src/commands/index.ts index 9a76711d08..2349a9fb22 100644 --- a/packages/cli/src/commands/index.ts +++ b/packages/cli/src/commands/index.ts @@ -35,6 +35,10 @@ import { registerPackageCommands as registerPackageLintCommands, registerRepoCommands as registerRepoLintCommands, } from '../modules/lint'; +import { + registerPackageCommands as registerMaintenancePackageCommands, + registerRepoCommands as registerMaintenanceRepoCommands, +} from '../modules/maintenance'; export function registerRepoCommand(program: Command) { const command = program @@ -44,30 +48,7 @@ export function registerRepoCommand(program: Command) { registerRepoBuildCommands(command); registerRepoTestCommands(command); registerRepoLintCommands(command); - - command - .command('fix') - .description('Automatically fix packages in the project') - .option( - '--publish', - 'Enable additional fixes that only apply when publishing packages', - ) - .option( - '--check', - 'Fail if any packages would have been changed by the command', - ) - .action(lazy(() => import('./repo/fix'), 'command')); - - command - .command('clean') - .description('Delete cache and output directories') - .action(lazy(() => import('./repo/clean'), 'command')); - - command - .command('list-deprecations') - .description('List deprecations') - .option('--json', 'Output as JSON') - .action(lazy(() => import('./repo/list-deprecations'), 'command')); + registerMaintenanceRepoCommands(command); } export function registerScriptCommand(program: Command) { @@ -92,22 +73,8 @@ export function registerScriptCommand(program: Command) { registerPackageBuildCommands(command); registerPackageTestCommands(command); - + registerMaintenancePackageCommands(command); registerPackageLintCommands(command); - command - .command('clean') - .description('Delete cache directories') - .action(lazy(() => import('./clean/clean'), 'default')); - - command - .command('prepack') - .description('Prepares a package for packaging before publishing') - .action(lazy(() => import('./pack'), 'pre')); - - command - .command('postpack') - .description('Restores the changes made by the prepack command') - .action(lazy(() => import('./pack'), 'post')); } export function registerCommands(program: Command) { @@ -153,7 +120,6 @@ export function registerCommands(program: Command) { registerMigrateCommand(program); registerBuildCommands(program); registerInfoCommands(program); - program .command('create-github-app ') .description('Create new GitHub App in your organization.') diff --git a/packages/cli/src/modules/maintenance/alpha.ts b/packages/cli/src/modules/maintenance/alpha.ts new file mode 100644 index 0000000000..41192afdc4 --- /dev/null +++ b/packages/cli/src/modules/maintenance/alpha.ts @@ -0,0 +1,110 @@ +/* + * Copyright 2025 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 { Command } from 'commander'; +import { createCliPlugin } from '../../wiring/factory'; +import { lazy } from '../../lib/lazy'; + +export default createCliPlugin({ + pluginId: 'maintenance', + init: async reg => { + reg.addCommand({ + path: ['package', 'clean'], + description: 'Delete cache directories', + execute: async ({ args }) => { + const command = new Command(); + const defaultCommand = command.action( + lazy(() => import('./commands/package/clean'), 'default'), + ); + + await defaultCommand.parseAsync(args, { from: 'user' }); + }, + }); + + reg.addCommand({ + path: ['package', 'prepack'], + description: 'Prepares a package for packaging before publishing', + execute: async ({ args }) => { + const command = new Command(); + const defaultCommand = command.action( + lazy(() => import('./commands/package/pack'), 'pre'), + ); + + await defaultCommand.parseAsync(args, { from: 'user' }); + }, + }); + + reg.addCommand({ + path: ['package', 'postpack'], + description: 'Restores the changes made by the prepack command', + execute: async ({ args }) => { + const command = new Command(); + const defaultCommand = command.action( + lazy(() => import('./commands/package/pack'), 'post'), + ); + + await defaultCommand.parseAsync(args, { from: 'user' }); + }, + }); + + reg.addCommand({ + path: ['repo', 'fix'], + description: 'Automatically fix packages in the project', + execute: async ({ args }) => { + const command = new Command(); + const defaultCommand = command + .option( + '--publish', + 'Enable additional fixes that only apply when publishing packages', + ) + .option( + '--check', + 'Fail if any packages would have been changed by the command', + ) + .action(lazy(() => import('./commands/repo/fix'), 'command')); + + await defaultCommand.parseAsync(args, { from: 'user' }); + }, + }); + + reg.addCommand({ + path: ['repo', 'clean'], + description: 'Delete cache and output directories', + execute: async ({ args }) => { + const command = new Command(); + const defaultCommand = command.action( + lazy(() => import('./commands/repo/clean'), 'command'), + ); + + await defaultCommand.parseAsync(args, { from: 'user' }); + }, + }); + + reg.addCommand({ + path: ['repo', 'list-deprecations'], + description: 'List deprecations', + execute: async ({ args }) => { + const command = new Command(); + const defaultCommand = command + .option('--json', 'Output as JSON') + .action( + lazy(() => import('./commands/repo/list-deprecations'), 'command'), + ); + + await defaultCommand.parseAsync(args, { from: 'user' }); + }, + }); + }, +}); diff --git a/packages/cli/src/commands/clean/clean.ts b/packages/cli/src/modules/maintenance/commands/package/clean.ts similarity index 94% rename from packages/cli/src/commands/clean/clean.ts rename to packages/cli/src/modules/maintenance/commands/package/clean.ts index 74a1ea658b..5e2d0d65b3 100644 --- a/packages/cli/src/commands/clean/clean.ts +++ b/packages/cli/src/modules/maintenance/commands/package/clean.ts @@ -15,7 +15,7 @@ */ import fs from 'fs-extra'; -import { paths } from '../../lib/paths'; +import { paths } from '../../../../lib/paths'; export default async function clean() { await fs.remove(paths.resolveTarget('dist')); diff --git a/packages/cli/src/commands/pack.ts b/packages/cli/src/modules/maintenance/commands/package/pack.ts similarity index 80% rename from packages/cli/src/commands/pack.ts rename to packages/cli/src/modules/maintenance/commands/package/pack.ts index 17162b9596..510401c6d1 100644 --- a/packages/cli/src/commands/pack.ts +++ b/packages/cli/src/modules/maintenance/commands/package/pack.ts @@ -17,11 +17,11 @@ import { productionPack, revertProductionPack, -} from '../modules/build/lib/packager/productionPack'; -import { paths } from '../lib/paths'; +} from '../../../../modules/build/lib/packager/productionPack'; +import { paths } from '../../../../lib/paths'; import fs from 'fs-extra'; -import { publishPreflightCheck } from '../lib/publishing'; -import { createTypeDistProject } from '../lib/typeDistProject'; +import { publishPreflightCheck } from '../../../../lib/publishing'; +import { createTypeDistProject } from '../../../../lib/typeDistProject'; export const pre = async () => { publishPreflightCheck({ diff --git a/packages/cli/src/commands/repo/clean.ts b/packages/cli/src/modules/maintenance/commands/repo/clean.ts similarity index 97% rename from packages/cli/src/commands/repo/clean.ts rename to packages/cli/src/modules/maintenance/commands/repo/clean.ts index 13ff061423..51c31129ea 100644 --- a/packages/cli/src/commands/repo/clean.ts +++ b/packages/cli/src/modules/maintenance/commands/repo/clean.ts @@ -19,7 +19,7 @@ import fs from 'fs-extra'; import { resolve as resolvePath } from 'path'; import { promisify } from 'util'; import { PackageGraph } from '@backstage/cli-node'; -import { paths } from '../../lib/paths'; +import { paths } from '../../../../lib/paths'; const execFile = promisify(execFileCb); diff --git a/packages/cli/src/commands/repo/fix.ts b/packages/cli/src/modules/maintenance/commands/repo/fix.ts similarity index 99% rename from packages/cli/src/commands/repo/fix.ts rename to packages/cli/src/modules/maintenance/commands/repo/fix.ts index a55440c53f..83f4da6114 100644 --- a/packages/cli/src/commands/repo/fix.ts +++ b/packages/cli/src/modules/maintenance/commands/repo/fix.ts @@ -24,8 +24,8 @@ import { import { OptionValues } from 'commander'; import fs from 'fs-extra'; import { resolve as resolvePath, posix, relative as relativePath } from 'path'; -import { paths } from '../../lib/paths'; -import { publishPreflightCheck } from '../../lib/publishing'; +import { paths } from '../../../../lib/paths'; +import { publishPreflightCheck } from '../../../../lib/publishing'; /** * A mutable object representing a package.json file with potential fixes. diff --git a/packages/cli/src/commands/repo/list-deprecations.ts b/packages/cli/src/modules/maintenance/commands/repo/list-deprecations.ts similarity index 98% rename from packages/cli/src/commands/repo/list-deprecations.ts rename to packages/cli/src/modules/maintenance/commands/repo/list-deprecations.ts index 772038025a..de83f7a4ac 100644 --- a/packages/cli/src/commands/repo/list-deprecations.ts +++ b/packages/cli/src/modules/maintenance/commands/repo/list-deprecations.ts @@ -19,7 +19,7 @@ import { ESLint } from 'eslint'; import { OptionValues } from 'commander'; import { relative as relativePath } from 'path'; import { PackageGraph } from '@backstage/cli-node'; -import { paths } from '../../lib/paths'; +import { paths } from '../../../../lib/paths'; export async function command(opts: OptionValues) { const packages = await PackageGraph.listTargetPackages(); diff --git a/packages/cli/src/modules/maintenance/index.ts b/packages/cli/src/modules/maintenance/index.ts new file mode 100644 index 0000000000..48669e9c34 --- /dev/null +++ b/packages/cli/src/modules/maintenance/index.ts @@ -0,0 +1,61 @@ +/* + * Copyright 2025 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 { Command } from 'commander'; +import { lazy } from '../../lib/lazy'; + +export function registerPackageCommands(command: Command) { + command + .command('clean') + .description('Delete cache directories') + .action(lazy(() => import('./commands/package/clean'), 'default')); + + command + .command('prepack') + .description('Prepares a package for packaging before publishing') + .action(lazy(() => import('./commands/package/pack'), 'pre')); + + command + .command('postpack') + .description('Restores the changes made by the prepack command') + .action(lazy(() => import('./commands/package/pack'), 'post')); +} + +export function registerRepoCommands(command: Command) { + command + .command('fix') + .description('Automatically fix packages in the project') + .option( + '--publish', + 'Enable additional fixes that only apply when publishing packages', + ) + .option( + '--check', + 'Fail if any packages would have been changed by the command', + ) + .action(lazy(() => import('./commands/repo/fix'), 'command')); + + command + .command('clean') + .description('Delete cache and output directories') + .action(lazy(() => import('./commands/repo/clean'), 'command')); + + command + .command('list-deprecations') + .description('List deprecations') + .option('--json', 'Output as JSON') + .action(lazy(() => import('./commands/repo/list-deprecations'), 'command')); +} diff --git a/packages/cli/src/modules/migrate/commands/packageExports.ts b/packages/cli/src/modules/migrate/commands/packageExports.ts index c54422ceb9..4eb741ce4d 100644 --- a/packages/cli/src/modules/migrate/commands/packageExports.ts +++ b/packages/cli/src/modules/migrate/commands/packageExports.ts @@ -18,7 +18,7 @@ import { fixPackageExports, readFixablePackages, writeFixedPackages, -} from '../../../commands/repo/fix'; +} from '../../maintenance/commands/repo/fix'; export async function command() { console.log(