Skip to content

Commit dc3e2ba

Browse files
committed
feat: add auto-update support via electron-updater
1 parent a4aa639 commit dc3e2ba

3 files changed

Lines changed: 239 additions & 17 deletions

File tree

electron/main.js

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
const { app, BrowserWindow, dialog, ipcMain, Menu, Tray, nativeImage, nativeTheme, shell } = require("electron");
1+
const { app, BrowserWindow, dialog, ipcMain, Menu, Tray, nativeImage, nativeTheme, shell } = require("electron");
22
const path = require("node:path");
3+
const { autoUpdater } = require("electron-updater");
34
const fs = require("node:fs");
45
const { execFile, execFileSync, spawn } = require("node:child_process");
56
const util = require("node:util");
@@ -675,7 +676,62 @@ safeHandle("app:import-commands", async () => {
675676
safeHandle("app:list-system-processes", async () => listMatchedSystemProcesses(loadCommands(), getStatuses()));
676677
safeHandle("app:kill-system-process", async (_event, pid) => await killSystemProcess(pid, terminateProcess));
677678

679+
function setupAutoUpdater() {
680+
if (isDev) {
681+
console.log("[AutoUpdate] Skipped in dev");
682+
return;
683+
}
684+
685+
autoUpdater.autoDownload = true;
686+
autoUpdater.autoInstallOnAppQuit = true;
687+
688+
autoUpdater.on("checking-for-update", () => {
689+
console.log("[AutoUpdate] Checking for updates...");
690+
});
691+
692+
autoUpdater.on("update-available", (info) => {
693+
console.log("[AutoUpdate] Update available:", info.version);
694+
if (mainWindow && !mainWindow.isDestroyed()) {
695+
mainWindow.webContents.send("update:available", info);
696+
}
697+
});
698+
699+
autoUpdater.on("download-progress", (progress) => {
700+
const pct = Math.round(progress.percent);
701+
if (mainWindow && !mainWindow.isDestroyed()) {
702+
mainWindow.webContents.send("update:progress", { percent: pct, transferred: progress.transferred, total: progress.total });
703+
}
704+
});
705+
706+
autoUpdater.on("update-downloaded", (info) => {
707+
console.log("[AutoUpdate] Update downloaded:", info.version);
708+
if (mainWindow && !mainWindow.isDestroyed()) {
709+
mainWindow.webContents.send("update:downloaded", info);
710+
}
711+
const choice = dialog.showMessageBoxSync(mainWindow, {
712+
type: "info",
713+
title: "Update Ready",
714+
message: "A new version has been downloaded. Restart now to apply?",
715+
buttons: ["Restart Now", "Later"]
716+
});
717+
if (choice === 0) {
718+
autoUpdater.quitAndInstall();
719+
}
720+
});
721+
722+
autoUpdater.on("update-not-available", (info) => {
723+
console.log("[AutoUpdate] Already up-to-date:", info.version);
724+
});
725+
726+
autoUpdater.on("error", (err) => {
727+
console.error("[AutoUpdate] Error:", err);
728+
});
729+
730+
autoUpdater.checkForUpdates();
731+
}
732+
678733
app.whenReady().then(async () => {
734+
setupAutoUpdater();
679735
hydrateRuntime();
680736
saveSettings(loadSettings());
681737
ensureTray();

0 commit comments

Comments
 (0)