Skip to content
Closed
12 changes: 9 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@ const { Option } = require('./lib/option.js');
* Expose the root command.
*/

exports = module.exports = new Command();
exports.program = exports; // More explicit access to global command.
// Implicit export of createArgument, createCommand, and createOption.
const program = new Command();
exports = module.exports = program; // default export (deprecated)
exports.program = program; // more explicit access to global command

// Support aggregated import (import * as commander) in TypeScript.
// Do not delete these lines even if they seem redundant!
exports.createCommand = program.createCommand;
exports.createArgument = program.createArgument;
exports.createOption = program.createOption;

/**
* Expose classes
Expand Down
66 changes: 41 additions & 25 deletions tests/ts-imports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,54 @@ function checkClass(obj: object, name: string): void {
expect(obj.constructor.name).toEqual(name);
}

test('legacy default export of global Command', () => {
checkClass(commander, 'Command');
describe('commander', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
describe('commander', () => {
describe('import * as commander', () => {

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Or you were calling it aggregated import which would be ok too.)

test.each([
'program',
'createCommand',
'createArgument',
'createOption',
'CommanderError',
'InvalidArgumentError',
'InvalidOptionArgumentError',
'Command',
'Argument',
'Option',
'Help'
])('has %s', (key) => {
expect(commander).toHaveProperty(key);
});
});

test('program', () => {
checkClass(program, 'Command');
});
describe('class name', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
describe('class name', () => {
describe('named imports', () => {

test('program', () => {
checkClass(program, 'Command');
});

test('createCommand', () => {
checkClass(createCommand(), 'Command');
});
test('createCommand', () => {
checkClass(createCommand(), 'Command');
});

test('Command', () => {
checkClass(new Command('name'), 'Command');
});
test('Command', () => {
checkClass(new Command('name'), 'Command');
});

test('Option', () => {
checkClass(new Option('-e, --example', 'description'), 'Option');
});
test('Option', () => {
checkClass(new Option('-e, --example', 'description'), 'Option');
});

test('CommanderError', () => {
checkClass(new CommanderError(1, 'code', 'failed'), 'CommanderError');
});
test('CommanderError', () => {
checkClass(new CommanderError(1, 'code', 'failed'), 'CommanderError');
});

test('InvalidArgumentError', () => {
checkClass(new InvalidArgumentError('failed'), 'InvalidArgumentError');
});
test('InvalidArgumentError', () => {
checkClass(new InvalidArgumentError('failed'), 'InvalidArgumentError');
});

test('InvalidOptionArgumentError', () => { // Deprecated
checkClass(new InvalidOptionArgumentError('failed'), 'InvalidArgumentError');
});
test('InvalidOptionArgumentError', () => { // Deprecated
checkClass(new InvalidOptionArgumentError('failed'), 'InvalidArgumentError');
});

test('Help', () => {
checkClass(new Help(), 'Help');
test('Help', () => {
checkClass(new Help(), 'Help');
});
});
34 changes: 18 additions & 16 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"types": [
"node",
"jest"
],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"include": ["**/*.ts"],
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"types": [
"node",
"jest"
],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"include": [
"**/*.ts"
],
}