update
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
// electron-updater is CommonJS with a lazy getter for `autoUpdater`, so a named
|
||||
// ESM import fails at runtime under Node ESM (cjs-module-lexer can't detect it).
|
||||
// Use the default interop import and access the instance lazily inside initUpdater
|
||||
// (after app.whenReady), matching the original intended evaluation timing.
|
||||
import electronUpdater from 'electron-updater'
|
||||
import { BrowserWindow, ipcMain } from 'electron'
|
||||
|
||||
let mainWindow: BrowserWindow | null = null
|
||||
|
||||
export function initUpdater(win: BrowserWindow): void {
|
||||
mainWindow = win
|
||||
const autoUpdater = electronUpdater.autoUpdater
|
||||
|
||||
// 检查更新
|
||||
ipcMain.handle('updater:check', async () => {
|
||||
try {
|
||||
const result = await autoUpdater.checkForUpdates()
|
||||
return result?.updateInfo ? { available: true, version: result.updateInfo.version } : { available: false }
|
||||
} catch {
|
||||
return { available: false }
|
||||
}
|
||||
})
|
||||
|
||||
// 下载更新
|
||||
ipcMain.handle('updater:download', async () => {
|
||||
try {
|
||||
await autoUpdater.downloadUpdate()
|
||||
return { success: true }
|
||||
} catch (err: any) {
|
||||
return { success: false, error: err.message }
|
||||
}
|
||||
})
|
||||
|
||||
// 安装更新
|
||||
ipcMain.handle('updater:install', () => {
|
||||
autoUpdater.quitAndInstall()
|
||||
})
|
||||
|
||||
autoUpdater.autoDownload = false
|
||||
|
||||
autoUpdater.on('checking-for-update', () => {
|
||||
mainWindow?.webContents.send('updater:status', { status: 'checking' })
|
||||
})
|
||||
|
||||
autoUpdater.on('update-available', (info) => {
|
||||
mainWindow?.webContents.send('updater:status', {
|
||||
status: 'available',
|
||||
version: info.version,
|
||||
releaseNotes: info.releaseNotes,
|
||||
})
|
||||
})
|
||||
|
||||
autoUpdater.on('update-not-available', () => {
|
||||
mainWindow?.webContents.send('updater:status', { status: 'not-available' })
|
||||
})
|
||||
|
||||
autoUpdater.on('download-progress', (progress) => {
|
||||
mainWindow?.webContents.send('updater:status', {
|
||||
status: 'downloading',
|
||||
percent: progress.percent,
|
||||
transferred: progress.transferred,
|
||||
total: progress.total,
|
||||
bytesPerSecond: progress.bytesPerSecond,
|
||||
})
|
||||
})
|
||||
|
||||
autoUpdater.on('update-downloaded', () => {
|
||||
mainWindow?.webContents.send('updater:status', { status: 'downloaded' })
|
||||
})
|
||||
|
||||
autoUpdater.on('error', (err) => {
|
||||
mainWindow?.webContents.send('updater:status', { status: 'error', message: err.message })
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user