2023-10-11 12:54:41 +02:00
|
|
|
|
import { app, ipcMain } from 'electron'
|
2023-10-11 10:42:47 +02:00
|
|
|
|
import { is } from '@electron-toolkit/utils'
|
2024-05-08 05:02:00 +02:00
|
|
|
|
import electronUpdater from 'electron-updater'
|
2024-05-15 05:16:02 +02:00
|
|
|
|
import { devPublishPath } from '$electron/configs/index.js'
|
2023-10-11 10:42:47 +02:00
|
|
|
|
|
2024-05-08 05:02:00 +02:00
|
|
|
|
const { autoUpdater } = electronUpdater
|
|
|
|
|
|
2023-10-11 12:54:41 +02:00
|
|
|
|
export default (mainWindow) => {
|
2023-10-27 13:18:09 +02:00
|
|
|
|
if (is.dev) {
|
2023-10-17 09:27:08 +02:00
|
|
|
|
autoUpdater.updateConfigPath = devPublishPath
|
2023-10-16 05:53:08 +02:00
|
|
|
|
Object.defineProperty(app, 'isPackaged', {
|
|
|
|
|
get() {
|
|
|
|
|
return true
|
|
|
|
|
},
|
|
|
|
|
})
|
2023-10-11 12:54:41 +02:00
|
|
|
|
}
|
2023-10-11 10:42:47 +02:00
|
|
|
|
|
2023-10-11 12:54:41 +02:00
|
|
|
|
// 触发检查更新(此方法用于被渲染线程调用,例如页面点击检查更新按钮来调用此方法)
|
|
|
|
|
ipcMain.on('check-for-update', () => {
|
|
|
|
|
autoUpdater.checkForUpdates()
|
|
|
|
|
})
|
2023-10-11 10:42:47 +02:00
|
|
|
|
|
2023-10-11 12:54:41 +02:00
|
|
|
|
// 下载更新
|
|
|
|
|
ipcMain.on('download-update', () => {
|
|
|
|
|
autoUpdater.downloadUpdate()
|
|
|
|
|
})
|
2023-10-11 10:42:47 +02:00
|
|
|
|
|
2023-10-11 12:54:41 +02:00
|
|
|
|
// 安装更新
|
|
|
|
|
ipcMain.on('quit-and-install', () => {
|
2023-11-10 09:44:59 +01:00
|
|
|
|
setImmediate(() => {
|
|
|
|
|
app.isQuiting = true
|
|
|
|
|
autoUpdater.quitAndInstall()
|
|
|
|
|
})
|
2023-10-11 12:54:41 +02:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// 设置自动下载为false(默认为true,检测到有更新就自动下载)
|
|
|
|
|
autoUpdater.autoDownload = false
|
|
|
|
|
// 检测下载错误
|
|
|
|
|
autoUpdater.on('error', (error) => {
|
2023-10-12 03:10:56 +02:00
|
|
|
|
console.error('update-error')
|
|
|
|
|
mainWindow.webContents.send('update-error', error)
|
2023-10-11 12:54:41 +02:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// 检测是否需要更新
|
|
|
|
|
autoUpdater.on('checking-for-update', (ret) => {
|
|
|
|
|
console.log('checking-for-update', ret)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// 检测到可以更新时
|
|
|
|
|
autoUpdater.on('update-available', (ret) => {
|
|
|
|
|
mainWindow.webContents.send('update-available', ret)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// 检测到不需要更新时
|
|
|
|
|
autoUpdater.on('update-not-available', (ret) => {
|
|
|
|
|
mainWindow.webContents.send('update-not-available', ret)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// 更新下载进度
|
|
|
|
|
autoUpdater.on('download-progress', (ret) => {
|
|
|
|
|
mainWindow.webContents.send('download-progress', ret)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// 当需要更新的内容下载完成后
|
|
|
|
|
autoUpdater.on('update-downloaded', (ret) => {
|
|
|
|
|
mainWindow.webContents.send('update-downloaded', ret)
|
|
|
|
|
})
|
|
|
|
|
}
|