@@ -5,6 +5,7 @@ import FileSystem from "./file-system";
55import Network from "./network" ;
66
77const child_process = require ( 'child_process' ) ;
8+ const fs = require ( 'fs' ) ;
89
910export default class Update {
1011
@@ -139,6 +140,7 @@ export default class Update {
139140 let startFile = this . prefix . getBinDir ( ) + '/start' ;
140141 let updateFile = this . prefix . getCacheDir ( ) + '/start' ;
141142 let updateScriptFile = this . prefix . getCacheDir ( ) + '/update.sh' ;
143+ let log = this . prefix . getCacheDir ( ) + '/update.log' ;
142144
143145 if ( ! this . fs . exists ( startFile ) ) {
144146 startFile = this . prefix . getRootDir ( ) + '/start' ;
@@ -152,13 +154,23 @@ export default class Update {
152154 this . fs . rm ( updateFile ) ;
153155 }
154156
157+ if ( this . fs . exists ( updateScriptFile ) ) {
158+ this . fs . rm ( updateScriptFile ) ;
159+ }
160+
161+ if ( this . fs . exists ( log ) ) {
162+ this . fs . rm ( log ) ;
163+ }
164+
155165 const updateScript = `#!/usr/bin/env sh
156166
157167processPid=${ window . process . pid }
158168startFile="${ startFile } "
159169updateFile="${ updateFile } "
160170iterator=0
161171
172+ echo "Start to update"
173+
162174while [ "$(ps -p $processPid -o comm=)" != "" ]; do
163175 sleep 1
164176 iterator=$((iterator + 1))
@@ -189,18 +201,33 @@ rm -rf "${updateScriptFile}"`;
189201 this . fs . filePutContents ( updateScriptFile , updateScript ) ;
190202 this . fs . chmod ( updateScriptFile ) ;
191203
192- try {
193- const child = child_process . spawn ( updateScriptFile , [ ] , {
194- detached : true ,
195- stdio : [ 'ignore' ]
196- } ) ;
197-
198- child . unref ( ) ;
199- } catch ( e ) {
200- }
201-
202- return Utils . sleep ( 1000 ) . then ( ( ) => window . app . getSystem ( ) . closeApp ( ) ) ;
203- } ) ;
204+ return new Promise ( ( resolve ) => {
205+ try {
206+ let isNext = false ;
207+ const next = ( ) => {
208+ if ( ! isNext ) {
209+ isNext = true ;
210+ resolve ( ) ;
211+ }
212+ } ;
213+
214+ let out = fs . openSync ( log , 'a' ) ;
215+ let err = fs . openSync ( log , 'a' ) ;
216+
217+ let child = child_process . spawn ( updateScriptFile , [ ] , {
218+ detached : true ,
219+ stdio : [ 'ignore' , out , err ]
220+ } ) ;
221+
222+ child . unref ( ) ;
223+ child . stdout . on ( 'data' , ( log ) => next ( log ) ) ;
224+ child . stderr . on ( 'data' , ( log ) => next ( log ) ) ;
225+ } catch ( e ) {
226+ resolve ( ) ;
227+ }
228+ } ) ;
229+ } )
230+ . then ( ( ) => window . app . getSystem ( ) . closeApp ( ) ) ;
204231 } ) ;
205232 }
206233}
0 commit comments