UNPKG

805 BJavaScriptView Raw
1var through = require('through');
2var fs = require('fs');
3
4module.exports = function () {
5 var line = '';
6 var stream = through(write, flush);
7 return stream;
8
9 function write (buf) {
10 for (var i = 0; i < buf.length; i++) {
11 var c = typeof buf === 'string'
12 ? buf.charAt(i)
13 : String.fromCharCode(buf[i])
14 ;
15 if (c === '\n') flush();
16 else line += c;
17 }
18 }
19
20 function flush () {
21 if (fs.writeSync && /^win/.test(process.platform)) {
22 try { fs.writeSync(1, line + '\n'); }
23 catch (e) { stream.emit('error', e) }
24 }
25 else {
26 try { console.log(line) }
27 catch (e) { stream.emit('error', e) }
28 }
29 line = '';
30 }
31};