feat: add overWrite option for commands (#503)

* feat: add overWrite option for commands

* fix: throws an error if a command is overwritten
This commit is contained in:
Nate Fischer 2016-08-08 11:44:16 -07:00 committed by GitHub
parent 2395214fee
commit 902f92ab5b
2 changed files with 13 additions and 0 deletions

View File

@ -372,6 +372,7 @@ var DEFAULT_WRAP_OPTIONS = {
pipeOnly: false,
unix: true,
wrapOutput: true,
overWrite: false,
};
// Register a new ShellJS command
@ -379,6 +380,11 @@ function _register(name, implementation, wrapOptions) {
wrapOptions = wrapOptions || {};
// If an option isn't specified, use the default
wrapOptions = objectAssign({}, DEFAULT_WRAP_OPTIONS, wrapOptions);
if (shell[name] && !wrapOptions.overWrite) {
throw new Error('unable to overwrite `' + name + '` command');
}
if (wrapOptions.pipeOnly) {
wrapOptions.canReceivePipe = true;
shellMethods[name] = wrap(name, implementation, wrapOptions);

View File

@ -96,4 +96,11 @@ assert.equal(ret.stdout, '');
assert.equal(ret.stderr, 'foo: Exited with code 5');
assert.equal(shell.error(), 'foo: Exited with code 5');
// Cannot overwrite an existing command by default
var oldCat = shell.cat;
assert.throws(function () {
plugin.register('cat', fooImplementation);
}, 'Error: unable to overwrite `cat` command');
assert.equal(shell.cat, oldCat);
shell.exit(123);