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

command:<name> event doesn't bubble to the parent #1402

Closed
grabbou opened this issue Nov 26, 2020 · 2 comments
Closed

command:<name> event doesn't bubble to the parent #1402

grabbou opened this issue Nov 26, 2020 · 2 comments
Labels
bug Commander is not working as intended
Milestone

Comments

@grabbou
Copy link

grabbou commented Nov 26, 2020

Hey,

Not sure if I am using this correctly, but it does seem to be a good use case and not far away from working.

My program is as follows:

const config = program.commad('config');
const start = program.command('start');

program.on('command:*', () => /* handle unexpected command */);

program.on('command:config', () => /* never gets called */);

In the above example, when config command is executed, the command:config listener never gets executed.

After console logging around the Commander source code, I have found out that the reason this doesn't work is that listener is registered on parent (in this context, main program) whereas the emit function is called on a subcommand (config).

Changing this.emit to this.parent.emit fixes it https://github.com/tj/commander.js/blob/master/index.js#L1003

Is there any way to bubble events up? It seems to be quite natural to listen to command:<name> on the main object, otherwise, there's no difference from attaching a listener to config vs tweaking action handler.

@shadowspawn shadowspawn added the bug Commander is not working as intended label Nov 26, 2020
@shadowspawn
Copy link
Collaborator

In your example, the emit for command:config should indeed be called on program. After some investigation, I think this is a bug introduced in Commander v5. The refactor of the code changed where the emit is called.

@shadowspawn shadowspawn added the pending release Merged into a branch for a future release, but not released yet label Dec 1, 2020
@shadowspawn shadowspawn added this to the v7.0.0 milestone Dec 1, 2020
@shadowspawn shadowspawn removed the pending release Merged into a branch for a future release, but not released yet label Jan 15, 2021
@shadowspawn
Copy link
Collaborator

Fix included in Commander 7.0.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Commander is not working as intended
Projects
None yet
Development

No branches or pull requests

2 participants