Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if node receives stdin

I'm interested in determining if my node script is being called with data being streamed into it or not.

That is, I want to differentiate between these two cases

$ node index.js
$ ls | node index.js

I found this way of determining that:

if(process.stdin.isTTY) {
  console.log('called without pipe');
} else {
  console.log('called with data streamed in');
}

Is it reliable? Is it semantically appropriate?

like image 428
JuanCaicedo Avatar asked Oct 01 '16 00:10

JuanCaicedo


1 Answers

Answering my own question: here's a link to the [docs].

$ node -p -e "Boolean(process.stdout.isTTY)"
true
$ node -p -e "Boolean(process.stdout.isTTY)" | cat
false
like image 88
JuanCaicedo Avatar answered Nov 08 '22 16:11

JuanCaicedo