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

Emit new events beforeAction and beforeExec #1330

Closed
wants to merge 2 commits into from
Closed
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
36 changes: 36 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,37 @@ class Command extends EventEmitter {
return this;
};

/**
* Emit a general event and a sub-event to this command and all of its
* parents in the command hierarchy, proceeding bottom-up. The number of
* events emitted at each level increases by one so that a given command
* can select for events on any of its sub-command trees. Probably overkill,
* but given that the depth is usually one it doesn't seem harmful.
*/
_emitToTree(eventName, args) {
let val = {
command: this,
name: this.name(),
cmdPath: this.name(), // initially
execArg: args.execArg,
args: args.args
};

let cmd;

for (cmd = this; cmd; cmd = cmd.parent) {
cmd.emit(eventName, val);

if (cmd.parent) {
const newCmdPath = cmd.parent.name() + '.' + val.cmdPath;

// Use spread to make a new emitted value, because the event structure
// is supposed to be read-only.
val = { ...val, cmdPath: newCmdPath };
}
}
}

/**
* @return {boolean}
* @api private
Expand Down Expand Up @@ -858,14 +889,17 @@ class Command extends EventEmitter {
// add executable arguments to spawn
args = incrementNodeInspectorPort(process.execArgv).concat(args);

this._emitToTree('preSpawn', { execArg: process.argv[0], args: args });
proc = spawn(process.argv[0], args, { stdio: 'inherit' });
} else {
this._emitToTree('preSpawn', { execArg: bin, args: args });
proc = spawn(bin, args, { stdio: 'inherit' });
}
} else {
args.unshift(bin);
// add executable arguments to spawn
args = incrementNodeInspectorPort(process.execArgv).concat(args);
this._emitToTree('preSpawn', { execArg: process.execPath, args: args });
proc = spawn(process.execPath, args, { stdio: 'inherit' });
}

Expand Down Expand Up @@ -972,7 +1006,9 @@ class Command extends EventEmitter {
}
});

this._emitToTree('preAction', { execArg: this.name(), args: args });
this._actionHandler(args);
this._emitToTree('postAction', { execArg: this.name(), args: args });
this.emit('command:' + this.name(), operands, unknown);
} else if (operands.length) {
if (this._findCommand('*')) {
Expand Down