Skip to content

Instantly share code, notes, and snippets.

@kaizhu256
Created December 3, 2020 08:35
Show Gist options
  • Select an option

  • Save kaizhu256/5732029fd026f3d3ba8cac10ff3a1f60 to your computer and use it in GitHub Desktop.

Select an option

Save kaizhu256/5732029fd026f3d3ba8cac10ff3a1f60 to your computer and use it in GitHub Desktop.
nodejs repl with ability to run shell commands in prompt
function replStart () {
/*
* this function will start repl-debugger
*/
let that;
if (globalThis.utility2_repl1) {
return;
}
// start repl
that = require("repl").start({
useGlobal: true
});
globalThis.utility2_repl1 = that;
// init history
that.setupHistory(require("path").resolve(
process.env.HOME + "/.node_repl_history"
), function () {
return;
});
// save eval-function
that.evalDefault = that.eval;
// hook custom-eval-function
that.eval = function (script, context, file, onError) {
script.replace((
/^(\S+)\u0020(.*?)\n/
), function (ignore, match1, match2) {
switch (match1) {
// syntax-sugar - run shell-cmd
case "$":
switch (match2.split(" ").slice(0, 2).join(" ")) {
// syntax-sugar - run git diff
case "git diff":
match2 += " --color";
break;
// syntax-sugar - run git log
case "git log":
match2 += " -n 10";
break;
// syntax-sugar - run ll
case "ll":
match2 = "ls -Fal";
break;
case "npm test":
case "shBuildApp":
if (process.platform === "win32") {
match2 = process.env.UTILITY2_BIN + " " + match2;
}
break;
}
match2 = match2.replace((
/^git\u0020/
), "git --no-pager ");
// source lib.utility2.sh
match2 = (
(
process.platform !== "win32" &&
process.env.UTILITY2_BIN && (match2 !== ":")
)
? ". " + process.env.UTILITY2_BIN + ";" + match2
: match2
);
// run shell-cmd
console.error("$ " + match2);
require("child_process").spawn(match2, {
shell: true,
stdio: [
"ignore", 1, 2
]
// print exitCode
}).on("exit", function (exitCode) {
console.error("$ EXIT_CODE=" + exitCode);
that.evalDefault("\n", context, file, onError);
});
script = "\n";
break;
// syntax-sugar - map text with charCodeAt
case "charCode":
console.error(
match2.split("").map(function (chr) {
return (
"\\u" +
chr.charCodeAt(0).toString(16).padStart(4, 0)
);
}).join("")
);
script = "\n";
break;
// syntax-sugar - sort chr
case "charSort":
console.error(JSON.stringify(match2.split("").sort().join("")));
script = "\n";
break;
// syntax-sugar - list obj-keys, sorted by item-type
// console.error(Object.keys(global).map(function(key){return(typeof global[key]==='object'&&global[key]&&global[key]===global[key]?'global':typeof global[key])+' '+key;}).sort().join('\n')) // jslint ignore:line
case "keys":
script = (
"console.error(Object.keys(" + match2 +
").map(function(key){return(" +
"typeof " + match2 + "[key]==='object'&&" +
match2 + "[key]&&" +
match2 + "[key]===global[key]" +
"?'global'" +
":typeof " + match2 + "[key]" +
")+' '+key;" +
"}).sort().join('\\n'))\n"
);
break;
// syntax-sugar - print String(val)
case "print":
script = "console.error(String(" + match2 + "))\n";
break;
}
});
// eval script
that.evalDefault(script, context, file, onError);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment