From ba6e2dfdb70ebf2b2403df7e5776b9ba8e712f4a Mon Sep 17 00:00:00 2001 From: John Gee Date: Sun, 29 Nov 2020 14:10:28 +1300 Subject: [PATCH 1/2] Improve backwards compatibility for command events --- index.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 668c0e775..ed23283d6 100644 --- a/index.js +++ b/index.js @@ -1490,6 +1490,7 @@ Read more on https://git.io/JJc0W`); this.unknownOption(parsed.unknown[0]); } + const commandEvent = `command:${this.name()}`; if (this._actionHandler) { const args = this.args.slice(); this._args.forEach((arg, i) => { @@ -1501,9 +1502,11 @@ Read more on https://git.io/JJc0W`); }); this._actionHandler(args); - this.emit('command:' + this.name(), operands, unknown); + if (this.parent) this.parent.emit(commandEvent, operands, unknown); // legacy + } else if (this.parent && this.parent.listenerCount(commandEvent)) { + this.parent.emit(commandEvent, operands, unknown); // legacy } else if (operands.length) { - if (this._findCommand('*')) { + if (this._findCommand('*')) { // legacy this._dispatchSubcommand('*', operands, unknown); } else if (this.listenerCount('command:*')) { this.emit('command:*', operands, unknown); From 458dbb7aba1e9defc59fb529292ff730a7de556e Mon Sep 17 00:00:00 2001 From: John Gee Date: Sun, 29 Nov 2020 16:16:39 +1300 Subject: [PATCH 2/2] Implement tests for legacy command event --- tests/command.onCommand.test.js | 38 +++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 tests/command.onCommand.test.js diff --git a/tests/command.onCommand.test.js b/tests/command.onCommand.test.js new file mode 100644 index 000000000..149b96c68 --- /dev/null +++ b/tests/command.onCommand.test.js @@ -0,0 +1,38 @@ +const commander = require('../'); + +// The action handler used to be implemented using command events and listeners. +// Now, this is mostly just for backwards compatibility. + +describe(".command('*')", () => { + test('when action handler for subcommand then emit command:subcommand', () => { + const mockListener = jest.fn(); + const program = new commander.Command(); + program + .command('sub') + .action(() => {}); + program.on('command:sub', mockListener); + program.parse(['sub'], { from: 'user' }); + expect(mockListener).toHaveBeenCalled(); + }); + + test('when no action handler for subcommand then still emit command:subcommand', () => { + const mockListener = jest.fn(); + const program = new commander.Command(); + program + .command('sub'); + program.on('command:sub', mockListener); + program.parse(['sub'], { from: 'user' }); + expect(mockListener).toHaveBeenCalled(); + }); + + test('when subcommand has argument then emit command:subcommand with argument', () => { + const mockListener = jest.fn(); + const program = new commander.Command(); + program + .command('sub ') + .action(() => {}); + program.on('command:sub', mockListener); + program.parse(['sub', 'file'], { from: 'user' }); + expect(mockListener).toHaveBeenCalledWith(['file'], []); + }); +});