UNPKG

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