diff --git a/console/atest-desktop/index.html b/console/atest-desktop/index.html index 418c6b83..40fcdba3 100644 --- a/console/atest-desktop/index.html +++ b/console/atest-desktop/index.html @@ -38,6 +38,14 @@ + + + + + + + + Log @@ -118,10 +126,16 @@ window.electronAPI.setDownloadTimeout(downloadTimeout.value) }); +const binLocation = document.getElementById('bin-location'); +binLocation.addEventListener("input", function(e) { + window.electronAPI.setBinLocation(binLocation.value) +}); + (async function() { portInput.value = await window.electronAPI.getPort() extensionRegistry.value = await window.electronAPI.getExtensionRegistry() downloadTimeout.value = await window.electronAPI.getDownloadTimeout() + binLocation.value = await window.electronAPI.getBinLocation() document.getElementById('address').innerText = await window.electronAPI.getHomePage(); })(); diff --git a/console/atest-desktop/main.js b/console/atest-desktop/main.js index 7ed549fa..7f83a05e 100644 --- a/console/atest-desktop/main.js +++ b/console/atest-desktop/main.js @@ -120,6 +120,7 @@ let serverProcess; let serverPort = 7788; let extensionRegistry = "ghcr.io"; let downloadTimeout = "1m"; +let mainProcessLocation = "built-in"; // This method will be called when Electron has finished // initialization and is ready to create browser windows. @@ -156,6 +157,12 @@ app.whenReady().then(() => { }) ipcMain.handle('getHomePage', server.getHomePage) ipcMain.handle('getHealthzUrl', server.getHealthzUrl) + ipcMain.handle('getMainProcessLocation', () => { + return mainProcessLocation + }) + ipcMain.handle('setMainProcessLocation', (e, location) => { + mainProcessLocation = location + }) startServer() createWindow() @@ -178,28 +185,21 @@ const startServer = () => { recursive: true }) - // try to find the atest file first - const serverFile = process.platform === "win32" ? "atest.exe" : "atest" - const atestFromHome = path.join(homeBin, serverFile) - const atestFromPkg = path.join(__dirname, serverFile) - - const data = fs.readFileSync(atestFromPkg) - log.info('start to write file with length', data.length) - - try { - if (process.platform === "win32") { - const file = fs.openSync(atestFromHome, 'w'); - fs.writeSync(file, data, 0, data.length, 0); - fs.closeSync(file); - }else{ - fs.writeFileSync(atestFromHome, data); + let atestBinPath + switch (mainProcessLocation) { + case "built-in": + atestBinPath = locateBinPath() + break; + case "system-path": + const which = require('which'); + atestBinPath = process.platform === "win32" ? which.sync('atest.exe') : which.sync('atest') + break; + case "home-path": + atestBinPath = locateBinPath(false) + break; } - } catch (e) { - log.error('Error Code:', e.code); - } - fs.chmodSync(atestFromHome, 0o755); - serverProcess = spawn(atestFromHome, [ + serverProcess = spawn(atestBinPath, [ "server", `--http-port=${serverPort}`, "--port=0", @@ -223,6 +223,33 @@ const startServer = () => { log.info(serverProcess.spawnargs) } +const locateBinPath = (overwrite = true) => { + // try to find the atest file first + const serverFile = process.platform === "win32" ? "atest.exe" : "atest" + const atestFromHome = path.join(homeBin, serverFile) + if (!overwrite) { + return atestFromHome + } + + const atestFromPkg = path.join(__dirname, serverFile) + const data = fs.readFileSync(atestFromPkg) + log.info('start to write file with length', data.length) + + try { + if (process.platform === "win32") { + const file = fs.openSync(atestFromHome, 'w'); + fs.writeSync(file, data, 0, data.length, 0); + fs.closeSync(file); + }else{ + fs.writeFileSync(atestFromHome, data); + } + } catch (e) { + log.error('Error Code:', e.code); + } + fs.chmodSync(atestFromHome, 0o755); + return atestFromHome +} + const stopServer = () => { if (serverProcess) { serverProcess.kill() diff --git a/console/atest-desktop/preload.js b/console/atest-desktop/preload.js index 0c1d8875..260c887d 100644 --- a/console/atest-desktop/preload.js +++ b/console/atest-desktop/preload.js @@ -1,5 +1,5 @@ /* -Copyright 2024 API Testing Authors. +Copyright 2024-2025 API Testing Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -41,6 +41,8 @@ contextBridge.exposeInMainWorld('electronAPI', { setExtensionRegistry: (registry) => ipcRenderer.invoke('setExtensionRegistry', registry), getExtensionRegistry: () => ipcRenderer.invoke('getExtensionRegistry'), getDownloadTimeout: () => ipcRenderer.invoke('getDownloadTimeout'), - setDownloadTimeout: (timeout) => ipcRenderer.invoke('setDownloadTimeout', timeout), + setDownloadTimeout: (e) => ipcRenderer.invoke('setDownloadTimeout', e), + getMainProcessLocation: () => ipcRenderer.invoke('getMainProcessLocation'), + setMainProcessLocation: (e) => ipcRenderer.invoke('setMainProcessLocation', e), getHealthzUrl: () => ipcRenderer.invoke('getHealthzUrl'), })