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 | function bufToString(b) {
|
13 | if (typeof b === 'string')
|
14 | return b;
|
15 | return b.toString('utf8');
|
16 | }
|
17 | const originalConsoleLog = console.log;
|
18 |
|
19 | function mock(std) {
|
20 | let writes = [];
|
21 | return {
|
22 | stripColor: true,
|
23 | print: false,
|
24 | start() {
|
25 | debug('start', std);
|
26 | writes = [];
|
27 | process[std].write = (data, ...args) => {
|
28 | writes.push(bufToString(data));
|
29 | if (this.print) {
|
30 | g['stdout-stderr'][std].apply(process[std], [data, ...args]);
|
31 | }
|
32 | else {
|
33 | const callback = args[0];
|
34 | if (callback)
|
35 | callback();
|
36 | }
|
37 | return true;
|
38 | };
|
39 | if (std === 'stdout') {
|
40 | console.log = (...args) => {
|
41 | process.stdout.write(`${args.join(' ')}\n`);
|
42 | };
|
43 | }
|
44 | },
|
45 | stop() {
|
46 | process[std].write = g['stdout-stderr'][std];
|
47 | if (std === 'stdout')
|
48 | console.log = originalConsoleLog;
|
49 | debug('stop', std);
|
50 | },
|
51 | get output() {
|
52 | const o = this.stripColor ? writes.map(stripAnsi) : writes;
|
53 | return o.join('');
|
54 | },
|
55 | };
|
56 | }
|
57 | exports.stdout = mock('stdout');
|
58 | exports.stderr = mock('stderr');
|