我想將事情記錄到終端上,而無需將它們寫入process.stdout
或process.stderr
,以便管道流程不會因爲這些日誌而受到污染。如何在不寫入process.stdout或process.stderr的情況下寫入終端?
這是非常相當於在bash中echo "hello" > /dev/tty
(見this question),但我找不到如何從節點直接訪問/dev/tty
。
我想將事情記錄到終端上,而無需將它們寫入process.stdout
或process.stderr
,以便管道流程不會因爲這些日誌而受到污染。如何在不寫入process.stdout或process.stderr的情況下寫入終端?
這是非常相當於在bash中echo "hello" > /dev/tty
(見this question),但我找不到如何從節點直接訪問/dev/tty
。
只要打開它作爲一個文件,並寫入它:)
var fs = require('fs');
var tty = fs.createWriteStream('/dev/tty');
console.log('hello');
tty.write('foo\n');
tty.write('bar\n');
所以我找到了一種方法,但可能被視爲作弊;)
var exec = require('child_process').exec
process.stdout.write('writing to stdout')
exec("echo 'writing to the terminal without writing to stdout' > /dev/tty")