|
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"); |
2 | 2 | const path = require("node:path"); |
| 3 | +const { autoUpdater } = require("electron-updater"); |
3 | 4 | const fs = require("node:fs"); |
4 | 5 | const { execFile, execFileSync, spawn } = require("node:child_process"); |
5 | 6 | const util = require("node:util"); |
@@ -675,7 +676,62 @@ safeHandle("app:import-commands", async () => { |
675 | 676 | safeHandle("app:list-system-processes", async () => listMatchedSystemProcesses(loadCommands(), getStatuses())); |
676 | 677 | safeHandle("app:kill-system-process", async (_event, pid) => await killSystemProcess(pid, terminateProcess)); |
677 | 678 |
|
| 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 | + |
678 | 733 | app.whenReady().then(async () => { |
| 734 | + setupAutoUpdater(); |
679 | 735 | hydrateRuntime(); |
680 | 736 | saveSettings(loadSettings()); |
681 | 737 | ensureTray(); |
|
0 commit comments