Last active
May 9, 2020 17:38
-
-
Save oliverjumpertz/62b6c18a962da08d142491569535044d to your computer and use it in GitHub Desktop.
readStdIn is an awaitable naive function which reads all input from stdin as a single, new-line preserved string,
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
import readline from 'readline'; | |
async function readStdIn() { | |
return new Promise((resolve, reject) => { | |
const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); | |
let input = ''; | |
rl.on('line', (line) => { | |
input += line; | |
input += '\n'; | |
}); | |
rl.on('close', () => { | |
resolve(input); | |
}); | |
// in case no input is provided, we don't want to block forever | |
setTimeout(() => { | |
if (input === '') { | |
rl.close(); | |
} | |
}, 50); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment