Skip to content

Commit 4ceeb5e

Browse files
authored
Improve ESM and CJS support when importing modules in CLI (#430)
* Better error handling * Better ESM and CJS support when importing modules * Add changeset
1 parent 6e1890a commit 4ceeb5e

File tree

12 files changed

+52
-35
lines changed

12 files changed

+52
-35
lines changed

.changeset/heavy-moles-smell.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'codama': patch
3+
'@codama/errors': patch
4+
'@codama/cli': patch
5+
---
6+
7+
Improve ESM and CJS support when importing modules in CLI

packages/cli/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"@codama/renderers-rust": "workspace:*",
4545
"@codama/visitors": "workspace:*",
4646
"@codama/visitors-core": "workspace:*",
47-
"chalk": "^5.4.1",
47+
"chalk": "^4.1.2",
4848
"commander": "^13.1.0",
4949
"prompts": "^2.4.2"
5050
},

packages/cli/src/utils/import.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,11 @@ async function importLocalModule<T extends object>(identifier: string, modulePat
4747
const dotIndex = modulePath.lastIndexOf('.');
4848
const extension = dotIndex === -1 ? undefined : modulePath.slice(dotIndex);
4949
const modulePromise = extension === '.json' ? import(modulePath, { with: { type: 'json' } }) : import(modulePath);
50-
51-
try {
52-
return (await modulePromise) as unknown as T;
53-
} catch (error) {
54-
throw new Error(`Failed to import ${identifier} at "${modulePath}" as a local module`, { cause: error });
55-
}
50+
return await handleImportPromise(modulePromise, identifier, modulePath);
5651
}
5752

5853
async function importExternalModule<T extends object>(identifier: string, modulePath: string): Promise<T> {
59-
try {
60-
return (await import(modulePath)) as unknown as T;
61-
} catch (error) {
62-
throw new Error(`Failed to import ${identifier} at "${modulePath}" as a module`, { cause: error });
63-
}
54+
return await handleImportPromise(import(modulePath), identifier, modulePath);
6455
}
6556

6657
async function importExternalUserModule<T extends object>(identifier: string, modulePath: string): Promise<T> {
@@ -69,3 +60,22 @@ async function importExternalUserModule<T extends object>(identifier: string, mo
6960
const userModulePath = userRequire.resolve(modulePath);
7061
return await importExternalModule<T>(identifier, userModulePath);
7162
}
63+
64+
async function handleImportPromise<T extends object>(
65+
importPromise: Promise<unknown>,
66+
identifier: string,
67+
modulePath: string,
68+
): Promise<T> {
69+
try {
70+
return (await importPromise) as T;
71+
} catch (error) {
72+
let causeMessage =
73+
!!error && typeof error === 'object' && 'message' in error && typeof error.message === 'string'
74+
? (error as { message: string }).message
75+
: undefined;
76+
causeMessage = causeMessage ? ` (caused by: ${causeMessage})` : '';
77+
throw new Error(`Failed to import ${identifier} at "${modulePath}" as a module${causeMessage}`, {
78+
cause: error,
79+
});
80+
}
81+
}

packages/cli/src/utils/logs.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ export function logError(...args: unknown[]): void {
1616
console.log(chalk.red('[Error]'), ...args);
1717
}
1818

19+
export function logDebug(...args: unknown[]): void {
20+
console.log(chalk.magenta('[Debug]'), ...args);
21+
}
22+
1923
export function logBanner(): void {
2024
console.log(getBanner());
2125
}

packages/errors/bin/cli.cjs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env -S node
2+
3+
const run = require('../dist/cli.cjs').run;
4+
5+
run(process.argv);

packages/errors/bin/cli.mjs

Lines changed: 0 additions & 7 deletions
This file was deleted.

packages/errors/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
"dependencies": {
5656
"@codama/node-types": "workspace:*",
5757
"commander": "^13.1.0",
58-
"chalk": "^5.4.1"
58+
"chalk": "^4.1.2"
5959
},
6060
"license": "MIT",
6161
"repository": {

packages/internals/tsup.config.cli.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import { getBuildConfig } from './tsup.config.base';
44

55
export default defineConfig([
66
{
7-
...getBuildConfig({ format: 'esm', platform: 'node' }),
7+
...getBuildConfig({ format: 'cjs', platform: 'node' }),
88
entry: { cli: './src/cli/index.ts' },
99
outExtension() {
10-
return { js: `.mjs` };
10+
return { js: `.cjs` };
1111
},
1212
},
1313
]);

packages/library/bin/cli.cjs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env -S node
2+
3+
const run = require('../dist/cli.cjs').run;
4+
5+
run(process.argv);

packages/library/bin/cli.mjs

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)