Skip to content

Commit 902f92a

Browse files
authored
feat: add overWrite option for commands (#503)
* feat: add overWrite option for commands * fix: throws an error if a command is overwritten
1 parent 2395214 commit 902f92a

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

Diff for: src/common.js

+6
Original file line numberDiff line numberDiff line change
@@ -372,13 +372,19 @@ var DEFAULT_WRAP_OPTIONS = {
372372
pipeOnly: false,
373373
unix: true,
374374
wrapOutput: true,
375+
overWrite: false,
375376
};
376377

377378
// Register a new ShellJS command
378379
function _register(name, implementation, wrapOptions) {
379380
wrapOptions = wrapOptions || {};
380381
// If an option isn't specified, use the default
381382
wrapOptions = objectAssign({}, DEFAULT_WRAP_OPTIONS, wrapOptions);
383+
384+
if (shell[name] && !wrapOptions.overWrite) {
385+
throw new Error('unable to overwrite `' + name + '` command');
386+
}
387+
382388
if (wrapOptions.pipeOnly) {
383389
wrapOptions.canReceivePipe = true;
384390
shellMethods[name] = wrap(name, implementation, wrapOptions);

Diff for: test/plugin.js

+7
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,11 @@ assert.equal(ret.stdout, '');
9696
assert.equal(ret.stderr, 'foo: Exited with code 5');
9797
assert.equal(shell.error(), 'foo: Exited with code 5');
9898

99+
// Cannot overwrite an existing command by default
100+
var oldCat = shell.cat;
101+
assert.throws(function () {
102+
plugin.register('cat', fooImplementation);
103+
}, 'Error: unable to overwrite `cat` command');
104+
assert.equal(shell.cat, oldCat);
105+
99106
shell.exit(123);

0 commit comments

Comments
 (0)