Skip to content

Instantly share code, notes, and snippets.

@Jaballadares
Forked from dpyro/cli.js
Created June 4, 2021 00:34
Show Gist options
  • Save Jaballadares/b42e29f2132ee5aadc76a3977e6b45dc to your computer and use it in GitHub Desktop.
Save Jaballadares/b42e29f2132ee5aadc76a3977e6b45dc to your computer and use it in GitHub Desktop.
simple readline cli for node.js
const readline = require('readline')
/**
* Create an active bound readline interface using `stdin` and `stdout`.
*
* @param {function(string): void} callback handler for each inputed line
* @returns {readline.ReadLine} the active configured interface
*/
function createReadlineInterface(callback) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
})
// More events at: https://nodejs.org/api/readline.html#readline_class_interface
rl.on('line', (line) => {
callback(line)
rl.prompt()
})
rl.on('close', () => {
console.log('\nBye!')
process.exit(0)
})
rl.prompt()
return rl
}
module.exports = createReadlineInterface
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment