Skip to content

Instantly share code, notes, and snippets.

@miguelmota
Last active October 13, 2019 00:16
Show Gist options
  • Save miguelmota/85240f58068262662bf950f3efaf1de9 to your computer and use it in GitHub Desktop.
Save miguelmota/85240f58068262662bf950f3efaf1de9 to your computer and use it in GitHub Desktop.
Node.js CLI read pipe data input example
if (process.stdin) {
process.stdin.setEncoding('utf8')
process.stdin.resume()
let content = ''
process.stdin.on('data', (buf) => {
content += buf.toString()
})
setTimeout(() => {
const value = content.trim()
if (!value) {
value = cli.input[0]
}
run(value)
}, 10)
} else {
const value = cli.input[0]
run(value)
}
function run(value) {
console.log(value)
process.exit(0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment