@@ -46,13 +46,26 @@ exports.serve = async function serve(root, isBuild, port) {
4646 server : null ,
4747 build : null
4848 } ;
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+
4962 const writeLogs = async ( name , result ) => {
5063 try {
5164 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' ) ;
5366 }
5467 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' ) ;
5669 }
5770 } catch ( e1 ) {
5871 console . error ( `failed to write ${ name } logs in ${ logDir } ` , e1 ) ;
@@ -72,16 +85,15 @@ exports.serve = async function serve(root, isBuild, port) {
7285 stdio : 'pipe'
7386 } ) ;
7487 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 ) ;
7789 await buildProcess ;
7890 } catch ( e ) {
7991 buildResult = e ;
8092 if ( buildResult . stdout ) {
81- out . push ( buildResult . stdout ) ;
93+ pushLines ( buildResult . stdout , out ) ;
8294 }
8395 if ( buildResult . stderr ) {
84- err . push ( buildResult . stderr ) ;
96+ pushLines ( buildResult . stderr , err ) ;
8597 }
8698 hasErr = true ;
8799 }
@@ -99,8 +111,7 @@ exports.serve = async function serve(root, isBuild, port) {
99111 const out = [ ] ,
100112 err = [ ] ;
101113 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 ) ;
104115
105116 const closeServer = async ( ) => {
106117 if ( serverProcess ) {
@@ -121,10 +132,10 @@ exports.serve = async function serve(root, isBuild, port) {
121132 await serverProcess ;
122133 } catch ( e ) {
123134 if ( e . stdout ) {
124- out . push ( e . stdout ) ;
135+ pushLines ( e . stdout , out ) ;
125136 }
126137 if ( e . stderr ) {
127- err . push ( e . stderr ) ;
138+ pushLines ( e . stderr , err ) ;
128139 }
129140 if ( ! ! process . env . DEBUG && ! isWin ) {
130141 // treekill on windows uses taskkill and that ends up here always
0 commit comments