diff --git a/electron/main.ts b/electron/main.ts index 0454c4917..cea496c6a 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 = { @@ -7,17 +8,32 @@ export const ROOT_PATH = { let mainWindow: BrowserWindow | null +/** + * 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 +} + /** * Create electron window */ -function createWindow(): void { +async function createWindow(): Promise { const { width, height } = screen.getPrimaryDisplay().workAreaSize 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, @@ -25,14 +41,14 @@ function createWindow(): void { }) // Test active push message to Renderer-process. - mainWindow.webContents.on('did-finish-load', () => { - mainWindow?.webContents.send('main-process-message', new Date().toLocaleString()) + mainWindow!.webContents.on('did-finish-load', () => { + mainWindow!.webContents.send('main-process-message', new Date().toLocaleString()) }) if (process.env.VITE_DEV_SERVER_URL) { - mainWindow.loadURL(process.env.VITE_DEV_SERVER_URL) + mainWindow!.loadURL(process.env.VITE_DEV_SERVER_URL) } else { - mainWindow.loadFile(join(ROOT_PATH.dist, 'index.html')) + mainWindow!.loadFile(join(ROOT_PATH.dist, 'index.html')) } } @@ -60,7 +76,64 @@ protocol.registerSchemesAsPrivileged([ }, ]) -app.whenReady().then(createWindow) +app.whenReady().then(async () => { + console.log('Electron app is ready.') + console.log(`Cockpit version: ${app.getVersion()}`) + + console.log('Creating window...') + await createWindow() + + console.log('Setting up auto updater...') + setTimeout(() => { + setupAutoUpdater() + }, 10000) +}) + +const setupAutoUpdater = (): void => { + const autoUpdater = getAutoUpdater() + autoUpdater.forceDevUpdateConfig = true // TODO: Remove this before merging + autoUpdater.autoDownload = false // Prevent automatic downloads + + autoUpdater + .checkForUpdates() + .then((e) => console.log(e)) + .catch((e) => console.log(e)) + + autoUpdater.on('checking-for-update', () => { + mainWindow!.webContents.send('checking-for-update') + }) + + autoUpdater.on('update-available', (info) => { + mainWindow!.webContents.send('update-available', info) + }) + + autoUpdater.on('update-not-available', (info) => { + mainWindow!.webContents.send('update-not-available', info) + }) + + autoUpdater.on('download-progress', (progressInfo) => { + mainWindow!.webContents.send('download-progress', progressInfo) + }) + + 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') + }) +} app.on('before-quit', () => { // @ts-ignore: import.meta.env does not exist in the types diff --git a/electron/preload.ts b/electron/preload.ts new file mode 100644 index 000000000..935576a0a --- /dev/null +++ b/electron/preload.ts @@ -0,0 +1,17 @@ +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)), + onCheckingForUpdate: (callback: () => void) => + ipcRenderer.on('checking-for-update', (_event) => callback()), + onUpdateNotAvailable: (callback: (info: any) => void) => + ipcRenderer.on('update-not-available', (_event, info) => callback(info)), + onDownloadProgress: (callback: (info: any) => void) => + ipcRenderer.on('download-progress', (_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..7636b40ac 100644 --- a/src/App.vue +++ b/src/App.vue @@ -315,6 +315,7 @@ +