cli: refactored registration of script and migration commands

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2022-01-23 16:19:45 +01:00
parent 38963aa860
commit 036d95b99b
+101 -61
View File
@@ -18,14 +18,106 @@ import { assertError } from '@backstage/errors';
import { CommanderStatic } from 'commander';
import { exitWithError } from '../lib/errors';
export function registerCommands(program: CommanderStatic) {
const configOption = [
'--config <path>',
'Config files to load instead of app-config.yaml',
(opt: string, opts: string[]) => [...opts, opt],
Array<string>(),
] as const;
const configOption = [
'--config <path>',
'Config files to load instead of app-config.yaml',
(opt: string, opts: string[]) => [...opts, opt],
Array<string>(),
] as const;
export function registerScriptCommand(program: CommanderStatic) {
const command = program
.command('script [command]', { hidden: true })
.description('Lifecycle scripts for Backstage packages [EXPERIMENTAL]');
command
.command('start')
.description('Start a package for local development')
.option(...configOption)
.option('--role <name>', 'Run the command with an explicit package role')
.option('--check', 'Enable type checking and linting if available')
.option('--inspect', 'Enable debugger in Node.js environments')
.option(
'--inspect-brk',
'Enable debugger in Node.js environments, breaking before code starts',
)
.action(lazy(() => import('./start').then(m => m.command)));
command
.command('build')
.description('Build a package for publishing')
.option('--minify', 'Minify the generated code')
.option('--experimental-type-build', 'Enable experimental type build')
.action(lazy(() => import('./build').then(m => m.command)));
command
.command('bundle')
.description('Bundle a package for deployment')
.option(...configOption)
.option('--role <name>', 'Run the command with an explicit package role')
.option(
'--skip-build-dependencies',
'Skip the automatic building of local dependencies',
)
.option(
'--stats',
'If bundle stats are available, write them to the output directory',
)
.action(lazy(() => import('./bundle').then(m => m.command)));
program
.command('lint')
.option(
'--format <format>',
'Lint report output format',
'eslint-formatter-friendly',
)
.option('--fix', 'Attempt to automatically fix violations')
.description('Lint a package')
.action(lazy(() => import('./lint').then(m => m.default)));
program
.command('test')
.allowUnknownOption(true) // Allows the command to run, but we still need to parse raw args
.helpOption(', --backstage-cli-help') // Let Jest handle help
.description('Run tests, forwarding args to Jest, defaulting to watch mode')
.action(lazy(() => import('./testCommand').then(m => m.default)));
command
.command('clean')
.description('Delete cache directories')
.action(lazy(() => import('./clean/clean').then(m => m.default)));
command
.command('prepack')
.description('Prepares a package for packaging before publishing')
.action(lazy(() => import('./pack').then(m => m.pre)));
command
.command('postpack')
.description('Restores the changes made by the prepack command')
.action(lazy(() => import('./pack').then(m => m.post)));
}
export function registerMigrateCommand(program: CommanderStatic) {
const command = program
.command('migrate [command]', { hidden: true })
.description('Migration utilities [EXPERIMENTAL]');
command
.command('package-role')
.description(`Add package role field to packages that don't have it`)
.action(lazy(() => import('./migrate/packageRole').then(m => m.default)));
command
.command('package-scripts')
.description('Set package scripts according to each package role')
.action(
lazy(() => import('./migrate/packageScripts').then(m => m.command)),
);
}
export function registerCommands(program: CommanderStatic) {
program
.command('app:build')
.description('Build an app for a production release')
@@ -205,60 +297,8 @@ export function registerCommands(program: CommanderStatic) {
.description('Print configuration schema')
.action(lazy(() => import('./config/schema').then(m => m.default)));
const script = program
.command('script [command]', { hidden: true })
.description('Lifecycle scripts for Backstage packages [EXPERIMENTAL]');
script
.command('bundle')
.description('Bundle a package for deployment')
.option(...configOption)
.option('--role <name>', 'Run the command with an explicit package role')
.option(
'--skip-build-dependencies',
'Skip the automatic building of local dependencies',
)
.option(
'--stats',
'If bundle stats are available, write them to the output directory',
)
.action(lazy(() => import('./bundle').then(m => m.command)));
script
.command('build')
.description('Build a package for publishing')
.option('--minify', 'Minify the generated code')
.option('--experimental-type-build', 'Enable experimental type build')
.action(lazy(() => import('./build').then(m => m.command)));
script
.command('start')
.description('Start a package for local development')
.option(...configOption)
.option('--role <name>', 'Run the command with an explicit package role')
.option('--check', 'Enable type checking and linting if available')
.option('--inspect', 'Enable debugger in Node.js environments')
.option(
'--inspect-brk',
'Enable debugger in Node.js environments, breaking before code starts',
)
.action(lazy(() => import('./start').then(m => m.command)));
const migrate = program
.command('migrate [command]', { hidden: true })
.description('Migration utilities [EXPERIMENTAL]');
migrate
.command('package-role')
.description(`Add package role field to packages that don't have it`)
.action(lazy(() => import('./migrate/packageRole').then(m => m.default)));
migrate
.command('package-scripts')
.description('Set package scripts according to each package role')
.action(
lazy(() => import('./migrate/packageScripts').then(m => m.command)),
);
registerScriptCommand(program);
registerMigrateCommand(program);
program
.command('versions:bump')