@@ -9,22 +9,38 @@ const readline = require('readline');
9
9
const WRITE_MAGIC_CODE = '\u001b[0m\u001b[0m\u001b[0m ...' ;
10
10
11
11
// hook stdout to get last line write
12
+ // const util = require('util');
13
+ // util.debug('stdout: ' + util.inspect(string));
12
14
let lastLineStr = '' ;
13
15
hookStdout ( string => {
14
16
lastLineStr = / \n $ / . test ( string ) ? string : lastLineStr ;
17
+ } , ( ) => {
18
+ // when stderr reset lastLineStr
19
+ lastLineStr = '' ;
15
20
} ) ;
16
21
17
- function hookStdout ( callback ) {
22
+ // hookStdout from https://gist.github.com/pguillory/729616
23
+ function hookStdout ( callback , callbackErr = callback ) {
18
24
const oldWrite = process . stdout . write ;
25
+ const oldWriteErr = process . stderr . write ;
19
26
process . stdout . write = ( function hookStdoutWrite ( write ) {
20
27
return function hookedStdoutWrite ( ...args ) {
21
28
write . apply ( process . stdout , args ) ;
22
29
callback . apply ( null , args ) ;
23
30
} ;
24
31
} ) ( process . stdout . write ) ;
25
32
33
+ // hook stderr
34
+ process . stderr . write = ( function hookStderrWrite ( write ) {
35
+ return function hookedStderrWrite ( ...args ) {
36
+ write . apply ( process . stdout , args ) ;
37
+ callbackErr . apply ( null , args ) ;
38
+ } ;
39
+ } ) ( process . stderr . write ) ;
40
+
26
41
return function unhook ( ) {
27
42
process . stdout . write = oldWrite ;
43
+ process . stderr . write = oldWriteErr ;
28
44
} ;
29
45
}
30
46
0 commit comments