1 | 'use strict';
|
2 |
|
3 | var through = require('@ljharb/through');
|
4 | var fs = require('fs');
|
5 |
|
6 | module.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) {
|
34 | console.log(line);
|
35 | } else if (typeof document !== 'undefined') {
|
36 |
|
37 | document.body.innerHTML += line + '<br />';
|
38 | }
|
39 | } catch (e) {
|
40 | stream.emit('error', e);
|
41 | }
|
42 | }
|
43 | line = '';
|
44 | }
|
45 | };
|