|
| 1 | +import { app, BrowserWindow } from 'electron'; |
| 2 | +import { autoUpdater } from 'electron-updater'; |
| 3 | + |
| 4 | +let updateCheckInterval: NodeJS.Timeout | null = null; |
| 5 | +let getMainWindowRef: (() => BrowserWindow | null) | null = null; |
| 6 | + |
| 7 | +/** |
| 8 | + * Log helper that sends log to both main process console and renderer process |
| 9 | + */ |
| 10 | +function logOTA(message: string) { |
| 11 | + console.log(`[OTA] ${message}`); |
| 12 | + if (getMainWindowRef) { |
| 13 | + const mainWindow = getMainWindowRef(); |
| 14 | + if (mainWindow && !mainWindow.isDestroyed()) { |
| 15 | + mainWindow.webContents.send('ota-log', message); |
| 16 | + } |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * Initialize auto updater with GitHub as the update source |
| 22 | + */ |
| 23 | +function initAutoUpdater(getMainWindow: () => BrowserWindow | null) { |
| 24 | + getMainWindowRef = getMainWindow; |
| 25 | + logOTA('initAutoUpdater called'); |
| 26 | + try { |
| 27 | + autoUpdater.autoDownload = true; |
| 28 | + autoUpdater.autoInstallOnAppQuit = true; |
| 29 | + autoUpdater.allowPrerelease = true; // Allow beta/alpha versions |
| 30 | + autoUpdater.allowDowngrade = false; |
| 31 | + |
| 32 | + if (app.isPackaged) { |
| 33 | + autoUpdater.setFeedURL({ |
| 34 | + provider: 'github', |
| 35 | + owner: 'ExpTechTW', |
| 36 | + repo: 'ES-Net-Wave', |
| 37 | + vPrefixedTagName: false, |
| 38 | + }); |
| 39 | + } |
| 40 | + |
| 41 | + // Event handlers |
| 42 | + autoUpdater.on('checking-for-update', () => { |
| 43 | + logOTA('Checking for update...'); |
| 44 | + const mainWindow = getMainWindow(); |
| 45 | + if (mainWindow && !mainWindow.isDestroyed()) { |
| 46 | + mainWindow.webContents.send('update-checking'); |
| 47 | + } |
| 48 | + }); |
| 49 | + |
| 50 | + autoUpdater.on('update-available', (info) => { |
| 51 | + logOTA(`Update available: ${info.version}`); |
| 52 | + const mainWindow = getMainWindow(); |
| 53 | + if (mainWindow && !mainWindow.isDestroyed()) { |
| 54 | + mainWindow.webContents.send('update-available', info); |
| 55 | + } |
| 56 | + }); |
| 57 | + |
| 58 | + autoUpdater.on('update-not-available', (info) => { |
| 59 | + logOTA(`Update not available: ${info.version}`); |
| 60 | + const mainWindow = getMainWindow(); |
| 61 | + if (mainWindow && !mainWindow.isDestroyed()) { |
| 62 | + mainWindow.webContents.send('update-not-available', info); |
| 63 | + } |
| 64 | + }); |
| 65 | + |
| 66 | + autoUpdater.on('download-progress', (progressObj) => { |
| 67 | + logOTA(`Download progress: ${progressObj.percent.toFixed(2)}%`); |
| 68 | + const mainWindow = getMainWindow(); |
| 69 | + if (mainWindow && !mainWindow.isDestroyed()) { |
| 70 | + mainWindow.webContents.send('download-progress', progressObj); |
| 71 | + } |
| 72 | + }); |
| 73 | + |
| 74 | + autoUpdater.on('update-downloaded', (info) => { |
| 75 | + logOTA(`Update downloaded: ${info.version}`); |
| 76 | + const mainWindow = getMainWindow(); |
| 77 | + if (mainWindow && !mainWindow.isDestroyed()) { |
| 78 | + mainWindow.webContents.send('update-downloaded', info); |
| 79 | + } |
| 80 | + // Wait 3 seconds before installing |
| 81 | + setTimeout(() => { |
| 82 | + autoUpdater.quitAndInstall(true, true); |
| 83 | + }, 3000); |
| 84 | + }); |
| 85 | + |
| 86 | + autoUpdater.on('error', (err) => { |
| 87 | + const message = err && err.message ? err.message : String(err); |
| 88 | + logOTA(`Update error: ${message}`); |
| 89 | + const mainWindow = getMainWindow(); |
| 90 | + if (mainWindow && !mainWindow.isDestroyed()) { |
| 91 | + mainWindow.webContents.send('update-error', message); |
| 92 | + } |
| 93 | + }); |
| 94 | + |
| 95 | + startAutoUpdateScheduler(); |
| 96 | + logOTA('Auto updater initialized'); |
| 97 | + } catch (err) { |
| 98 | + logOTA(`Initialization error: ${err}`); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +/** |
| 103 | + * Start the auto update scheduler |
| 104 | + */ |
| 105 | +function startAutoUpdateScheduler() { |
| 106 | + if (!app.isPackaged) { |
| 107 | + logOTA('Auto update skipped (not packaged)'); |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + if (updateCheckInterval) { |
| 112 | + logOTA('Auto update scheduler already running'); |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | + logOTA('Auto update scheduler started'); |
| 117 | + // Check immediately |
| 118 | + autoUpdater.checkForUpdates().catch(() => undefined); |
| 119 | + // Then check every 5 minutes |
| 120 | + updateCheckInterval = setInterval(() => { |
| 121 | + autoUpdater.checkForUpdates().catch(() => undefined); |
| 122 | + }, 300000); |
| 123 | +} |
| 124 | + |
| 125 | +/** |
| 126 | + * Stop the auto update scheduler |
| 127 | + */ |
| 128 | +function stopAutoUpdateScheduler() { |
| 129 | + if (updateCheckInterval) { |
| 130 | + clearInterval(updateCheckInterval); |
| 131 | + updateCheckInterval = null; |
| 132 | + logOTA('Auto update scheduler stopped'); |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +export { initAutoUpdater, stopAutoUpdateScheduler }; |
0 commit comments