检测节点是否收到stdin

我有兴趣确定是否正在调用我的节点脚本,数据正在stream入或未stream入。

也就是说,我想区分这两种情况

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

我发现这样确定:

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

它可靠吗? 它在语义上是否合适?

回答我自己的问题。 是的,根据一个Node Core团队成员的说法,这是一个很好的方法:

https://twitter.com/thealphanerd/status/782009479382626304