Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove default export of global program #2017

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/deprecated.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const program = new Command()
- Removed from README in Commander v5.
- Deprecated from Commander v7.
- Removed from TypeScript declarations in Commander v8.
- Removed from CommonJS in Commander v12. Deprecated and gone!

## Callback to .help() and .outputHelp()

Expand Down
10 changes: 4 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ const { Option } = require('./lib/option.js');

// @ts-check

/**
* Expose the root command.
*/
exports.program = new Command();

exports = module.exports = new Command();
exports.program = exports; // More explicit access to global command.
// Implicit export of createArgument, createCommand, and createOption.
exports.createCommand = (name) => new Command(name);
exports.createOption = (flags, description) => new Option(flags, description);
exports.createArgument = (name, description) => new Argument(name, description);

/**
* Expose classes
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures-extensions/pm.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node

const program = require('../../');
const { program } = require('../../');

program
.command('try-ts', 'test file extension lookup')
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/inspect.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node

const program = require('../../');
const { program } = require('../../');

program
.command('sub', 'install one or more packages')
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/pm
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node

var program = require('../../');
var { program } = require('../../');

program
.version('0.0.1')
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/pm-cache.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const program = require('../../');
const { program } = require('../../');

program
.command('clear', 'clear the cache')
Expand Down
71 changes: 59 additions & 12 deletions tests/program.test.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,67 @@
const commander = require('../');
const {
program,
Command,
Option,
Argument,
Help,
CommanderError,
InvalidArgumentError,
InvalidOptionArgumentError,
createCommand,
createOption,
createArgument
} = require('../index.js');

// Do some testing of the default export(s).
// Similar tests to ts-imports.test.ts and esm-imports-test.js.

test('when require commander then is a Command (default export of global)', () => {
// Deprecated global command
const program = commander;
expect(program.constructor.name).toBe('Command');
/* eslint jest/expect-expect: ["error", { "assertFunctionNames": ["expect", "checkClass"] }] */

function checkClass(obj, name) {
expect(typeof obj).toEqual('object');
expect(obj.constructor.name).toEqual(name);
}

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

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

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

test('Argument', () => {
checkClass(new Argument('<foo>', 'description'), 'Argument');
});

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

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

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

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

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

test('when require commander then has program (named export of global)', () => {
// program added in v5
const program = commander.program;
expect(program.constructor.name).toBe('Command');
test('createOption', () => {
checkClass(createOption('-e, --example', 'description'), 'Option');
});

test('when require commander then has newable Command', () => {
const cmd = new commander.Command();
expect(cmd.constructor.name).toBe('Command');
test('createArgument', () => {
checkClass(createArgument('<foo>', 'description'), 'Argument');
});
6 changes: 0 additions & 6 deletions tests/ts-imports.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { program, Command, Option, CommanderError, InvalidArgumentError, InvalidOptionArgumentError, Help, createCommand } from '../';

import * as commander from '../';

// Do some simple checks that expected imports are available at runtime.
// Similar tests to esm-imports-test.js

Expand All @@ -11,10 +9,6 @@ function checkClass(obj: object, name: string): void {
expect(obj.constructor.name).toEqual(name);
}

test('legacy default export of global Command', () => {
checkClass(commander, 'Command');
});

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