Last active
October 13, 2019 00:16
-
-
Save miguelmota/85240f58068262662bf950f3efaf1de9 to your computer and use it in GitHub Desktop.
Node.js CLI read pipe data input example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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