From 902f92ab5b10e02ad23461f76fbf69cba4f5bb7c Mon Sep 17 00:00:00 2001 From: Nate Fischer Date: Mon, 8 Aug 2016 11:44:16 -0700 Subject: [PATCH] feat: add overWrite option for commands (#503) * feat: add overWrite option for commands * fix: throws an error if a command is overwritten --- src/common.js | 6 ++++++ test/plugin.js | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/src/common.js b/src/common.js index 5b5cd9f..919aee2 100644 --- a/src/common.js +++ b/src/common.js @@ -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); diff --git a/test/plugin.js b/test/plugin.js index bcc9c62..42a7830 100644 --- a/test/plugin.js +++ b/test/plugin.js @@ -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);