1

Is there a way in Node.js to differentiate between a script called with xargs and a script called with a pipe?

$ echo test | xargs node test.js
$ echo test | node test.js

I was hoping for something similar to process.stdin.isTTY described in the docs

JuanCaicedo
  • 3,132
  • 2
  • 14
  • 36

1 Answers1

0

You should be able the solution described here to detect if some data is being piped to node process.

In case of xargs, the stream is being passed to xargs rather than node, so attempting to read from stdin should give you null.

Community
  • 1
  • 1
lorefnon
  • 12,875
  • 6
  • 61
  • 93
  • This is an interesting answer. My trouble is that that check is asynchronous. That is, you can only tell if `this.read()` returns `null` once the `readable` listener gets called. In the xargs case, it will never get called. Do you just have to wait a certain amount of time and if readable hasn't been called by then assume there's no input stream? – JuanCaicedo Oct 01 '16 at 20:21
  • readable listener will get called. The check is asynchronous and I don't think there is a synchronous equivalent, but it will be called irrespective of something was passed to stdin or not. – lorefnon Oct 01 '16 at 20:30