From 46cb5451172518f01e0be97380d55501b7c3e11f Mon Sep 17 00:00:00 2001 From: Rafael Araujo Lehmkuhl Date: Wed, 6 Nov 2024 17:04:58 -0300 Subject: [PATCH] maybe --- dev-app-update.yml | 3 + electron/main.ts | 65 +++++++++++++- electron/preload.ts | 11 +++ src/App.vue | 2 + src/components/UpdateNotification.vue | 123 ++++++++++++++++++++++++++ vite.config.ts | 6 ++ 6 files changed, 206 insertions(+), 4 deletions(-) create mode 100644 dev-app-update.yml create mode 100644 electron/preload.ts create mode 100644 src/components/UpdateNotification.vue diff --git a/dev-app-update.yml b/dev-app-update.yml new file mode 100644 index 000000000..d55dbcc61 --- /dev/null +++ b/dev-app-update.yml @@ -0,0 +1,3 @@ +owner: bluerobotics +repo: cockpit +provider: github \ No newline at end of file diff --git a/electron/main.ts b/electron/main.ts index 0454c4917..9efa706f0 100644 --- a/electron/main.ts +++ b/electron/main.ts @@ -1,4 +1,5 @@ -import { app, BrowserWindow, protocol, screen } from 'electron' +import { app, BrowserWindow, ipcMain, protocol, screen } from 'electron' +import electronUpdater, { type AppUpdater } from 'electron-updater' import { join } from 'path' export const ROOT_PATH = { @@ -15,9 +16,9 @@ function createWindow(): void { mainWindow = new BrowserWindow({ icon: join(ROOT_PATH.dist, 'pwa-512x512.png'), webPreferences: { - webSecurity: false, - contextIsolation: false, - nodeIntegration: true, + preload: join(__dirname, 'preload.js'), + contextIsolation: true, + nodeIntegration: false, allowRunningInsecureContent: true, }, width, @@ -62,6 +63,62 @@ protocol.registerSchemesAsPrivileged([ app.whenReady().then(createWindow) +/** + * Get auto updater instance + * @returns {AppUpdater} + * @see https://www.electron.build/auto-update + */ +function getAutoUpdater(): AppUpdater { + // Using destructuring to access autoUpdater due to the CommonJS module of 'electron-updater'. + // It is a workaround for ESM compatibility issues, see https://github.com/electron-userland/electron-builder/issues/7976. + const { autoUpdater } = electronUpdater + autoUpdater.logger = require('electron-log') + // @ts-ignore + autoUpdater.logger.transports.file.level = 'info' + return autoUpdater +} + +app.whenReady().then(() => { + console.log('Electron app is ready.') + console.log(`Cockpit version: ${app.getVersion()}`) + + const autoUpdater = getAutoUpdater() + autoUpdater.forceDevUpdateConfig = true // TODO: Remove this before merging + + autoUpdater.autoDownload = false // Prevent automatic downloads + + autoUpdater.on('update-available', (info) => { + mainWindow?.webContents.send('update-available', info) + }) + + autoUpdater.on('update-downloaded', (info) => { + mainWindow?.webContents.send('update-downloaded', info) + }) + + // Add handlers for update control + ipcMain.on('download-update', () => { + autoUpdater.downloadUpdate() + }) + + ipcMain.on('install-update', () => { + autoUpdater.quitAndInstall() + }) + + ipcMain.on('cancel-update', () => { + // Cancel any ongoing download + autoUpdater.removeAllListeners('update-downloaded') + autoUpdater.removeAllListeners('download-progress') + }) + + // Check for software updates + console.log('Will start update checking routine...') + + autoUpdater + .checkForUpdates() + .then((e) => console.log(e)) + .catch((e) => console.log(e)) +}) + app.on('before-quit', () => { // @ts-ignore: import.meta.env does not exist in the types if (import.meta.env.DEV) { diff --git a/electron/preload.ts b/electron/preload.ts new file mode 100644 index 000000000..94c0da540 --- /dev/null +++ b/electron/preload.ts @@ -0,0 +1,11 @@ +import { contextBridge, ipcRenderer } from 'electron' + +contextBridge.exposeInMainWorld('electronAPI', { + onUpdateAvailable: (callback: (info: any) => void) => + ipcRenderer.on('update-available', (_event, info) => callback(info)), + onUpdateDownloaded: (callback: (info: any) => void) => + ipcRenderer.on('update-downloaded', (_event, info) => callback(info)), + downloadUpdate: () => ipcRenderer.send('download-update'), + installUpdate: () => ipcRenderer.send('install-update'), + cancelUpdate: () => ipcRenderer.send('cancel-update'), +}) diff --git a/src/App.vue b/src/App.vue index fc19580ca..edb6d0ee2 100644 --- a/src/App.vue +++ b/src/App.vue @@ -315,6 +315,7 @@ + diff --git a/vite.config.ts b/vite.config.ts index 72c3d8fea..ffe459e8c 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -18,6 +18,12 @@ export default defineConfig({ vite: { build: { outDir: 'dist/electron', + rollupOptions: { + input: { + main: path.join(__dirname, 'electron/main.ts'), + preload: path.join(__dirname, 'electron/preload.ts') + } + } }, }, onstart: () => {