1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const stripAnsi = require("strip-ansi");
|
4 | const debug = require('debug')('stdout-stderr');
|
5 | const g = global;
|
6 | if (!g['stdout-stderr']) {
|
7 | g['stdout-stderr'] = {
|
8 | stdout: process.stdout.write,
|
9 | stderr: process.stderr.write,
|
10 | };
|
11 | }
|
12 |
|
13 | function mock(std) {
|
14 | let writes = [];
|
15 | return {
|
16 | stripColor: true,
|
17 | print: false,
|
18 | start() {
|
19 | debug('start', std);
|
20 | writes = [];
|
21 | process[std].write = (data, ...args) => {
|
22 | writes.push(bufToString(data));
|
23 | if (this.print)
|
24 | g['stdout-stderr'][std].apply(process[std], [data, ...args]);
|
25 | return true;
|
26 | };
|
27 | },
|
28 | stop() {
|
29 | process[std].write = g['stdout-stderr'][std];
|
30 | debug('stop', std);
|
31 | },
|
32 | get output() {
|
33 | let o = this.stripColor ? writes.map(stripAnsi) : writes;
|
34 | return o.join('');
|
35 | },
|
36 | };
|
37 | }
|
38 | exports.stdout = mock('stdout');
|
39 | exports.stderr = mock('stderr');
|
40 | function bufToString(b) {
|
41 | if (typeof b === 'string')
|
42 | return b;
|
43 | return b.toString('utf8');
|
44 | }
|