I have been searching far and wide for a node module that provides a REPL-like CLI prompt interface for a Node app.
What I am looking for is kind of a hybrid between inquirer or prompt, and commander.
Node provides a built-in REPL module, however this just exposes the raw javascript of your application. I want to built a virtual interface for an application.
Example, supposing you run node server.js
, you would get a prompt:
server:~$
This would behave like a basic unix prompt, in your own virtual context. Pressing enter
:
server:~$
server:~$
Or help
:
server:~$ help
Commands:
debug [setting] Enables or disables debugging.
show stats Displays live stats for your web server.
server:~$
And you could execute custom commands:
server:~$ debug on -v 7
Debugging turned on with a verbosity of 7
... live logging ...
Any idea of what NPM modules handle this?
Due to nothing matching my needs, I ended up building and publishing Vorpal, a framework for building Interactive CLIs in Node.
You could use the standard REPL module and plug in a custom command evaluator. The "up arrow to repeat commands" and such is already baked into the repl module, so you don't need to worry about that.
For example, here's a crappily implemented evaluator that does the things you've described in your question:
var repl = require("repl");
var cmds = {
"help" : function(input, context) {
return "debug [setting] Enables or disables debugging..."
},
"debug" : function(input, context) {
var args = input.split(/\s+/).slice(1);
var onoff = args[0];
var verbosity = args[2];
return "Debugging turned " + onoff + " with a verbosity of " + verbosity;
},
"exit": function(input, context) {
process.exit();
},
"default" : function(input, context) {
return "Command not understood";
}
};
function eval(input, context, filename, callback) {
var cmd = input.split(/\s+/)[0];
var result = (cmds[cmd] || cmds["default"])(input, context);
callback(null, result);
}
repl.start({
prompt: "server:~$ ",
eval: eval
});
Please note the focus of the demo here is how to implement a custom REPL; there are obviously better ways to implement the evaluator than whitespace splitting and a hash of functions, but how you implement the command evaluator depends on what your application is supposed to do
There is a node-shell available, see also the home page.
Features from the home page are:
Shell brings a Connect inspired API, Express inspired routing, and other similar functionality to console based applications.
You find a basic reddis client example on the homepage which comes with command completion and history.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With