Skip to content

Commit

Permalink
fix: use new attribute for repl inspection
Browse files Browse the repository at this point in the history
This updates the custom inspect implementation to use the preferred
attribute for compatibility with newer node versions.

See nfischer/shelljs-plugin-inspect#9 for more
details.
  • Loading branch information
nfischer committed Jan 14, 2022
1 parent ac9fd88 commit 9bb67b9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
17 changes: 13 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require('shelljs-plugin-inspect');
require('shelljs-plugin-open');
require('shelljs-plugin-sleep');

var util = require('util');
var repl = require('repl');
var argv = require('minimist')(process.argv.slice(2));
var replHistory = require('repl.history');
Expand Down Expand Up @@ -51,6 +52,9 @@ var replServer = repl.start({
var HISTORY_FILE = path.join(osHomedir(), '.n_shell_history');
replHistory(replServer, HISTORY_FILE);

// Newer versions of node use a symbol called util.inspect.custom.
var inspectAttribute = util.inspect.custom || 'inspect';

function wrap(fun, key) {
if (typeof fun !== 'function') {
return fun; // not a function
Expand All @@ -62,19 +66,24 @@ function wrap(fun, key) {
function emptyInspect() {
return '';
}
var oldInspect = ret.inspect.bind(ret);
var oldInspect;
if (ret[inspectAttribute]) {
oldInspect = ret[inspectAttribute].bind(ret);
} else {
oldInspect = function() { return ''; }
}
if (key === 'echo' || key === 'exec') {
ret.inspect = emptyInspect;
ret[inspectAttribute] = emptyInspect;
} else if (key === 'pwd' || key === 'which') {
ret.inspect = function () {
ret[inspectAttribute] = function () {
var oldResult = oldInspect();
return oldResult.match(/\n$/) ? oldResult : oldResult + '\n';
};
}
}
return ret;
};
outerRet.inspect = outerRet.inspect || function () { return this(); };
outerRet[inspectAttribute] = outerRet[inspectAttribute] || function () { return this(); };
return outerRet;
}
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
"repl.history": "^0.1.3",
"require-relative": "^0.8.7",
"shelljs": "^0.8.5",
"shelljs-plugin-clear": "^0.2.0",
"shelljs-plugin-inspect": "^0.2.0",
"shelljs-plugin-clear": "^0.2.1",
"shelljs-plugin-inspect": "^0.2.1",
"shelljs-plugin-open": "^0.2.0",
"shelljs-plugin-sleep": "^0.2.1"
},
Expand Down

0 comments on commit 9bb67b9

Please sign in to comment.