heapwolf / prompt-sync

a synchronous prompt for node.js
MIT License
211 stars 42 forks source link

Will not work in Bash command when information piped in #40

Open guypursey opened 4 years ago

guypursey commented 4 years ago

To recreate, create the following file prompt-test.js, as a very simple test:

#!/usr/bin/env node
const prompt = require("prompt-sync")({"sigint": true})

for (var i = 0; i < 5; i++) {
  console.log("Before prompt line")
  var result = prompt("This is a test -- enter something >");
  console.log("Result of prompt", result)
}

If run as ./prompt-test.js, it will ask for an input 5 times and print that output as you might expect.

However if you trying running echo hello world | ./prompt-test.js, the prompt itself is caught in a loop and repeats the prompt line and all previous inputs continually.

This might be happening because of my lack of understanding about Bash processes, sub-processes, or piping. Any help appreciated.

guypursey commented 4 years ago

This might have something to do with the reliance on process.stdin ... code suggests that's being used when process.platform === 'win32'. From the little I know, if a TTY readstream can be used instead, for example, it means piped input and keyboard input can be accepted and should work.