Skip to content

Instantly share code, notes, and snippets.

@Low-power
Last active April 23, 2018 05:18
Show Gist options
  • Save Low-power/db1f931f820d2ef56af4b7c61af2aa6c to your computer and use it in GitHub Desktop.
Save Low-power/db1f931f820d2ef56af4b7c61af2aa6c to your computer and use it in GitHub Desktop.
Nodejs wrapper to support .noderc.js and dynamic prompt.
#!/bin/sh
# A Node.js wrapper
# Copyright 2015-2018 Rivoreo
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
NODE_PREFIX=/opt/node
NODE="$NODE_PREFIX/bin/node"
print_usage() {
printf "Usage: %s [-{c|e} <command>] [-ipv] [<script-file>] [<argument>] [...]\\n" "$1"
}
command=
interactive=
print=
while getopts c:e:vpi c
do case $c in
c|e)
command="$OPTARG"
;;
v)
exec "$NODE" -v
;;
i)
interactive=-i
;;
p)
print=-p
;;
*)
print_usage "$0"
exit 255
;;
esac done
shift $((OPTIND-1))
load_rc_file='{
var _fs = require("fs");
var _vm = require("vm");
_home = process.env["HOME"];
if(_home) {
rc_file = _home + "/.noderc.js";
if(_fs.existsSync(rc_file)) _vm.runInThisContext(_fs.readFileSync(rc_file));
rc_file = undefined;
delete rc_file;
}
delete _home;
}'
[ -n "$command" ] && exec "$NODE" -e "$load_rc_file
$command" $interactive $print "$@"
run_file='{
_fs.readFile(__filename === "-" ? "/dev/stdin" : __filename, function(e, data) {
var _path = require("path");
var _offset = 0;
if(e) throw e;
if(data.length > 3 && data[0] == 0x23 && data[1] == 0x21) {
// Strip #!...
_offset = 2;
while(++_offset < data.length) if(data[_offset] == 0xa) break;
}
if(__filename === "-") {
__filename = "[stdin]";
} else {
__filename = _path.resolve(__filename);
__dirname = _path.dirname(__filename);
}
_vm.runInThisContext(data.toString("utf-8", _offset), { filename:__filename });
});
}'
[ $# = 0 ] && [ -z "$interactive" ] && [ ! -t 0 ] && exec "$NODE" -e "$load_rc_file
__filename = \"-\";
$run_file" $print
[ "$1" = - ] && shift && set -- /dev/stdin "$@"
start_repl='{
var _get_prompt;
switch(typeof prompt) {
case "string":
break;
case "function":
_get_prompt = prompt;
prompt = prompt();
break;
case "undefined":
prompt = "nodejs> ";
break;
}
var repl = require("repl").start(prompt, process, null, true, true);
if(typeof _get_prompt === "function") {
repl.getPrompt = _get_prompt;
repl.displayPrompt = function(preserve_cur) {
var prompt;
if (repl.bufferedCommand.length) {
prompt = "...";
var levelInd = new Array(this.lines.level.length).join("..");
prompt += levelInd + " ";
} else prompt = repl.getPrompt();
//repl.REPLServer.super_.prototype.setPrompt.call(repl, prompt);
repl.setPrompt(prompt);
repl.prompt(preserve_cur);
};
}
//repl.commands.exit =
repl.on("close", function() {
process.stderr.write("exit\n");
repl.emit("exit");
process.exit();
});
}'
run_file_or_start_repl="if(process.argv.length > 1) {
__filename = process.argv[1];
$run_file
} else $start_repl"
exec "$NODE" -e "$load_rc_file
$run_file_or_start_repl" $interactive $print "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment