cli: update alpha entry to support dynamic imports
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
@@ -24,6 +24,6 @@ import chalk from 'chalk';
|
||||
),
|
||||
);
|
||||
const initializer = new CliInitializer();
|
||||
initializer.add(import('./modules/config/alpha').then(m => m.default));
|
||||
initializer.add(import('./modules/config/alpha'));
|
||||
await initializer.run();
|
||||
})();
|
||||
|
||||
@@ -32,7 +32,7 @@ export default createCliPlugin({
|
||||
'Only include the schema that applies to the given package',
|
||||
)
|
||||
.description('Browse the configuration reference documentation')
|
||||
.action(lazy(() => import('./commands/docs').then(m => m.default)));
|
||||
.action(lazy(() => import('./commands/docs'), 'default'));
|
||||
|
||||
await defaultCommand.parseAsync(args, { from: 'user' });
|
||||
},
|
||||
|
||||
@@ -32,7 +32,7 @@ export function registerCommands(program: Command) {
|
||||
'Only include the schema that applies to the given package',
|
||||
)
|
||||
.description('Browse the configuration reference documentation')
|
||||
.action(lazy(() => import('./commands/docs').then(m => m.default)));
|
||||
.action(lazy(() => import('./commands/docs'), 'default'));
|
||||
|
||||
program
|
||||
.command('config:print')
|
||||
@@ -49,7 +49,7 @@ export function registerCommands(program: Command) {
|
||||
)
|
||||
.option(...configOption)
|
||||
.description('Print the app configuration for the current package')
|
||||
.action(lazy(() => import('./commands/print').then(m => m.default)));
|
||||
.action(lazy(() => import('./commands/print'), 'default'));
|
||||
|
||||
program
|
||||
.command('config:check')
|
||||
@@ -68,7 +68,7 @@ export function registerCommands(program: Command) {
|
||||
.description(
|
||||
'Validate that the given configuration loads and matches schema',
|
||||
)
|
||||
.action(lazy(() => import('./commands/validate').then(m => m.default)));
|
||||
.action(lazy(() => import('./commands/validate'), 'default'));
|
||||
|
||||
program
|
||||
.command('config:schema')
|
||||
@@ -83,5 +83,5 @@ export function registerCommands(program: Command) {
|
||||
.option('--merge', 'Print the config schemas merged', true)
|
||||
.option('--no-merge', 'Print the config schemas not merged')
|
||||
.description('Print configuration schema')
|
||||
.action(lazy(() => import('./commands/schema').then(m => m.default)));
|
||||
.action(lazy(() => import('./commands/schema'), 'default'));
|
||||
}
|
||||
|
||||
@@ -22,16 +22,23 @@ import { version } from '../lib/version';
|
||||
import chalk from 'chalk';
|
||||
import { exitWithError } from '../lib/errors';
|
||||
import { assertError } from '@backstage/errors';
|
||||
import { isPromise } from 'util/types';
|
||||
|
||||
type UninitializedFeature = CliFeature | Promise<CliFeature>;
|
||||
type UninitializedFeature = CliFeature | Promise<{ default: CliFeature }>;
|
||||
|
||||
export class CliInitializer {
|
||||
private graph = new CommandGraph();
|
||||
private commandRegistry = new CommandRegistry(this.graph);
|
||||
#uninitiazedFeatures: Promise<CliFeature>[] = [];
|
||||
|
||||
add(module: UninitializedFeature) {
|
||||
this.#uninitiazedFeatures.push(Promise.resolve(module));
|
||||
add(feature: UninitializedFeature) {
|
||||
if (isPromise(feature)) {
|
||||
this.#uninitiazedFeatures.push(
|
||||
feature.then(f => unwrapFeature(f.default)),
|
||||
);
|
||||
} else {
|
||||
this.#uninitiazedFeatures.push(Promise.resolve(feature));
|
||||
}
|
||||
}
|
||||
|
||||
async #register(feature: CliFeature) {
|
||||
@@ -136,3 +143,22 @@ function isCliPlugin(feature: CliFeature): feature is InternalCliPlugin {
|
||||
// Backwards compatibility for v1 registrations that use duck typing
|
||||
return 'plugin' in internal;
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
export function unwrapFeature(
|
||||
feature: CliFeature | { default: CliFeature },
|
||||
): CliFeature {
|
||||
if ('$$type' in feature) {
|
||||
return feature;
|
||||
}
|
||||
|
||||
// This is a workaround where default exports get transpiled to `exports['default'] = ...`
|
||||
// in CommonJS modules, which in turn results in a double `{ default: { default: ... } }` nesting
|
||||
// when importing using a dynamic import.
|
||||
// TODO: This is a broader issue than just this piece of code, and should move away from CommonJS.
|
||||
if ('default' in feature) {
|
||||
return feature.default;
|
||||
}
|
||||
|
||||
return feature;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user