Files
test-project-161/electron/main/update.ts
T
Song e803167b31 refactor: upgrade deps, setup tailwind4 and correct ci (#258)
* refresh

* e2e test

* Change debugger type from 'pwa-chrome' to 'chrome'

* ci: align workflows with electron-vite-vue#540

* chore: update test, tsconfig and deps

* chore: update deps and debug config

* chore: refresh style

* docs: update readme

* docs: make linter happy

* ci: bump versions

* chore: refactor update handling and improve UI elements

* 1.0.0
2026-05-31 22:42:38 +08:00

108 lines
3.4 KiB
TypeScript

import { app, ipcMain } from 'electron'
import type {
ProgressInfo,
UpdateDownloadedEvent,
UpdateInfo,
} from 'electron-updater'
// Pure cjs module does not support named exports, so we need to import the default export and access the autoUpdater property
import updater from 'electron-updater'
const autoUpdater = updater.autoUpdater
let cancellationToken = new updater.CancellationToken()
let isDownloading = false
export function update(win: Electron.BrowserWindow) {
// When set to false, the update download will be triggered through the API
autoUpdater.autoDownload = false
autoUpdater.disableWebInstaller = false
autoUpdater.allowDowngrade = false
// start check
autoUpdater.on('checking-for-update', function () { })
// update available
autoUpdater.on('update-available', (arg: UpdateInfo) => {
win.webContents.send('update-can-available', { update: true, version: app.getVersion(), newVersion: arg?.version })
})
// update not available
autoUpdater.on('update-not-available', (arg: UpdateInfo) => {
win.webContents.send('update-can-available', { update: false, version: app.getVersion(), newVersion: arg?.version })
})
// Checking for updates
ipcMain.handle('check-update', async () => {
if (!app.isPackaged) {
const error = new Error('The update feature is only available after the package.')
return { message: error.message, error }
}
try {
return await autoUpdater.checkForUpdates()
} catch (error) {
const resolvedError = error instanceof Error ? error : new Error('Network error')
return { message: resolvedError.message, error: resolvedError }
}
})
// Start downloading and feedback on progress
ipcMain.handle('start-download', (event: Electron.IpcMainInvokeEvent) => {
if (isDownloading) return
isDownloading = true
startDownload(
(error, progressInfo) => {
if (error) {
isDownloading = false
// feedback download error message
event.sender.send('update-error', { message: error.message, error })
} else {
// feedback update progress message
event.sender.send('download-progress', progressInfo)
}
},
() => {
isDownloading = false
// feedback update downloaded message
event.sender.send('update-downloaded')
}
)
})
// Cancel downloading
ipcMain.handle('cancel-download', () => {
cancellationToken.cancel()
cancellationToken = new updater.CancellationToken();
})
// Install now
ipcMain.handle('quit-and-install', () => {
autoUpdater.quitAndInstall(false, true)
})
}
function startDownload(
callback: (error: Error | null, info: ProgressInfo | null) => void,
complete: (event: UpdateDownloadedEvent) => void,
) {
const onDownloadProgress = (info: ProgressInfo) => callback(null, info)
const onError = (error: Error) => {
cleanup()
callback(error, null)
}
const onDownloaded = (event: UpdateDownloadedEvent) => {
cleanup()
complete(event)
}
const cleanup = () => {
autoUpdater.off('download-progress', onDownloadProgress)
autoUpdater.off('error', onError)
autoUpdater.off('update-downloaded', onDownloaded)
}
autoUpdater.on('download-progress', onDownloadProgress)
autoUpdater.on('error', onError)
autoUpdater.once('update-downloaded', onDownloaded)
autoUpdater.downloadUpdate(cancellationToken)
}