diff --git a/.changeset/tall-scissors-sip.md b/.changeset/tall-scissors-sip.md new file mode 100644 index 0000000000..7d4cdc7059 --- /dev/null +++ b/.changeset/tall-scissors-sip.md @@ -0,0 +1,5 @@ +--- +'@techdocs/cli': patch +--- + +Internal update to work with dynamic imports. diff --git a/packages/techdocs-cli/src/commands/index.ts b/packages/techdocs-cli/src/commands/index.ts index 955dcca24e..253a2900fd 100644 --- a/packages/techdocs-cli/src/commands/index.ts +++ b/packages/techdocs-cli/src/commands/index.ts @@ -81,7 +81,7 @@ export function registerCommands(program: Command) { false, ) .alias('build') - .action(lazy(() => import('./generate/generate').then(m => m.default))); + .action(lazy(() => import('./generate/generate'), 'default')); program .command('migrate') @@ -143,7 +143,7 @@ export function registerCommands(program: Command) { '25', ) .option('-v --verbose', 'Enable verbose output.', false) - .action(lazy(() => import('./migrate/migrate').then(m => m.default))); + .action(lazy(() => import('./migrate/migrate'), 'default')); program .command('publish') @@ -225,7 +225,7 @@ export function registerCommands(program: Command) { 'Path of the directory containing generated files to publish', './site/', ) - .action(lazy(() => import('./publish/publish').then(m => m.default))); + .action(lazy(() => import('./publish/publish'), 'default')); program .command('serve:mkdocs') @@ -254,7 +254,7 @@ export function registerCommands(program: Command) { ) .option('-p, --port ', 'Port to serve documentation locally', '8000') .option('-v --verbose', 'Enable verbose output.', false) - .action(lazy(() => import('./serve/mkdocs').then(m => m.default))); + .action(lazy(() => import('./serve/mkdocs'), 'default')); program .command('serve') @@ -323,21 +323,33 @@ export function registerCommands(program: Command) { ); } }) - .action(lazy(() => import('./serve/serve').then(m => m.default))); + .action(lazy(() => import('./serve/serve'), 'default')); } -// Wraps an action function so that it always exits and handles errors // Humbly taken from backstage-cli's registerCommands -function lazy( - getActionFunc: () => Promise<(...args: any[]) => Promise>, +type ActionFunc = (...args: any[]) => Promise; +type ActionExports = { + [KName in keyof TModule as TModule[KName] extends ActionFunc + ? KName + : never]: TModule[KName]; +}; + +// Wraps an action function so that it always exits and handles errors +export function lazy( + moduleLoader: () => Promise, + exportName: keyof ActionExports, ): (...args: any[]) => Promise { return async (...args: any[]) => { try { - const actionFunc = await getActionFunc(); + const mod = await moduleLoader(); + const actualModule = ( + mod as unknown as { default: ActionExports } + ).default; + const actionFunc = actualModule[exportName] as ActionFunc; await actionFunc(...args); + process.exit(0); } catch (error) { - // eslint-disable-next-line no-console console.error(error.message); process.exit(1); }