@@ -46,13 +46,26 @@ exports.serve = async function serve(root, isBuild, port) {
46
46
server : null ,
47
47
build : null
48
48
} ;
49
+
50
+ const pushLines = ( str , arr ) => {
51
+ const lines = str . split ( / \r ? \n / ) ;
52
+ if ( lines [ lines . length - 1 ] === '' ) {
53
+ lines . pop ( ) ; // last element empty means str ended with \n, remove it or we end up with extra \n when joining again
54
+ }
55
+ Array . prototype . push . apply ( arr , lines ) ;
56
+ } ;
57
+ const collectLogs = ( proc , { out, err } ) => {
58
+ proc . stdout . on ( 'data' , ( d ) => pushLines ( d . toString ( ) , out ) ) ;
59
+ proc . stderr . on ( 'data' , ( d ) => pushLines ( d . toString ( ) , err ) ) ;
60
+ } ;
61
+
49
62
const writeLogs = async ( name , result ) => {
50
63
try {
51
64
if ( result . out && result . out . length > 0 ) {
52
- fs . writeFileSync ( path . join ( logDir , `${ name } .log` ) , result . out . join ( '' ) , 'utf-8' ) ;
65
+ fs . writeFileSync ( path . join ( logDir , `${ name } .log` ) , result . out . join ( '\n ' ) , 'utf-8' ) ;
53
66
}
54
67
if ( result . err && result . err . length > 0 ) {
55
- fs . writeFileSync ( path . join ( logDir , `${ name } .err.log` ) , result . err . join ( '' ) , 'utf-8' ) ;
68
+ fs . writeFileSync ( path . join ( logDir , `${ name } .err.log` ) , result . err . join ( '\n ' ) , 'utf-8' ) ;
56
69
}
57
70
} catch ( e1 ) {
58
71
console . error ( `failed to write ${ name } logs in ${ logDir } ` , e1 ) ;
@@ -72,16 +85,15 @@ exports.serve = async function serve(root, isBuild, port) {
72
85
stdio : 'pipe'
73
86
} ) ;
74
87
logs . build = { out, err } ;
75
- buildProcess . stdout . on ( 'data' , ( d ) => out . push ( d . toString ( ) ) ) ;
76
- buildProcess . stderr . on ( 'data' , ( d ) => err . push ( d . toString ( ) ) ) ;
88
+ collectLogs ( buildProcess , logs . build ) ;
77
89
await buildProcess ;
78
90
} catch ( e ) {
79
91
buildResult = e ;
80
92
if ( buildResult . stdout ) {
81
- out . push ( buildResult . stdout ) ;
93
+ pushLines ( buildResult . stdout , out ) ;
82
94
}
83
95
if ( buildResult . stderr ) {
84
- err . push ( buildResult . stderr ) ;
96
+ pushLines ( buildResult . stderr , err ) ;
85
97
}
86
98
hasErr = true ;
87
99
}
@@ -99,8 +111,7 @@ exports.serve = async function serve(root, isBuild, port) {
99
111
const out = [ ] ,
100
112
err = [ ] ;
101
113
logs . server = { out, err } ;
102
- serverProcess . stdout . on ( 'data' , ( d ) => out . push ( d . toString ( ) ) ) ;
103
- serverProcess . stderr . on ( 'data' , ( d ) => err . push ( d . toString ( ) ) ) ;
114
+ collectLogs ( serverProcess , logs . server ) ;
104
115
105
116
const closeServer = async ( ) => {
106
117
if ( serverProcess ) {
@@ -121,10 +132,10 @@ exports.serve = async function serve(root, isBuild, port) {
121
132
await serverProcess ;
122
133
} catch ( e ) {
123
134
if ( e . stdout ) {
124
- out . push ( e . stdout ) ;
135
+ pushLines ( e . stdout , out ) ;
125
136
}
126
137
if ( e . stderr ) {
127
- err . push ( e . stderr ) ;
138
+ pushLines ( e . stderr , err ) ;
128
139
}
129
140
if ( ! ! process . env . DEBUG && ! isWin ) {
130
141
// treekill on windows uses taskkill and that ends up here always
0 commit comments