diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index 9fb909592b..1d78771006 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -26,18 +26,18 @@ const main = (argv: string[]) => { .command('app:build') .description('Build an app for a production release') .option('--stats', 'Write bundle stats to output directory') - .action(lazyAction(() => import('./commands/app/build'), 'default')); + .action(run(() => import('./commands/app/build').then(m => m.default))); program .command('app:serve') .description('Serve an app for local development') .option('--check', 'Enable type checking and linting') - .action(lazyAction(() => import('./commands/app/serve'), 'default')); + .action(run(() => import('./commands/app/serve').then(m => m.default))); program .command('backend:build') .description('Build a backend plugin') - .action(lazyAction(() => import('./commands/backend/build'), 'default')); + .action(run(() => import('./commands/backend/build').then(m => m.default))); program .command('backend:build-image ') @@ -45,7 +45,7 @@ const main = (argv: string[]) => { 'Builds a docker image from the package, with all local deps included', ) .action( - lazyAction(() => import('./commands/backend/buildImage'), 'default'), + run(() => import('./commands/backend/buildImage').then(m => m.default)), ); program @@ -53,22 +53,21 @@ const main = (argv: string[]) => { .description('Start local development server with HMR for the backend') .option('--check', 'Enable type checking and linting') .option('--inspect', 'Enable debugger') - .action(lazyAction(() => import('./commands/backend/dev'), 'default')); + .action(run(() => import('./commands/backend/dev').then(m => m.default))); program .command('app:diff') .option('--check', 'Fail if changes are required') .option('--yes', 'Apply all changes') .description('Diff an existing app with the creation template') - .action(lazyAction(() => import('./commands/app/diff'), 'default')); + .action(run(() => import('./commands/app/diff').then(m => m.default))); program .command('create-plugin') .description('Creates a new plugin in the current repository') .action( - lazyAction( - () => import('./commands/create-plugin/createPlugin'), - 'default', + run(() => + import('./commands/create-plugin/createPlugin').then(m => m.default), ), ); @@ -76,41 +75,40 @@ const main = (argv: string[]) => { .command('remove-plugin') .description('Removes plugin in the current repository') .action( - lazyAction( - () => import('./commands/remove-plugin/removePlugin'), - 'default', + run(() => + import('./commands/remove-plugin/removePlugin').then(m => m.default), ), ); program .command('plugin:build') .description('Build a plugin') - .action(lazyAction(() => import('./commands/plugin/build'), 'default')); + .action(run(() => import('./commands/plugin/build').then(m => m.default))); program .command('plugin:serve') .description('Serves the dev/ folder of a plugin') .option('--check', 'Enable type checking and linting') - .action(lazyAction(() => import('./commands/plugin/serve'), 'default')); + .action(run(() => import('./commands/plugin/serve').then(m => m.default))); program .command('plugin:export') .description('Exports the dev/ folder of a plugin') .option('--stats', 'Write bundle stats to output directory') - .action(lazyAction(() => import('./commands/plugin/export'), 'default')); + .action(run(() => import('./commands/plugin/export').then(m => m.default))); program .command('plugin:diff') .option('--check', 'Fail if changes are required') .option('--yes', 'Apply all changes') .description('Diff an existing plugin with the creation template') - .action(lazyAction(() => import('./commands/plugin/diff'), 'default')); + .action(run(() => import('./commands/plugin/diff').then(m => m.default))); program .command('build') .description('Build a package for publishing') .option('--outputs ', 'List of formats to output [types,cjs,esm]') - .action(lazyAction(() => import('./commands/build'), 'default')); + .action(run(() => import('./commands/build').then(m => m.default))); program .command('lint') @@ -121,14 +119,14 @@ const main = (argv: string[]) => { ) .option('--fix', 'Attempt to automatically fix violations') .description('Lint a package') - .action(lazyAction(() => import('./commands/lint'), 'default')); + .action(run(() => import('./commands/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(lazyAction(() => import('./commands/testCommand'), 'default')); + .action(run(() => import('./commands/testCommand').then(m => m.default))); program .command('config:print') @@ -142,27 +140,29 @@ const main = (argv: string[]) => { 'Format to print the configuration in, either json or yaml [yaml]', ) .description('Print the app configuration for the current package') - .action(lazyAction(() => import('./commands/config/print'), 'default')); + .action(run(() => import('./commands/config/print').then(m => m.default))); program .command('prepack') .description('Prepares a package for packaging before publishing') - .action(lazyAction(() => import('./commands/pack'), 'pre')); + .action(run(() => import('./commands/pack').then(m => m.pre))); program .command('postpack') .description('Restores the changes made by the prepack command') - .action(lazyAction(() => import('./commands/pack'), 'post')); + .action(run(() => import('./commands/pack').then(m => m.post))); program .command('clean') .description('Delete cache directories') - .action(lazyAction(() => import('./commands/clean/clean'), 'default')); + .action(run(() => import('./commands/clean/clean').then(m => m.default))); program .command('build-workspace ...') .description('Builds a temporary dist workspace from the provided packages') - .action(lazyAction(() => import('./commands/buildWorkspace'), 'default')); + .action( + run(() => import('./commands/buildWorkspace').then(m => m.default)), + ); program.on('command:*', () => { console.log(); @@ -182,16 +182,12 @@ const main = (argv: string[]) => { }; // Wraps an action function so that it always exits and handles errors -function lazyAction( - actionRequireFunc: () => Promise< - { [name in Export]: (...args: T) => Promise } - >, - exportName: Export, -): (...args: T) => Promise { - return async (...args: T) => { +function run( + getActionFunc: () => Promise<(...args: any[]) => Promise>, +): (...args: any[]) => Promise { + return async (...args: any[]) => { try { - const module = await actionRequireFunc(); - const actionFunc = module[exportName]; + const actionFunc = await getActionFunc(); await actionFunc(...args); process.exit(0); } catch (error) {