UNPKG

949 BJavaScriptView Raw
1'use strict';
2
3var through = require('@ljharb/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 if (typeof console !== 'undefined' && console.log) { // eslint-disable-line no-console
34 console.log(line); // eslint-disable-line no-console
35 } else if (typeof document !== 'undefined') {
36 // for IE < 9
37 document.body.innerHTML += line + '<br />';
38 }
39 } catch (e) {
40 stream.emit('error', e);
41 }
42 }
43 line = '';
44 }
45};