Node.jsで標準入力がパイプかどうか判定する
例えば入力ファイルの行数をカウントするlinecount.jsっていうのがあったとして
$ node linecount.js foo.txt
っていう使い方と
$ cat foo.txt | linecount.js
って両方に対応したいときNode.jsではprocess.stdin.isTTYを使えば判定できる。
if (process.stdin.isTTY) { // $ node linecount.js foo.txt // みたいに実行した時の処理 } else { // $ cat foo.txt | linecount.js // みたいに実行した時の処理 }