|
1 | | -import { dialog } from 'electron' |
| 1 | +import { app, ipcMain } from 'electron' |
2 | 2 | import { autoUpdater } from 'electron-updater' |
| 3 | +import { settings, registerSettingsChangeHook } from './settings' |
3 | 4 |
|
4 | | -export function initAutoUpdater(): void { |
5 | | - if (process.platform === 'darwin') { |
6 | | - return |
| 5 | +/** |
| 6 | + * Auto-update from GitHub Releases: checks after startup (and every 4 h), |
| 7 | + * downloads in the background, and installs silently over the existing |
| 8 | + * install path on quit (NSIS keeps the registry install location). macOS is |
| 9 | + * excluded — unsigned builds cannot auto-update, users download the dmg. |
| 10 | + */ |
| 11 | + |
| 12 | +const STARTUP_DELAY_MS = 15 * 1000 |
| 13 | +const CHECK_INTERVAL_MS = 4 * 60 * 60 * 1000 |
| 14 | + |
| 15 | +export interface UpdateStatus { |
| 16 | + status: |
| 17 | + | 'idle' |
| 18 | + | 'checking' |
| 19 | + | 'downloading' |
| 20 | + | 'downloaded' |
| 21 | + | 'not-available' |
| 22 | + | 'error' |
| 23 | + | 'unsupported' |
| 24 | + currentVersion: string |
| 25 | + version?: string |
| 26 | + progress?: number |
| 27 | + message?: string |
| 28 | +} |
| 29 | + |
| 30 | +let status: UpdateStatus = { status: 'idle', currentVersion: app.getVersion() } |
| 31 | +let startupTimer: NodeJS.Timeout | null = null |
| 32 | +let intervalTimer: NodeJS.Timeout | null = null |
| 33 | +let lastAvailableVersion = '' |
| 34 | +let initialized = false |
| 35 | + |
| 36 | +function sendToRenderer(channel: string, ...args: unknown[]) { |
| 37 | + const mainWindow = global.mainWindow |
| 38 | + if (mainWindow && !mainWindow.isDestroyed()) { |
| 39 | + mainWindow.webContents.send(channel, ...args) |
7 | 40 | } |
| 41 | +} |
8 | 42 |
|
9 | | - try { |
10 | | - autoUpdater.autoDownload = false |
11 | | - |
12 | | - autoUpdater.on('update-available', async () => { |
13 | | - const result = await dialog.showMessageBox({ |
14 | | - type: 'info', |
15 | | - buttons: ['立即下载', '稍后'], |
16 | | - defaultId: 0, |
17 | | - cancelId: 1, |
18 | | - title: '发现新版本', |
19 | | - message: '检测到新版本可用。', |
20 | | - detail: '现在下载并安装更新吗?' |
21 | | - }) |
22 | | - if (result.response === 0) { |
23 | | - autoUpdater.downloadUpdate().catch((err) => console.error(err)) |
24 | | - } |
25 | | - }) |
| 43 | +function setStatus(next: Partial<UpdateStatus> & { status: UpdateStatus['status'] }): void { |
| 44 | + status = { ...status, ...next } |
| 45 | + sendToRenderer('update-status', status) |
| 46 | +} |
26 | 47 |
|
27 | | - autoUpdater.on('error', (error) => { |
28 | | - console.error('Auto update error:', error) |
| 48 | +function checkNow(): UpdateStatus { |
| 49 | + if (!initialized) { |
| 50 | + setStatus({ |
| 51 | + status: 'unsupported', |
| 52 | + message: 'macOS 暂不支持自动更新,请到 GitHub Releases 手动下载 dmg' |
29 | 53 | }) |
| 54 | + return status |
| 55 | + } |
| 56 | + if (status.status === 'checking' || status.status === 'downloading') { |
| 57 | + return status // a check/download is already in flight |
| 58 | + } |
| 59 | + autoUpdater.checkForUpdates().catch((err) => { |
| 60 | + console.error('Auto update check failed:', err) |
| 61 | + setStatus({ status: 'error', message: err instanceof Error ? err.message : String(err) }) |
| 62 | + }) |
| 63 | + setStatus({ status: 'checking', message: undefined }) |
| 64 | + return status |
| 65 | +} |
30 | 66 |
|
31 | | - autoUpdater.on('update-not-available', () => { |
32 | | - // no-op |
33 | | - }) |
| 67 | +function syncSchedule(): void { |
| 68 | + if (startupTimer) { |
| 69 | + clearTimeout(startupTimer) |
| 70 | + startupTimer = null |
| 71 | + } |
| 72 | + if (intervalTimer) { |
| 73 | + clearInterval(intervalTimer) |
| 74 | + intervalTimer = null |
| 75 | + } |
| 76 | + if (!initialized || !settings.autoUpdateEnabled) return |
| 77 | + startupTimer = setTimeout(() => { |
| 78 | + startupTimer = null |
| 79 | + checkNow() |
| 80 | + }, STARTUP_DELAY_MS) |
| 81 | + intervalTimer = setInterval(() => checkNow(), CHECK_INTERVAL_MS) |
| 82 | +} |
34 | 83 |
|
35 | | - autoUpdater.on('update-downloaded', async () => { |
36 | | - const res = await dialog.showMessageBox({ |
37 | | - type: 'info', |
38 | | - buttons: ['立即重启', '稍后'], |
39 | | - defaultId: 0, |
40 | | - cancelId: 1, |
41 | | - title: '更新已就绪', |
42 | | - message: '更新已下载完成。', |
43 | | - detail: '是否立即重启以应用更新?' |
44 | | - }) |
45 | | - if (res.response === 0) { |
46 | | - setImmediate(() => autoUpdater.quitAndInstall(false, true)) |
47 | | - } |
48 | | - }) |
| 84 | +export function getUpdateStatus(): UpdateStatus { |
| 85 | + return status |
| 86 | +} |
| 87 | + |
| 88 | +export function initAutoUpdater(): void { |
| 89 | + // IPC is registered on every platform so the renderer UI stays consistent |
| 90 | + ipcMain.handle('get-update-status', () => getUpdateStatus()) |
| 91 | + ipcMain.handle('check-update', () => checkNow()) |
| 92 | + ipcMain.handle('install-update', () => { |
| 93 | + if (initialized && status.status === 'downloaded') { |
| 94 | + // Silent install over the current install path, relaunch after |
| 95 | + autoUpdater.quitAndInstall(true, true) |
| 96 | + } |
| 97 | + return getUpdateStatus() |
| 98 | + }) |
49 | 99 |
|
50 | | - // Trigger the check after window creation |
51 | | - autoUpdater.checkForUpdates().catch((err) => console.error(err)) |
52 | | - } catch (e) { |
53 | | - console.error('Failed to initialize auto-updater:', e) |
| 100 | + registerSettingsChangeHook((changed) => { |
| 101 | + if ('autoUpdateEnabled' in changed) { |
| 102 | + syncSchedule() |
| 103 | + } |
| 104 | + }) |
| 105 | + |
| 106 | + if (process.platform === 'darwin') { |
| 107 | + return |
54 | 108 | } |
| 109 | + |
| 110 | + initialized = true |
| 111 | + autoUpdater.autoDownload = true |
| 112 | + autoUpdater.autoInstallOnAppQuit = true |
| 113 | + |
| 114 | + autoUpdater.on('checking-for-update', () => setStatus({ status: 'checking', message: undefined })) |
| 115 | + autoUpdater.on('update-available', (info) => { |
| 116 | + lastAvailableVersion = info.version |
| 117 | + setStatus({ status: 'downloading', version: info.version, progress: 0 }) |
| 118 | + }) |
| 119 | + autoUpdater.on('download-progress', (progress) => { |
| 120 | + setStatus({ |
| 121 | + status: 'downloading', |
| 122 | + version: lastAvailableVersion || undefined, |
| 123 | + progress: Math.round(progress.percent) |
| 124 | + }) |
| 125 | + }) |
| 126 | + autoUpdater.on('update-downloaded', (info) => { |
| 127 | + setStatus({ status: 'downloaded', version: info.version, progress: 100 }) |
| 128 | + }) |
| 129 | + autoUpdater.on('update-not-available', () => |
| 130 | + setStatus({ status: 'not-available', message: undefined }) |
| 131 | + ) |
| 132 | + autoUpdater.on('error', (err) => { |
| 133 | + console.error('Auto update error:', err) |
| 134 | + setStatus({ status: 'error', message: err.message }) |
| 135 | + }) |
| 136 | + |
| 137 | + syncSchedule() |
55 | 138 | } |
0 commit comments