|
1 | 1 | import 'dotenv/config' |
2 | | -import { app, BrowserWindow, globalShortcut } from 'electron' |
| 2 | +import { app, BrowserWindow, globalShortcut, dialog } from 'electron' |
| 3 | +import { autoUpdater } from 'electron-updater' |
3 | 4 |
|
4 | 5 | // Swallow AbortError from user-initiated stream cancellations to keep console clean |
5 | 6 | function isAbortError(error: unknown): boolean { |
@@ -39,6 +40,53 @@ app.whenReady().then(() => { |
39 | 40 |
|
40 | 41 | createWindow() |
41 | 42 |
|
| 43 | + // Configure auto-updater: prompt to download when update is available |
| 44 | + try { |
| 45 | + autoUpdater.autoDownload = false |
| 46 | + autoUpdater.on('update-available', async () => { |
| 47 | + const result = await dialog.showMessageBox({ |
| 48 | + type: 'info', |
| 49 | + buttons: ['立即下载', '稍后'], |
| 50 | + defaultId: 0, |
| 51 | + cancelId: 1, |
| 52 | + title: '发现新版本', |
| 53 | + message: '检测到新版本可用。', |
| 54 | + detail: '现在下载并安装更新吗?' |
| 55 | + }) |
| 56 | + if (result.response === 0) { |
| 57 | + autoUpdater.downloadUpdate().catch((err) => console.error(err)) |
| 58 | + } |
| 59 | + }) |
| 60 | + |
| 61 | + autoUpdater.on('error', (error) => { |
| 62 | + console.error('Auto update error:', error) |
| 63 | + }) |
| 64 | + |
| 65 | + autoUpdater.on('update-not-available', () => { |
| 66 | + // no-op |
| 67 | + }) |
| 68 | + |
| 69 | + autoUpdater.on('update-downloaded', async () => { |
| 70 | + const res = await dialog.showMessageBox({ |
| 71 | + type: 'info', |
| 72 | + buttons: ['立即重启', '稍后'], |
| 73 | + defaultId: 0, |
| 74 | + cancelId: 1, |
| 75 | + title: '更新已就绪', |
| 76 | + message: '更新已下载完成。', |
| 77 | + detail: '是否立即重启以应用更新?' |
| 78 | + }) |
| 79 | + if (res.response === 0) { |
| 80 | + setImmediate(() => autoUpdater.quitAndInstall()) |
| 81 | + } |
| 82 | + }) |
| 83 | + |
| 84 | + // Trigger the check after window creation |
| 85 | + autoUpdater.checkForUpdates().catch((err) => console.error(err)) |
| 86 | + } catch (e) { |
| 87 | + console.error('Failed to initialize auto-updater:', e) |
| 88 | + } |
| 89 | + |
42 | 90 | app.on('activate', function () { |
43 | 91 | // On macOS it's common to re-create a window in the app when the |
44 | 92 | // dock icon is clicked and there are no other windows open. |
|
0 commit comments