UNPKG

1.36 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const stripAnsi = require("strip-ansi");
4/** mocks stdout or stderr */
5function mock(std) {
6 let debug;
7 try {
8 debug = require('debug')(std);
9 }
10 catch (_a) { }
11 const orig = process[std].write;
12 let writes = [];
13 function _debug(msg) {
14 if (debug && !debug.enabled)
15 return;
16 // remap writer to allow it to send to debug
17 const prev = process[std].write;
18 process[std].write = orig;
19 debug(msg);
20 process[std].write = prev;
21 }
22 return {
23 stripColor: true,
24 print: false,
25 start() {
26 writes = [];
27 process[std].write = (data, ...args) => {
28 _debug(data);
29 writes.push(bufToString(data));
30 if (this.print)
31 orig.apply(process[std], [data, ...args]);
32 return true;
33 };
34 },
35 stop() {
36 process[std].write = orig;
37 },
38 get output() {
39 let o = this.stripColor ? writes.map(stripAnsi) : writes;
40 return o.join('');
41 },
42 };
43}
44exports.stdout = mock('stdout');
45exports.stderr = mock('stderr');
46function bufToString(b) {
47 if (typeof b === 'string')
48 return b;
49 return b.toString('utf8');
50}