1

I have a large Node repo with many npm scripts to manage the project. I created an interactive script with Inquirer.js to act as an entry point to help developers to navigate. The script outputs the result in JSON.

To pick the selected npm script, and run it, I tried the following:

{
    "scripts": {
        "dev": "node dev.js | jq .project | xargs yarn run"
    }
}

But instead of seeing the Inquirer script, this command stalls with an error message coming from jq.

What's the right way to wait for the result before piping to the next program?

miklosme
  • 761
  • 1
  • 5
  • 19
  • There is no bug caused by a lack of waiting in your command. You can determine the real cause of failure by examining the `jq` error message. – that other guy Nov 15 '21 at 21:11
  • @thatotherguy It happens immediately after `yarn dev`, the problem is not with the output, but something with the streams. Piping does not wait for the node script to finish. – miklosme Nov 16 '21 at 14:42
  • What is the `jq` error message? – that other guy Nov 16 '21 at 18:41
  • @thatotherguy ``` $ node dev.js | jq .project | xargs yarn run parse error: Invalid numeric literal at line 1, column 2 (node:54803) UnhandledPromiseRejectionWarning: Error: write EPIPE at afterWriteDispatched (internal/stream_base_commons.js:156:25) at writeGeneric (internal/stream_base_commons.js:147:3) [...] (node:54803) UnhandledPromiseRejectionWarning: Unhandled promise rejection. ``` – miklosme Nov 17 '21 at 17:44
  • This means that `node dev.js` is outputting something that isn't valid JSON. You can use `node dev.js | tee /tmp/dump | jq .project | xargs yarn run` to save a copy of the output to `/tmp/dump` so that you can inspect it manually. – that other guy Nov 17 '21 at 18:28
  • @thatotherguy Yeah, it outputs the Inqurier.js prompt as expected: ```% cat /tmp/dump ? Which project do you want to develop? (Use arrow keys) ❯ Foo Bar ``` – miklosme Nov 18 '21 at 19:03
  • That's a bug. Any questions for the user that is not part of the output is supposed to go to /dev/tty (or at least stderr). They can not be written to stdout. – that other guy Nov 18 '21 at 19:12

0 Answers0