Reputation: 1079
I'm building a CLI app with nodejs with native REPL
. A dumb sample is:
const repl = require('repl')
const myApp = require('myApp')
repl.start({
eval: (input, context, filename, callback) => {
const result = myApp(input)
callback(null, result)
},
writer: (result) => result
? 'my result: ' + result
: '' // <--- empty result
})
Everything is fine, but a newline is created for every response of the app. I'd like to override this behavior, and decide whether to create a new line or not.
Example Let's pretend the app to be a calculator:
$ node index.js
add 5
add 3
result
8
Because add
command doesn't have any result, a new command should be inserted in the very next line. However, what I'm getting from the above code is:
$ node index.js
add 5
add 3
result
8
with an empty line added after any 0add
call.
How can I prevent this?
Upvotes: 0
Views: 254
Reputation: 1079
So I found out there's a parameter I could set, ignoreUndefined
. If true, when you call callback(null, undefined)
(empty result case), it won't add a new line.
Thus, in the above example:
const repl = require('repl')
const myApp = require('myApp')
repl.start({
ignoreUndefined: true,
eval: (input, context, filename, callback) => {
const result = myApp(input)
callback(null, result)
},
writer: (result) => result
? 'my result: ' + result
: undefined // <--- empty result
})
docs: https://nodejs.org/api/repl.html
similar question: node.js REPL "undefined"
Upvotes: 1