| 4 | import { join } from 'path' |
| 5 | |
| 6 | export function setupAutoUpdater(): void { |
| 7 | // 设置自动下载为false,手动控制下载过程 |
| 8 | autoUpdater.autoDownload = false |
| 9 | autoUpdater.autoInstallOnAppQuit = true |
| 10 | |
| 11 | // 在开发环境中启用自动更新,使用开发配置 |
| 12 | if (is.dev) { |
| 13 | autoUpdater.updateConfigPath = join(__dirname, '../../dev-app-update.yml') |
| 14 | // 强制启用开发环境的更新检查 |
| 15 | autoUpdater.forceDevUpdateConfig = true |
| 16 | // 开发环境下跳过签名验证 |
| 17 | autoUpdater.allowDowngrade = true |
| 18 | } |
| 19 | |
| 20 | // 更新可用时的处理 |
| 21 | autoUpdater.on('update-available', (info) => { |
| 22 | console.log('Update available:', info) |
| 23 | // 通知渲染进程有更新可用 |
| 24 | BrowserWindow.getAllWindows().forEach((window) => { |
| 25 | window.webContents.send('update-available', info) |
| 26 | }) |
| 27 | }) |
| 28 | |
| 29 | // 没有更新时的处理 |
| 30 | autoUpdater.on('update-not-available', (info) => { |
| 31 | console.log('Update not available:', info) |
| 32 | BrowserWindow.getAllWindows().forEach((window) => { |
| 33 | window.webContents.send('update-not-available', info) |
| 34 | }) |
| 35 | }) |
| 36 | |
| 37 | // 更新下载进度 |
| 38 | autoUpdater.on('download-progress', (progressObj) => { |
| 39 | console.log('Download progress:', progressObj) |
| 40 | BrowserWindow.getAllWindows().forEach((window) => { |
| 41 | window.webContents.send('download-progress', progressObj) |
| 42 | }) |
| 43 | }) |
| 44 | |
| 45 | // 更新下载完成 |
| 46 | autoUpdater.on('update-downloaded', (info) => { |
| 47 | console.log('Update downloaded:', info) |
| 48 | BrowserWindow.getAllWindows().forEach((window) => { |
| 49 | window.webContents.send('update-downloaded', info) |
| 50 | }) |
| 51 | }) |
| 52 | |
| 53 | // 更新错误处理 |
| 54 | autoUpdater.on('error', (error) => { |
| 55 | console.error('Update error:', error) |
| 56 | BrowserWindow.getAllWindows().forEach((window) => { |
| 57 | window.webContents.send('update-error', error.message) |
| 58 | }) |
| 59 | }) |
| 60 | } |
| 61 | |
| 62 | export { autoUpdater } |