stdin.js 586 B

123456789101112131415161718192021222324
  1. const readFromStdin = async () => new Promise((resolve, reject) => {
  2. const encoding = 'utf-8'
  3. let data
  4. data = ''
  5. process.stdin.setEncoding(encoding)
  6. process.stdin.on('readable', function () {
  7. const chunk = process.stdin.read()
  8. if (chunk !== null) {
  9. data += chunk
  10. }
  11. })
  12. process.stdin.on('error', (error) => {
  13. reject(error)
  14. })
  15. process.stdin.on('end', function () {
  16. // There will be a trailing \n from the user hitting enter. Get rid of it.
  17. data = data.replace(/\n$/, '')
  18. resolve(data)
  19. })
  20. })
  21. module.exports = {
  22. read: readFromStdin
  23. }