| 1 | /* |
| 2 | * @Author: nevin |
| 3 | * @Date: 2025-01-17 19:25:29 |
| 4 | * @LastEditTime: 2025-01-20 11:15:53 |
| 5 | * @LastEditors: nevin |
| 6 | * @Description: 框架更新 |
| 7 | */ |
| 8 | import { app, ipcMain } from 'electron'; |
| 9 | import { createRequire } from 'node:module'; |
| 10 | import type { |
| 11 | ProgressInfo, |
| 12 | UpdateDownloadedEvent, |
| 13 | UpdateInfo, |
| 14 | } from 'electron-updater'; |
| 15 | |
| 16 | const { autoUpdater } = createRequire(import.meta.url)('electron-updater'); |
| 17 | |
| 18 | export function update(win: Electron.BrowserWindow) { |
| 19 | // When set to false, the update download will be triggered through the API |
| 20 | autoUpdater.autoDownload = false; |
| 21 | autoUpdater.disableWebInstaller = false; |
| 22 | autoUpdater.allowDowngrade = false; |
| 23 | |
| 24 | // start check |
| 25 | autoUpdater.on('checking-for-update', function () {}); |
| 26 | // update available |
| 27 | autoUpdater.on('update-available', (arg: UpdateInfo) => { |
| 28 | win.webContents.send('update-can-available', { |
| 29 | update: true, |
| 30 | version: app.getVersion(), |
| 31 | newVersion: arg?.version, |
| 32 | }); |
| 33 | }); |
| 34 | // update not available |
| 35 | autoUpdater.on('update-not-available', (arg: UpdateInfo) => { |
| 36 | win.webContents.send('update-can-available', { |
| 37 | update: false, |
| 38 | version: app.getVersion(), |
| 39 | newVersion: arg?.version, |
| 40 | }); |
| 41 | }); |
| 42 | |
| 43 | // Checking for updates |
| 44 | ipcMain.handle('check-update', async () => { |
| 45 | if (!app.isPackaged) { |
| 46 | const error = new Error( |
| 47 | 'The update feature is only available after the package.', |
| 48 | ); |
| 49 | return { message: error.message, error }; |
| 50 | } |
| 51 | |
| 52 | try { |
| 53 | return await autoUpdater.checkForUpdatesAndNotify(); |
| 54 | } catch (error) { |
| 55 | return { message: 'Network error', error }; |
| 56 | } |
| 57 | }); |
| 58 | |
| 59 | // Start downloading and feedback on progress |
| 60 | ipcMain.handle('start-download', (event: Electron.IpcMainInvokeEvent) => { |
| 61 | startDownload( |
| 62 | (error, progressInfo) => { |
| 63 | if (error) { |
| 64 | // feedback download error message |
| 65 | event.sender.send('update-error', { message: error.message, error }); |
| 66 | } else { |
| 67 | // feedback update progress message |
| 68 | event.sender.send('download-progress', progressInfo); |
| 69 | } |
| 70 | }, |
| 71 | () => { |
| 72 | // feedback update downloaded message |
| 73 | event.sender.send('update-downloaded'); |
| 74 | }, |
| 75 | ); |
| 76 | }); |
| 77 | |
| 78 | // Install now |
| 79 | ipcMain.handle('quit-and-install', () => { |
| 80 | autoUpdater.quitAndInstall(false, true); |
| 81 | }); |
| 82 | } |
| 83 | |
| 84 | function startDownload( |
| 85 | callback: (error: Error | null, info: ProgressInfo | null) => void, |
| 86 | complete: (event: UpdateDownloadedEvent) => void, |
| 87 | ) { |
| 88 | autoUpdater.on('download-progress', (info: ProgressInfo) => |
| 89 | callback(null, info), |
| 90 | ); |
| 91 | autoUpdater.on('error', (error: Error) => callback(error, null)); |
| 92 | autoUpdater.on('update-downloaded', complete); |
| 93 | autoUpdater.downloadUpdate(); |
| 94 | } |
| 95 |