File tree Expand file tree Collapse file tree 1 file changed +34
-4
lines changed Expand file tree Collapse file tree 1 file changed +34
-4
lines changed Original file line number Diff line number Diff line change @@ -46,11 +46,41 @@ export class DiffParser implements Parser {
46
46
47
47
// 执行 diff
48
48
this . stdout = await new Promise ( ( resolve , reject ) => {
49
- child . exec ( `git diff ${ this . hash } ` , this . opt , ( err , stdout ) => {
50
- if ( err ) {
51
- reject ( err ) ;
49
+ const command = `git diff ${ this . hash } ` ;
50
+
51
+ // 注意不能用 exec, 原因详见 https://github.com/matmanjs/incremental-coverage/issues/8
52
+ const cmd = child . spawn ( command , [ ] , {
53
+ ...( this . opt || { } ) ,
54
+ // windows 中如果不设置它的话会出错
55
+ shell : true ,
56
+ } , ) ;
57
+
58
+ let result = '' ;
59
+ let errMsg = '' ;
60
+
61
+ // 增加一个超时限制
62
+ const checkT = setTimeout ( ( ) => {
63
+ resolve ( result ) ;
64
+ } , 5000 ) ;
65
+
66
+ cmd . stdout . on ( 'data' , ( data ) => {
67
+ result += data ;
68
+ } ) ;
69
+
70
+ cmd . stderr . on ( 'data' , ( data ) => {
71
+ // console.error(`stderr: ${data}`);
72
+ errMsg += data ;
73
+ } ) ;
74
+
75
+ cmd . on ( 'close' , ( code ) => {
76
+ clearTimeout ( checkT ) ;
77
+
78
+ if ( code ) {
79
+ // console.error(`${command} close: ${code}`);
80
+ reject ( new Error ( `(close=${ code } )${ errMsg } ` ) ) ;
81
+ } else {
82
+ resolve ( result ) ;
52
83
}
53
- resolve ( stdout ) ;
54
84
} ) ;
55
85
} ) ;
56
86
}
You can’t perform that action at this time.
0 commit comments