Skip to content

Commit

Permalink
Merge pull request backstage#28531 from backstage/rugvip/techdocs-cli…
Browse files Browse the repository at this point in the history
…-dev

techdocs-cli: fix dynamic imports
  • Loading branch information
Rugvip authored Jan 16, 2025
2 parents 70e2dba + 69f84ac commit c2d07ee
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/tall-scissors-sip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@techdocs/cli': patch
---

Internal update to work with dynamic imports.
32 changes: 22 additions & 10 deletions packages/techdocs-cli/src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -254,7 +254,7 @@ export function registerCommands(program: Command) {
)
.option('-p, --port <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')
Expand Down Expand Up @@ -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<void>>,
type ActionFunc = (...args: any[]) => Promise<void>;
type ActionExports<TModule extends object> = {
[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<TModule extends object>(
moduleLoader: () => Promise<TModule>,
exportName: keyof ActionExports<TModule>,
): (...args: any[]) => Promise<never> {
return async (...args: any[]) => {
try {
const actionFunc = await getActionFunc();
const mod = await moduleLoader();
const actualModule = (
mod as unknown as { default: ActionExports<TModule> }
).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);
}
Expand Down

0 comments on commit c2d07ee

Please sign in to comment.