From f9e0153c2455958dae2c310bcce70da65f3eab07 Mon Sep 17 00:00:00 2001 From: Christopher Davis Date: Thu, 17 Oct 2024 11:17:18 -0400 Subject: [PATCH] Support Headless Auto-Update #8 --- api/src/lib/server.ts | 12 +++-- electron/src/main/index.ts | 23 +++++--- ui/src/lib/UpdateStatus.svelte | 98 +++++----------------------------- 3 files changed, 37 insertions(+), 96 deletions(-) diff --git a/api/src/lib/server.ts b/api/src/lib/server.ts index 799c66d..416794b 100644 --- a/api/src/lib/server.ts +++ b/api/src/lib/server.ts @@ -50,6 +50,7 @@ process.on('unhandledRejection', (reason, promise) => { export let version = new State('version', '') export let updateChannel = new State('updateChannel', undefined, { persist: true }) +export let updateStatus = new State('updateStatus', {}) export let app: Express = express() app.use(express.json({ limit: '500mb' })) @@ -100,13 +101,16 @@ async function initSever() { // Electron Hook if present let parentPort = process['parentPort'] - parentPort?.on('message', (e) => { + parentPort?.on('message', (e: any) => { console.log('[electron to api]', e) - if (e.data.event == 'version') version.set(e.data.body) - if (e.data.event == 'updateChannel') { + if (e.data.event == 'version') { + version.set(e.data.body) + } else if (e.data.event == 'updateChannel') { if (e.data.body) updateChannel.set(e.data.body) + } else if (['update-available', 'download-progress', 'update-downloaded'].includes(e.data.event)) { + updateStatus.set(e.data) } - wss?.send(e.data.event, e.data.body) + // wss?.send(e.data.event, e.data.body) }) updateChannel.subscribe((v) => { diff --git a/electron/src/main/index.ts b/electron/src/main/index.ts index 2943902..418737d 100644 --- a/electron/src/main/index.ts +++ b/electron/src/main/index.ts @@ -7,6 +7,16 @@ import { autoUpdater } from 'electron-updater' let apiProcess: Electron.UtilityProcess let apiPort: any = 9999 +/** asnyc needed for updateCheckLoop to allow electron to launch main window */ +async function updateCheckLoop() { + console.log('[electron] Checking for updates on channel', autoUpdater.channel) + autoUpdater.checkForUpdates() + setInterval(() => { + autoUpdater.checkForUpdates() + }, 7.2e6) + // autoUpdater.checkForUpdatesAndNotify({ title: 'MeshSense', body: 'MeshSense has an update!' }) +} + function createWindow(): void { // Create the browser window. const mainWindow = new BrowserWindow({ @@ -22,12 +32,6 @@ function createWindow(): void { }) mainWindow.on('ready-to-show', () => { - console.log('[electron] Checking for updates on channel', autoUpdater.channel) - autoUpdater.checkForUpdates() - setInterval(() => { - autoUpdater.checkForUpdates() - }, 7.2e6) - // autoUpdater.checkForUpdatesAndNotify({ title: 'MeshSense', body: 'MeshSense has an update!' }) mainWindow.show() }) @@ -73,7 +77,7 @@ app.whenReady().then(async () => { } console.log('[electron] Arguments', process.argv) - if (!process.argv.includes('--headless')) { + if (!process.env['HEADLESS']) { apiProcess.stdout?.on('data', createWindowOnServerListening) } apiProcess.postMessage({ event: 'version', body: app.getVersion() }) @@ -129,6 +133,11 @@ app.whenReady().then(async () => { // dock icon is clicked and there are no other windows open. if (BrowserWindow.getAllWindows().length === 0) createWindow() }) + + updateCheckLoop() + // setTimeout(() => { + // autoUpdater.quitAndInstall() + // }, 3000) }) // Quit when all windows are closed, except on macOS. There, it's common diff --git a/ui/src/lib/UpdateStatus.svelte b/ui/src/lib/UpdateStatus.svelte index 81f9239..12f914e 100644 --- a/ui/src/lib/UpdateStatus.svelte +++ b/ui/src/lib/UpdateStatus.svelte @@ -1,99 +1,27 @@