Guest User

Untitled

a guest
Feb 12th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. var readInput = async () => {
  2. var question = async (query, password = false) => {
  3. return new Promise((resolve, reject) => {
  4. var result = ''
  5. process.stdin.setEncoding('utf8')
  6. process.stdin.setRawMode(true)
  7. process.stdout.write(query)
  8. const onReadable = () => {
  9. const chunk = process.stdin.read()
  10. if (chunk !== null) {
  11. if (chunk === '\n' || chunk === '\r' || chunk === '\u0004') {
  12. process.stdout.write('\n')
  13. process.stdin.setRawMode(false)
  14. process.stdin.removeListener('readable', onReadable)
  15. resolve(result)
  16. } else if (chunk === '\u0003') {
  17. process.stdout.write('\n')
  18. process.stdin.setRawMode(false)
  19. process.stdin.removeListener('readable', onReadable)
  20. reject(new Error('Input interrupted'))
  21. } else {
  22. result += chunk
  23. if (password) {
  24. process.stdout.write('*')
  25. } else {
  26. process.stdout.write(chunk)
  27. }
  28. }
  29. }
  30. }
  31. process.stdin.on('readable', onReadable)
  32. })
  33. }
  34.  
  35. var result = {}
  36.  
  37. try {
  38. result.username = await question('username : ')
  39. result.password = await question('password : ', true)
  40. } catch (error) {
  41. process.stdin.emit('end')
  42. throw error
  43. }
  44.  
  45. process.stdin.emit('end')
  46.  
  47. return result
  48. }
Add Comment
Please, Sign In to add comment