|
| 1 | +import { app, ipcMain } from 'electron' |
| 2 | +import { is } from '@electron-toolkit/utils' |
| 3 | +import { autoUpdater } from 'electron-updater' |
| 4 | + |
| 5 | +type AppUpdateState = |
| 6 | + | 'idle' |
| 7 | + | 'checking' |
| 8 | + | 'available' |
| 9 | + | 'not-available' |
| 10 | + | 'downloading' |
| 11 | + | 'downloaded' |
| 12 | + | 'unsupported' |
| 13 | + | 'error' |
| 14 | + |
| 15 | +export interface AppUpdateStatus { |
| 16 | + currentVersion: string |
| 17 | + state: AppUpdateState |
| 18 | + availableVersion?: string |
| 19 | + lastCheckedAt?: string |
| 20 | + error?: string |
| 21 | +} |
| 22 | + |
| 23 | +let handlersRegistered = false |
| 24 | +let listenersRegistered = false |
| 25 | + |
| 26 | +let updateStatus: AppUpdateStatus = { |
| 27 | + currentVersion: app.getVersion(), |
| 28 | + state: is.dev ? 'unsupported' : 'idle' |
| 29 | +} |
| 30 | + |
| 31 | +function getStatus(): AppUpdateStatus { |
| 32 | + return { |
| 33 | + ...updateStatus, |
| 34 | + currentVersion: app.getVersion() |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +function setStatus(next: Partial<AppUpdateStatus>): void { |
| 39 | + updateStatus = { |
| 40 | + ...updateStatus, |
| 41 | + ...next, |
| 42 | + currentVersion: app.getVersion() |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +function parseUpdaterError(error: unknown): string { |
| 47 | + if (error instanceof Error) return error.message |
| 48 | + return String(error) |
| 49 | +} |
| 50 | + |
| 51 | +function updatesSupported(): boolean { |
| 52 | + return app.isPackaged && !is.dev |
| 53 | +} |
| 54 | + |
| 55 | +function ensureUpdaterListeners(): void { |
| 56 | + if (listenersRegistered) return |
| 57 | + listenersRegistered = true |
| 58 | + |
| 59 | + autoUpdater.autoDownload = false |
| 60 | + autoUpdater.autoInstallOnAppQuit = false |
| 61 | + |
| 62 | + autoUpdater.on('checking-for-update', () => { |
| 63 | + setStatus({ |
| 64 | + state: 'checking', |
| 65 | + error: undefined |
| 66 | + }) |
| 67 | + }) |
| 68 | + |
| 69 | + autoUpdater.on('update-available', (info) => { |
| 70 | + setStatus({ |
| 71 | + state: 'available', |
| 72 | + availableVersion: info.version, |
| 73 | + error: undefined |
| 74 | + }) |
| 75 | + }) |
| 76 | + |
| 77 | + autoUpdater.on('update-not-available', () => { |
| 78 | + setStatus({ |
| 79 | + state: 'not-available', |
| 80 | + availableVersion: undefined, |
| 81 | + error: undefined |
| 82 | + }) |
| 83 | + }) |
| 84 | + |
| 85 | + autoUpdater.on('download-progress', () => { |
| 86 | + setStatus({ |
| 87 | + state: 'downloading', |
| 88 | + error: undefined |
| 89 | + }) |
| 90 | + }) |
| 91 | + |
| 92 | + autoUpdater.on('update-downloaded', (info) => { |
| 93 | + setStatus({ |
| 94 | + state: 'downloaded', |
| 95 | + availableVersion: info.version, |
| 96 | + error: undefined |
| 97 | + }) |
| 98 | + }) |
| 99 | + |
| 100 | + autoUpdater.on('error', (error) => { |
| 101 | + setStatus({ |
| 102 | + state: 'error', |
| 103 | + error: parseUpdaterError(error) |
| 104 | + }) |
| 105 | + console.error('Auto updater error:', error) |
| 106 | + }) |
| 107 | +} |
| 108 | + |
| 109 | +async function handleCheckForUpdates(): Promise<AppUpdateStatus> { |
| 110 | + if (!updatesSupported()) { |
| 111 | + setStatus({ |
| 112 | + state: 'unsupported', |
| 113 | + availableVersion: undefined, |
| 114 | + error: 'Update checks are only available in packaged builds.', |
| 115 | + lastCheckedAt: new Date().toISOString() |
| 116 | + }) |
| 117 | + return getStatus() |
| 118 | + } |
| 119 | + |
| 120 | + ensureUpdaterListeners() |
| 121 | + |
| 122 | + try { |
| 123 | + setStatus({ |
| 124 | + state: 'checking', |
| 125 | + availableVersion: undefined, |
| 126 | + error: undefined, |
| 127 | + lastCheckedAt: new Date().toISOString() |
| 128 | + }) |
| 129 | + |
| 130 | + const result = await autoUpdater.checkForUpdates() |
| 131 | + setStatus({ lastCheckedAt: new Date().toISOString() }) |
| 132 | + |
| 133 | + if (!result) { |
| 134 | + setStatus({ |
| 135 | + state: 'unsupported', |
| 136 | + error: 'Updater is not active in this build.' |
| 137 | + }) |
| 138 | + return getStatus() |
| 139 | + } |
| 140 | + |
| 141 | + if (result.isUpdateAvailable) { |
| 142 | + setStatus({ |
| 143 | + state: 'available', |
| 144 | + availableVersion: result.updateInfo.version, |
| 145 | + error: undefined |
| 146 | + }) |
| 147 | + return getStatus() |
| 148 | + } |
| 149 | + |
| 150 | + setStatus({ |
| 151 | + state: 'not-available', |
| 152 | + availableVersion: undefined, |
| 153 | + error: undefined |
| 154 | + }) |
| 155 | + return getStatus() |
| 156 | + } catch (error) { |
| 157 | + setStatus({ |
| 158 | + state: 'error', |
| 159 | + error: parseUpdaterError(error) |
| 160 | + }) |
| 161 | + return getStatus() |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +async function handleInstallUpdate(): Promise<AppUpdateStatus> { |
| 166 | + if (!updatesSupported()) { |
| 167 | + setStatus({ |
| 168 | + state: 'unsupported', |
| 169 | + availableVersion: undefined, |
| 170 | + error: 'Updates are only available in packaged builds.' |
| 171 | + }) |
| 172 | + return getStatus() |
| 173 | + } |
| 174 | + |
| 175 | + ensureUpdaterListeners() |
| 176 | + |
| 177 | + if (updateStatus.state === 'downloaded') { |
| 178 | + autoUpdater.quitAndInstall() |
| 179 | + return getStatus() |
| 180 | + } |
| 181 | + |
| 182 | + if (updateStatus.state !== 'available') { |
| 183 | + setStatus({ |
| 184 | + state: 'error', |
| 185 | + error: 'No update ready to install. Check for updates first.' |
| 186 | + }) |
| 187 | + return getStatus() |
| 188 | + } |
| 189 | + |
| 190 | + try { |
| 191 | + setStatus({ |
| 192 | + state: 'downloading', |
| 193 | + error: undefined |
| 194 | + }) |
| 195 | + |
| 196 | + await autoUpdater.downloadUpdate() |
| 197 | + |
| 198 | + setStatus({ |
| 199 | + state: 'downloaded', |
| 200 | + error: undefined |
| 201 | + }) |
| 202 | + |
| 203 | + autoUpdater.quitAndInstall() |
| 204 | + return getStatus() |
| 205 | + } catch (error) { |
| 206 | + setStatus({ |
| 207 | + state: 'error', |
| 208 | + error: parseUpdaterError(error) |
| 209 | + }) |
| 210 | + return getStatus() |
| 211 | + } |
| 212 | +} |
| 213 | + |
| 214 | +export function registerUpdaterIpcHandlers(): void { |
| 215 | + if (handlersRegistered) return |
| 216 | + handlersRegistered = true |
| 217 | + |
| 218 | + ensureUpdaterListeners() |
| 219 | + |
| 220 | + ipcMain.handle('app:update:status', async () => getStatus()) |
| 221 | + ipcMain.handle('app:update:check', async () => handleCheckForUpdates()) |
| 222 | + ipcMain.handle('app:update:install', async () => handleInstallUpdate()) |
| 223 | +} |
| 224 | + |
| 225 | +export function checkForUpdatesInBackground(): void { |
| 226 | + if (!updatesSupported()) return |
| 227 | + |
| 228 | + ensureUpdaterListeners() |
| 229 | + |
| 230 | + autoUpdater.checkForUpdates().catch((error) => { |
| 231 | + setStatus({ |
| 232 | + state: 'error', |
| 233 | + error: parseUpdaterError(error) |
| 234 | + }) |
| 235 | + console.error('Auto updater check failed:', error) |
| 236 | + }) |
| 237 | +} |
0 commit comments