-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for named imports in ESM (#1440)
* Add esm wrapper and conditional exports * createCommand is an implict export, make explicit * Use deep link instead of experimental conditional * Add mjs to lint * Add sanity checks for esm imports * Create class instances in test * Add option so test-esm works for node 10 through 15 * Remove stale comment * Condense wrapper * Move esm to declaring program section of README * Add esm and TypeScript to program section * fix typo
- Loading branch information
1 parent
7ab9d3d
commit 37825b3
Showing
5 changed files
with
67 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import commander from './index.js'; | ||
|
||
// wrapper to provide named exports for ESM. | ||
export const { program, Option, Command, CommanderError, InvalidOptionArgumentError, Help, createCommand } = commander; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { program, Command, Option, CommanderError, InvalidOptionArgumentError, Help, createCommand } from '../esm.mjs'; | ||
|
||
// Do some simple checks that expected imports are available. | ||
// Run using `npm run test-esm`. | ||
|
||
function check(condition, explanation) { | ||
if (!condition) { | ||
console.log(`Failed assertion: ${explanation}`); | ||
process.exit(2); | ||
} | ||
} | ||
|
||
function checkClass(obj, name) { | ||
console.log(`Checking ${name}`); | ||
check(typeof obj === 'object', `new ${name}() produces object`); | ||
check(obj.constructor.name === name, `object constructor is ${name}`); | ||
} | ||
|
||
console.log('Checking program'); | ||
check(typeof program === 'object', 'program is object'); | ||
check(program.constructor.name === 'Command', 'program is class Command'); | ||
|
||
checkClass(new Command(), 'Command'); | ||
checkClass(new Option('-e, --example'), 'Option'); | ||
checkClass(new CommanderError(1, 'code', 'failed'), 'CommanderError'); | ||
checkClass(new InvalidOptionArgumentError('failed'), 'InvalidOptionArgumentError'); | ||
checkClass(new Help(), 'Help'); | ||
|
||
console.log('Checking createCommand'); | ||
check(typeof createCommand === 'function', 'createCommand is function'); | ||
|
||
console.log('No problems'); |