Skip to content

Instantly share code, notes, and snippets.

@andreburgaud
Last active November 18, 2019 15:32
Show Gist options
  • Save andreburgaud/27b3c09d64529099e5acc2bdd4761d59 to your computer and use it in GitHub Desktop.
Save andreburgaud/27b3c09d64529099e5acc2bdd4761d59 to your computer and use it in GitHub Desktop.
Bash and JS script allowing to start Node with defined options (i.e. harmony) and custom command and properties.
#!/usr/bin/env node.sh
// Published under the MIT License - https://opensource.org/licenses/MIT
// Installation:
// 1 - Copy both node.sh and node.js in a directory included in your system PATH
// 2 - Ensure that both scripts are executable (chmod +x node.js node.sh)
// Usage:
// 1 - Invoke node.js in a terminal to start the shell
// 2 - Type .help to list the REPL commands available
// 3 - To quit the shell, at the prompt, type .quit or .exit
let repl = require("repl");
let replServer = repl.start({ prompt: `Node:${process.version}> `});
let context = replServer.context;
// Configure what’s available in the REPL
context.util = require("util");
// Define a quit command (same as default exit command)
replServer.defineCommand('quit', {
help: 'Quit the repl (same as .exit)',
action: function() {
this.clearBufferedCommand();
console.log(`Bye!`);
this.close();
}
});
// Define custom functions (print...)
let customFunctions = (ctx) => {
ctx.print = console.log;
}
customFunctions(context);
// Define a command .reset that behaves like .clear, but redefines functions
// initially defined for the repl server, like print.
replServer.defineCommand('reset', {
help: 'Clear local context but preserve custom properties and functions like "print"',
action: function() {
this.clearBufferedCommand();
if (!this.useGlobal) {
this.outputStream.write('Clearing context...\n');
this.resetContext();
customFunctions(this.context);
}
this.displayPrompt();
}
});
#!/usr/bin/env bash
# Published under the MIT License - https://opensource.org/licenses/MIT
# Remove or comment the option --http-parser if it is not available in your version of Node
# - Node 12: 'lhttp' is the default but you can reverse to 'legacy'
# - Node 11: 'legacy' is the default but you can select 'lhttp'
# - Node 10: there is no option to change the http parser (default is 'legacy')
# Enable all harmony features that are set to false by default
OPTIONS=$(node --v8-options \
| grep -A1 "\-\-harmony" \
| awk '/^ --harmony/ { l = $1; getline; printf "%s %s\n", l, $4}' \
| grep false \
| cut -d ' ' -f 1)
node $OPTIONS \
--experimental-repl-await \
--experimental-worker \
--pending-deprecation \
--trace-deprecation \
--trace-warnings \
--http-parser=llhttp \
--title="AB Node JS" \
$*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment