Skip to content

Commit e5e4655

Browse files
authored
fix(marketplace): use new Backstage lazy loading function (#583)
* Revert "fix(marketplace-cli): use CJS imports instead of ESM to fix double-encapsulation import issue (#577)" This reverts commit 5a99fdd. * fix(marketplace-cli): use new Backstage `lazy` loading function
1 parent ae84573 commit e5e4655

3 files changed

Lines changed: 59 additions & 23 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@red-hat-developer-hub/marketplace-cli': patch
3+
---
4+
5+
use new Backstage `lazy` loading function

workspaces/marketplace/packages/cli/src/commands/index.ts

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,13 @@
1414
* limitations under the License.
1515
*/
1616
import { Command } from 'commander';
17-
import { exitWithError } from '../lib/errors';
18-
import { assertError } from '@backstage/errors';
19-
20-
// Wraps an action function so that it always exits and handles errors
21-
function lazy(
22-
getActionFunc: () => Promise<(...args: any[]) => Promise<void>>,
23-
): (...args: any[]) => Promise<never> {
24-
return async (...args: any[]) => {
25-
try {
26-
const actionFunc = await getActionFunc();
27-
await actionFunc(...args);
28-
29-
process.exit(0);
30-
} catch (error) {
31-
assertError(error);
32-
exitWithError(error);
33-
}
34-
};
35-
}
17+
import { lazy } from '../lib/lazy';
3618

3719
export const registerCommands = (program: Command) => {
3820
program
3921
.command('init')
4022
.description('init')
41-
.action(lazy(() => require('./init').default));
23+
.action(lazy(() => import('./init'), 'default'));
4224

4325
program
4426
.command('generate')
@@ -58,14 +40,14 @@ export const registerCommands = (program: Command) => {
5840
'metadata.namespace for the generated Package entities',
5941
)
6042
.option('--owner [owner]', 'spec.owner for the generated Package entities')
61-
.action(lazy(() => require('./generate').default));
43+
.action(lazy(() => import('./generate'), 'default'));
6244

6345
program
6446
.command('verify')
6547
.description(
6648
'Verify a set of marketplace entities. By default, it will read entities from the standard input',
6749
)
68-
.action(lazy(() => require('./verify').default));
50+
.action(lazy(() => import('./verify'), 'default'));
6951

7052
program
7153
.command('export-csv')
@@ -87,5 +69,5 @@ export const registerCommands = (program: Command) => {
8769
'The type of CSV to export. Can be one of: "plugin", "package", or "all". "all" will generate two files.',
8870
'all',
8971
)
90-
.action(lazy(() => require('./export-csv').default));
72+
.action(lazy(() => import('./export-csv'), 'default'));
9173
};
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright The Backstage Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// taken from https://github.com/backstage/backstage/blob/master/packages/cli/src/lib/lazy.ts
18+
19+
import { assertError } from '@backstage/errors';
20+
import { exitWithError } from './errors';
21+
22+
type ActionFunc = (...args: any[]) => Promise<void>;
23+
type ActionExports<TModule extends object> = {
24+
[KName in keyof TModule as TModule[KName] extends ActionFunc
25+
? KName
26+
: never]: TModule[KName];
27+
};
28+
29+
// Wraps an action function so that it always exits and handles errors
30+
export function lazy<TModule extends object>(
31+
moduleLoader: () => Promise<TModule>,
32+
exportName: keyof ActionExports<TModule>,
33+
): (...args: any[]) => Promise<never> {
34+
return async (...args: any[]) => {
35+
try {
36+
const mod = await moduleLoader();
37+
const actualModule = (
38+
mod as unknown as { default: ActionExports<TModule> }
39+
).default;
40+
const actionFunc = actualModule[exportName] as ActionFunc;
41+
await actionFunc(...args);
42+
43+
process.exit(0);
44+
} catch (error) {
45+
assertError(error);
46+
exitWithError(error);
47+
}
48+
};
49+
}

0 commit comments

Comments
 (0)