From ba781f581f3b8230873c476609dc99fa411aecf2 Mon Sep 17 00:00:00 2001 From: John Gee Date: Sat, 15 Feb 2020 19:32:24 +1300 Subject: [PATCH] Add program to exports --- Readme.md | 2 +- index.js | 1 + tests/program.test.js | 20 ++++++++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 tests/program.test.js diff --git a/Readme.md b/Readme.md index 966062092..42ae36305 100644 --- a/Readme.md +++ b/Readme.md @@ -56,7 +56,7 @@ Commander exports a global object which is convenient for quick programs. This is used in the examples in this README for brevity. ```js -const program = require('commander'); +const program = require('commander').program; program.version('0.0.1'); ``` diff --git a/index.js b/index.js index 7068137d2..0d1837ccd 100644 --- a/index.js +++ b/index.js @@ -1523,6 +1523,7 @@ class Command extends EventEmitter { */ exports = module.exports = new Command(); +exports.program = exports; // More explicit access to global command. /** * Expose classes diff --git a/tests/program.test.js b/tests/program.test.js new file mode 100644 index 000000000..a529ee27f --- /dev/null +++ b/tests/program.test.js @@ -0,0 +1,20 @@ +const commander = require('../'); + +// Do some testing of the default export(s). + +test('when require commander then is a Command (default export of global)', () => { + // Legacy global command + const program = commander; + expect(program.constructor.name).toBe('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('when require commander then has newable Command', () => { + const cmd = new commander.Command(); + expect(cmd.constructor.name).toBe('Command'); +});