11import _ from "lodash" ;
2+ import Utils from "./utils" ;
23import Prefix from "./prefix" ;
34import FileSystem from "./file-system" ;
45import Network from "./network" ;
56
7+ const child_process = require ( 'child_process' ) ;
8+
69export default class Update {
710
8- version = '1.4.10 ' ;
11+ version = '1.4.11 ' ;
912
1013 /**
1114 * @type {string }
@@ -128,4 +131,76 @@ export default class Update {
128131 return _ . trimStart ( last . tag_name , 'v' ) ;
129132 } ) ;
130133 }
134+
135+ /**
136+ * @return {Promise<void> }
137+ */
138+ updateSelf ( ) {
139+ let startFile = this . prefix . getBinDir ( ) + '/start' ;
140+ let updateFile = this . prefix . getCacheDir ( ) + '/start' ;
141+ let updateScriptFile = this . prefix . getCacheDir ( ) + '/update.sh' ;
142+
143+ if ( ! this . fs . exists ( startFile ) ) {
144+ startFile = this . prefix . getRootDir ( ) + '/start' ;
145+ }
146+
147+ if ( ! this . fs . exists ( startFile ) ) {
148+ return Promise . resolve ( ) ;
149+ }
150+
151+ if ( this . fs . exists ( updateFile ) ) {
152+ this . fs . rm ( updateFile ) ;
153+ }
154+
155+ const updateScript = `#!/usr/bin/env sh
156+
157+ processPid=${ window . process . pid }
158+ startFile="${ startFile } "
159+ updateFile="${ updateFile } "
160+ iterator=0
161+
162+ while [ "$(ps -p $processPid -o comm=)" != "" ]; do
163+ sleep 1
164+ iterator=$((iterator + 1))
165+
166+ if [ $iterator -gt 120 ]; then
167+ echo "Error update, exit."
168+ exit
169+ fi
170+
171+ echo "Waiting for process to complete"
172+ done
173+
174+ rm -rf "$startFile"
175+ mv "$updateFile" "$startFile"
176+ chmod +x "$startFile"
177+
178+ "$startFile" &
179+ rm -rf "${ updateScriptFile } "` ;
180+
181+ let promise = this . getRemoteVersion ( ) . then ( ( ) => this . data ) ;
182+
183+ return promise . then ( ( data ) => {
184+ let last = _ . head ( data ) ;
185+ last = _ . head ( last . assets ) ;
186+
187+ return this . network . download ( last . browser_download_url , updateFile )
188+ . then ( ( ) => {
189+ this . fs . filePutContents ( updateScriptFile , updateScript ) ;
190+ this . fs . chmod ( updateScriptFile ) ;
191+
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+ } ) ;
205+ }
131206}
0 commit comments