UNPKG

1.24 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const stripAnsi = require("strip-ansi");
4const debug = require('debug')('stdout-stderr');
5const g = global;
6if (!g['stdout-stderr']) {
7 g['stdout-stderr'] = {
8 stdout: process.stdout.write,
9 stderr: process.stderr.write,
10 };
11}
12/** mocks stdout or stderr */
13function 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}
38exports.stdout = mock('stdout');
39exports.stderr = mock('stderr');
40function bufToString(b) {
41 if (typeof b === 'string')
42 return b;
43 return b.toString('utf8');
44}