|
| 1 | +import { BrowserWindow, ipcMain } from 'electron' |
| 2 | +import electronUpdater, { type AppUpdater, type UpdateInfo } from 'electron-updater' |
| 3 | + |
| 4 | +export type UpdateStatus = |
| 5 | + | 'idle' |
| 6 | + | 'checking' |
| 7 | + | 'available' |
| 8 | + | 'not-available' |
| 9 | + | 'downloading' |
| 10 | + | 'ready' |
| 11 | + | 'error' |
| 12 | + |
| 13 | +export interface UpdateStatusPayload { |
| 14 | + status: UpdateStatus |
| 15 | + updateInfo?: UpdateInfo |
| 16 | + progress?: { |
| 17 | + percent: number |
| 18 | + bytesPerSecond: number |
| 19 | + transferred: number |
| 20 | + total: number |
| 21 | + } |
| 22 | + error?: string |
| 23 | +} |
| 24 | + |
| 25 | +let autoUpdater: AppUpdater | null = null |
| 26 | + |
| 27 | +function getMainWindow(): BrowserWindow | null { |
| 28 | + const windows = BrowserWindow.getAllWindows() |
| 29 | + return windows.length > 0 ? windows[0] : null |
| 30 | +} |
| 31 | + |
| 32 | +function sendUpdateStatus(payload: UpdateStatusPayload): void { |
| 33 | + const mainWindow = getMainWindow() |
| 34 | + if (mainWindow && !mainWindow.isDestroyed()) { |
| 35 | + mainWindow.webContents.send('update-status', payload) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +export function setupAutoUpdater(): AppUpdater { |
| 40 | + const { autoUpdater: updater } = electronUpdater |
| 41 | + const log = require('electron-log') |
| 42 | + log.transports.file.level = 'debug' |
| 43 | + updater.logger = log |
| 44 | + |
| 45 | + updater.autoDownload = false |
| 46 | + updater.autoInstallOnAppQuit = true |
| 47 | + |
| 48 | + autoUpdater = updater |
| 49 | + |
| 50 | + updater.on('checking-for-update', () => { |
| 51 | + sendUpdateStatus({ status: 'checking' }) |
| 52 | + }) |
| 53 | + |
| 54 | + updater.on('update-available', (info: UpdateInfo) => { |
| 55 | + sendUpdateStatus({ status: 'available', updateInfo: info }) |
| 56 | + }) |
| 57 | + |
| 58 | + updater.on('update-not-available', (info: UpdateInfo) => { |
| 59 | + sendUpdateStatus({ status: 'not-available', updateInfo: info }) |
| 60 | + }) |
| 61 | + |
| 62 | + updater.on('download-progress', (progress) => { |
| 63 | + sendUpdateStatus({ |
| 64 | + status: 'downloading', |
| 65 | + progress: { |
| 66 | + percent: progress.percent, |
| 67 | + bytesPerSecond: progress.bytesPerSecond, |
| 68 | + transferred: progress.transferred, |
| 69 | + total: progress.total |
| 70 | + } |
| 71 | + }) |
| 72 | + }) |
| 73 | + |
| 74 | + updater.on('update-downloaded', (info: UpdateInfo) => { |
| 75 | + sendUpdateStatus({ status: 'ready', updateInfo: info }) |
| 76 | + }) |
| 77 | + |
| 78 | + updater.on('error', (error: Error) => { |
| 79 | + sendUpdateStatus({ status: 'error', error: error.message }) |
| 80 | + }) |
| 81 | + |
| 82 | + return updater |
| 83 | +} |
| 84 | + |
| 85 | +export function registerUpdaterIpcHandlers(): void { |
| 86 | + ipcMain.handle('checkForUpdates', async () => { |
| 87 | + if (!autoUpdater) { |
| 88 | + throw new Error('Auto updater not initialized') |
| 89 | + } |
| 90 | + return await autoUpdater.checkForUpdates() |
| 91 | + }) |
| 92 | + |
| 93 | + ipcMain.handle('downloadUpdate', async () => { |
| 94 | + if (!autoUpdater) { |
| 95 | + throw new Error('Auto updater not initialized') |
| 96 | + } |
| 97 | + await autoUpdater.downloadUpdate() |
| 98 | + }) |
| 99 | + |
| 100 | + ipcMain.handle('quitAndInstall', () => { |
| 101 | + if (!autoUpdater) { |
| 102 | + throw new Error('Auto updater not initialized') |
| 103 | + } |
| 104 | + autoUpdater.quitAndInstall() |
| 105 | + }) |
| 106 | +} |
0 commit comments