Repository navigation
feat(electron): add auto-update functionality with electron-updater #221
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -26,10 +26,13 @@ const { | |||||
| nativeImage, | ||||||
| shell, | ||||||
| session, | ||||||
| dialog, | ||||||
| Notification, | ||||||
| } = require("electron"); | ||||||
| const path = require("path"); | ||||||
| const { spawn } = require("child_process"); | ||||||
| const fs = require("fs"); | ||||||
| const { autoUpdater } = require("electron-updater"); | ||||||
|
|
||||||
| // ── Single Instance Lock ─────────────────────────────────── | ||||||
| const gotTheLock = app.requestSingleInstanceLock(); | ||||||
|
|
@@ -62,6 +65,11 @@ let serverPort = 20128; | |||||
|
|
||||||
| const getServerUrl = () => `http://localhost:${serverPort}`; | ||||||
|
|
||||||
| // ── Auto-Updater Configuration ────────────────────────────── | ||||||
| autoUpdater.autoDownload = false; | ||||||
| autoUpdater.autoInstallOnAppQuit = true; | ||||||
| autoUpdater.logger = console; | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SUGGESTION: Using
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While using
Suggested change
|
||||||
|
|
||||||
| // ── Helper: Send IPC event to renderer (#5) ──────────────── | ||||||
| function sendToRenderer(channel, data) { | ||||||
| if (mainWindow && !mainWindow.isDestroyed()) { | ||||||
|
|
@@ -103,6 +111,85 @@ async function waitForServerExit(proc, timeoutMs = 5000) { | |||||
| ]); | ||||||
| } | ||||||
|
|
||||||
| // ── Auto-Updater Event Handlers ───────────────────────────── | ||||||
| function setupAutoUpdater() { | ||||||
| autoUpdater.on("checking-for-update", () => { | ||||||
| sendToRenderer("update-status", { status: "checking" }); | ||||||
| console.log("[Electron] Checking for updates..."); | ||||||
| }); | ||||||
|
|
||||||
| autoUpdater.on("update-available", (info) => { | ||||||
| sendToRenderer("update-status", { status: "available", version: info.version }); | ||||||
| console.log("[Electron] Update available:", info.version); | ||||||
| }); | ||||||
|
|
||||||
| autoUpdater.on("update-not-available", (info) => { | ||||||
| sendToRenderer("update-status", { status: "not-available", version: info.version }); | ||||||
| console.log("[Electron] No update available"); | ||||||
| }); | ||||||
|
|
||||||
| autoUpdater.on("download-progress", (progress) => { | ||||||
| sendToRenderer("update-status", { | ||||||
| status: "downloading", | ||||||
| percent: Math.round(progress.percent), | ||||||
| transferred: progress.transferred, | ||||||
| total: progress.total, | ||||||
| }); | ||||||
| }); | ||||||
|
|
||||||
| autoUpdater.on("update-downloaded", (info) => { | ||||||
| sendToRenderer("update-status", { status: "downloaded", version: info.version }); | ||||||
| console.log("[Electron] Update downloaded:", info.version); | ||||||
|
|
||||||
| if (Notification.isSupported()) { | ||||||
| const notification = new Notification({ | ||||||
| title: "OmniRoute Update Ready", | ||||||
| body: `Version ${info.version} is ready to install. Click to restart.`, | ||||||
| }); | ||||||
| notification.on("click", () => { | ||||||
| autoUpdater.quitAndInstall(); | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WARNING: |
||||||
| }); | ||||||
| notification.show(); | ||||||
| } | ||||||
| }); | ||||||
|
|
||||||
| autoUpdater.on("error", (error) => { | ||||||
| sendToRenderer("update-status", { status: "error", message: error.message }); | ||||||
| console.error("[Electron] Update error:", error); | ||||||
| }); | ||||||
| } | ||||||
|
|
||||||
| async function checkForUpdates(silent = false) { | ||||||
| if (isDev) { | ||||||
| console.log("[Electron] Dev mode — skipping auto-update"); | ||||||
| if (!silent) { | ||||||
| sendToRenderer("update-status", { status: "error", message: "Updates disabled in dev mode" }); | ||||||
| } | ||||||
| return; | ||||||
| } | ||||||
| try { | ||||||
| await autoUpdater.checkForUpdates(); | ||||||
| } catch (error) { | ||||||
| console.error("[Electron] Check for updates failed:", error); | ||||||
| if (!silent) { | ||||||
| sendToRenderer("update-status", { status: "error", message: error.message }); | ||||||
| } | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This console.error("[Electron] Check for updates failed:", error);
if (!silent) {
sendToRenderer("update-status", { status: "error", message: error.message });
}
throw error; |
||||||
| } | ||||||
| } | ||||||
|
|
||||||
| async function downloadUpdate() { | ||||||
| try { | ||||||
| await autoUpdater.downloadUpdate(); | ||||||
| } catch (error) { | ||||||
| console.error("[Electron] Download update failed:", error); | ||||||
| sendToRenderer("update-status", { status: "error", message: error.message }); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to the console.error("[Electron] Download update failed:", error);
sendToRenderer("update-status", { status: "error", message: error.message });
throw error; |
||||||
| } | ||||||
| } | ||||||
|
|
||||||
| function installUpdate() { | ||||||
| autoUpdater.quitAndInstall(); | ||||||
| } | ||||||
|
|
||||||
| // ── Content Security Policy (#15) ────────────────────────── | ||||||
| function setupContentSecurityPolicy() { | ||||||
| session.defaultSession.webRequest.onHeadersReceived((details, callback) => { | ||||||
|
|
@@ -236,6 +323,11 @@ function createTray() { | |||||
| ], | ||||||
| }, | ||||||
| { type: "separator" }, | ||||||
| { | ||||||
| label: "Check for Updates", | ||||||
| click: () => checkForUpdates(false), | ||||||
| }, | ||||||
| { type: "separator" }, | ||||||
| { | ||||||
| label: "Quit", | ||||||
| click: () => { | ||||||
|
|
@@ -391,6 +483,24 @@ function setupIpcHandlers() { | |||||
| }); | ||||||
|
|
||||||
| ipcMain.on("window-close", () => mainWindow?.close()); | ||||||
|
|
||||||
| // Auto-update IPC handlers | ||||||
| ipcMain.handle("check-for-updates", async () => { | ||||||
| await checkForUpdates(false); | ||||||
| return { success: true }; | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WARNING: This IPC handler returns |
||||||
| }); | ||||||
|
|
||||||
| ipcMain.handle("download-update", async () => { | ||||||
| await downloadUpdate(); | ||||||
| return { success: true }; | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WARNING: This IPC handler returns |
||||||
| }); | ||||||
|
|
||||||
| ipcMain.handle("install-update", () => { | ||||||
| installUpdate(); | ||||||
| return { success: true }; | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WARNING: This IPC handler returns |
||||||
| }); | ||||||
|
|
||||||
| ipcMain.handle("get-app-version", () => app.getVersion()); | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CRITICAL: The |
||||||
| } | ||||||
|
|
||||||
| // ── App Lifecycle ────────────────────────────────────────── | ||||||
|
|
@@ -407,6 +517,14 @@ app.whenReady().then(async () => { | |||||
| createWindow(); | ||||||
| createTray(); | ||||||
| setupIpcHandlers(); | ||||||
| setupAutoUpdater(); | ||||||
|
|
||||||
| // Check for updates after a short delay (don't block startup) | ||||||
| if (!isDev) { | ||||||
| setTimeout(() => { | ||||||
| checkForUpdates(true); | ||||||
| }, 3000); | ||||||
| } | ||||||
|
|
||||||
| // macOS: recreate window when dock icon clicked | ||||||
| app.on("activate", () => { | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,7 +15,9 @@ | |
| "build:linux": "electron-builder --linux", | ||
| "pack": "electron-builder --dir" | ||
| }, | ||
| "dependencies": {}, | ||
| "dependencies": { | ||
| "electron-updater": "^6.8.3" | ||
| }, | ||
| "devDependencies": { | ||
| "electron": "^40.6.1", | ||
| "electron-builder": "^25.1.8" | ||
|
|
@@ -28,6 +30,11 @@ | |
| "output": "dist-electron", | ||
| "buildResources": "assets" | ||
| }, | ||
| "publish": { | ||
| "provider": "github", | ||
| "owner": "diegosouzapw", | ||
| "repo": "OmniRoute" | ||
| }, | ||
|
Comment on lines
+33
to
+37
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hardcoding the "repository": {
"type": "git",
"url": "https://github.com/diegosouzapw/OmniRoute.git"
} "publish": {
"provider": "github"
}, |
||
| "files": [ | ||
| "main.js", | ||
| "preload.js", | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WARNING: Unused import -
dialogis imported but never used in the codebase. Either remove this import or implement dialog functionality.