-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
39 lines (36 loc) · 958 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
var through = require('through2')
var util = require('util')
function stdout(outputStream, prefix) {
if (typeof outputStream === 'string') {
prefix = outputStream
outputStream = undefined
}
var output = outputStream
if (!output) {
if (process.browser) output = consoleLogStream()
else output = process.stdout
}
if (!prefix) prefix = ''
var ws = through({objectMode: true}, write)
function write(data, enc, next) {
var str;
if (Buffer.isBuffer(data)) {
this.push(Buffer.concat([new Buffer(prefix), data]))
} else if (typeof data === 'string') {
this.push(prefix + data + '\n')
} else {
this.push(prefix + util.inspect(data) + '\n')
}
next()
}
ws.pipe(output)
return ws
}
function consoleLogStream() {
return through(function(data, enc, next) {
if (Buffer.isBuffer(data)) console.log(data.toString())
else console.log(data)
next()
})
}
module.exports = stdout;