-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
37 lines (34 loc) · 974 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*!
* capture-stream <https://github.com/doowb/capture-stream>
*
* Copyright (c) 2015, Brian Woodward.
* Licensed under the MIT License.
*/
'use strict';
/**
* Capture the output from a stream and store later.
*
* ```js
* var restore = capture(process.stdout);
* console.log('Hello, world!!!');
* console.log('foo', 'bar');
*
* var output = restore();
* console.log(output);
* //=> [ [ 'Hello, world!!!\n' ], [ 'foo bar\n' ] ]
* ```
* @param {Stream} `stream` A stream to capture output from (e.g. `process.stdout`, `process.stderr`)
* @return {Function} `restore` function that restores normal output and returns an array of output.
* @api public
*/
module.exports = function captureStream(stream) {
var output = [];
var write = stream.write;
stream.write = function() {
output.push([].slice.call(arguments));
};
return function restore(wantString) {
stream.write = write;
return wantString ? output.join('') : output;
};
};