Using the defineCommand we can define a command that can be invoked using a dot before it:
replServer.defineCommand("foo", ...)
Then, in the repl, we can do:
> .foo
And that will call the command we have defined.
I would like to replicate the same functionality like in the nodejs debugger to define the commands like functions, instead of using .keyword.
Using useGlobal: true, we can pass the global context to the REPL, and then access any global functions, but I am wondering if there is a way to add them automatically in the REPL help output, like the defineCommand does.
How can we do that?
Here is a sample from the docs.
const repl = require('repl');
const replServer = repl.start({ prompt: '> ' });
replServer.defineCommand('sayhello', {
help: 'Say hello',
action(name) {
this.clearBufferedCommand();
console.log(`Hello, ${name}!`);
this.displayPrompt();
}
});
replServer.defineCommand('saybye', function saybye() {
console.log('Goodbye!');
this.close();
});
This is how the help output looks like:
$ node tmp.js
42
> .help
.break Sometimes you get stuck, this gets you out
.clear Break, and also clear the local context
.editor Enter editor mode
.exit Exit the repl
.help Print this help message
.load Load JS from a file into the REPL session
.save Save all evaluated commands in this REPL session to a file
.saybye
.sayhello Say hello
$ node debug tmp.js
...
debug> help
run, restart, r Run the application or reconnect
kill Kill a running application or disconnect
cont, c Resume execution
next, n Continue to next line in current file
step, s Step into, potentially entering a function
out, o Step out, leaving the current function
backtrace, bt Print the current backtrace
...