@@ -120,6 +120,7 @@ let serverProcess;
120120let serverPort = 7788 ;
121121let extensionRegistry = "ghcr.io" ;
122122let downloadTimeout = "1m" ;
123+ let mainProcessLocation = "built-in" ;
123124
124125// This method will be called when Electron has finished
125126// initialization and is ready to create browser windows.
@@ -156,6 +157,12 @@ app.whenReady().then(() => {
156157 } )
157158 ipcMain . handle ( 'getHomePage' , server . getHomePage )
158159 ipcMain . handle ( 'getHealthzUrl' , server . getHealthzUrl )
160+ ipcMain . handle ( 'getMainProcessLocation' , ( ) => {
161+ return mainProcessLocation
162+ } )
163+ ipcMain . handle ( 'setMainProcessLocation' , ( e , location ) => {
164+ mainProcessLocation = location
165+ } )
159166
160167 startServer ( )
161168 createWindow ( )
@@ -178,28 +185,21 @@ const startServer = () => {
178185 recursive : true
179186 } )
180187
181- // try to find the atest file first
182- const serverFile = process . platform === "win32" ? "atest.exe" : "atest"
183- const atestFromHome = path . join ( homeBin , serverFile )
184- const atestFromPkg = path . join ( __dirname , serverFile )
185-
186- const data = fs . readFileSync ( atestFromPkg )
187- log . info ( 'start to write file with length' , data . length )
188-
189- try {
190- if ( process . platform === "win32" ) {
191- const file = fs . openSync ( atestFromHome , 'w' ) ;
192- fs . writeSync ( file , data , 0 , data . length , 0 ) ;
193- fs . closeSync ( file ) ;
194- } else {
195- fs . writeFileSync ( atestFromHome , data ) ;
188+ let atestBinPath
189+ switch ( mainProcessLocation ) {
190+ case "built-in" :
191+ atestBinPath = locateBinPath ( )
192+ break ;
193+ case "system-path" :
194+ const which = require ( 'which' ) ;
195+ atestBinPath = process . platform === "win32" ? which . sync ( 'atest.exe' ) : which . sync ( 'atest' )
196+ break ;
197+ case "home-path" :
198+ atestBinPath = locateBinPath ( false )
199+ break ;
196200 }
197- } catch ( e ) {
198- log . error ( 'Error Code:' , e . code ) ;
199- }
200- fs . chmodSync ( atestFromHome , 0o755 ) ;
201201
202- serverProcess = spawn ( atestFromHome , [
202+ serverProcess = spawn ( atestBinPath , [
203203 "server" ,
204204 `--http-port=${ serverPort } ` ,
205205 "--port=0" ,
@@ -223,6 +223,33 @@ const startServer = () => {
223223 log . info ( serverProcess . spawnargs )
224224}
225225
226+ const locateBinPath = ( overwrite = true ) => {
227+ // try to find the atest file first
228+ const serverFile = process . platform === "win32" ? "atest.exe" : "atest"
229+ const atestFromHome = path . join ( homeBin , serverFile )
230+ if ( ! overwrite ) {
231+ return atestFromHome
232+ }
233+
234+ const atestFromPkg = path . join ( __dirname , serverFile )
235+ const data = fs . readFileSync ( atestFromPkg )
236+ log . info ( 'start to write file with length' , data . length )
237+
238+ try {
239+ if ( process . platform === "win32" ) {
240+ const file = fs . openSync ( atestFromHome , 'w' ) ;
241+ fs . writeSync ( file , data , 0 , data . length , 0 ) ;
242+ fs . closeSync ( file ) ;
243+ } else {
244+ fs . writeFileSync ( atestFromHome , data ) ;
245+ }
246+ } catch ( e ) {
247+ log . error ( 'Error Code:' , e . code ) ;
248+ }
249+ fs . chmodSync ( atestFromHome , 0o755 ) ;
250+ return atestFromHome
251+ }
252+
226253const stopServer = ( ) => {
227254 if ( serverProcess ) {
228255 serverProcess . kill ( )
0 commit comments