Skip to content

Commit

Permalink
feat(plugin): support deprecating plugins
Browse files Browse the repository at this point in the history
This adds a new wrap() option named `.deprecate`. This currently accepts
a string value, which will be the deprecation message for the plugin.
Deprecation works on standard commands as well as pipes. The call stack
points directly to the deprecated method, and not to any ShellJS
internals.

Fixes #541
Test: This adds unit tests.
  • Loading branch information
nfischer committed Nov 13, 2018
1 parent d4d1317 commit ed9ee1f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

var os = require('os');
var fs = require('fs');
var util = require('util');
var glob = require('glob');
var shell = require('..');

Expand Down Expand Up @@ -312,7 +313,7 @@ exports.randomFileName = randomFileName;
// command-logging, and other nice things
function wrap(cmd, fn, options) {
options = options || {};
return function () {
var fun = function () {
var retValue = null;

state.currentCmd = cmd;
Expand Down Expand Up @@ -409,6 +410,11 @@ function wrap(cmd, fn, options) {
state.currentCmd = 'shell.js';
return retValue;
};
if (options.deprecated) {
var msg = options.deprecated; // This is the deprecation message.
fun = util.deprecate(fun, msg);
}
return fun;
} // wrap
exports.wrap = wrap;

Expand All @@ -427,6 +433,7 @@ var DEFAULT_WRAP_OPTIONS = {
pipeOnly: false,
wrapOutput: true,
unix: true,
deprecated: '',
};

// This is populated during plugin registration
Expand Down
34 changes: 34 additions & 0 deletions test/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,37 @@ test('Cannot overwrite an existing command', t => {
}, 'Command `cat` already exists');
t.is(shell.cat, oldCat);
});

test('Deprecated command', t => {
process.throwDeprecation = true;
const oldThrowDep = process.throwDeprecation;
try {
plugin.register('dep', function dep(opt, arg) { return arg; }, {
cmdOptions: null,
deprecated: 'Use the foo method instead',
});

t.throws(() => {
shell.dep('bar');
}, /Use the foo method instead/);
} finally {
process.throwDeprecation = oldThrowDep;
}
});

test('Deprecated command in pipe', t => {
process.throwDeprecation = true;
plugin.register('depPipe', function depPipe(opt, arg) { return arg; }, {
cmdOptions: null,
deprecated: 'Use the foo pipe instead',
canReceivePipe: true,
});
const oldThrowDep = process.throwDeprecation;
try {
t.throws(() => {
shell.cat('./package.json').depPipe('bar');
}, /Use the foo pipe instead/);
} finally {
process.throwDeprecation = oldThrowDep;
}
});

0 comments on commit ed9ee1f

Please sign in to comment.