electron-updater使用指南
基础
检测是否最新版
autoUpdater.checkForUpdates()
下载最新版
autoUpdater.downloadUpdate()
项目使用
update.js
const { ipcMain } = require('electron')
const { autoUpdater } = require('electron-updater')
const path = require("path")
// 更新地址,该地址下放的是安装包和latest.yml
const updateURL = 'https://jkcgy.obs.cn-south-1.myhuaweicloud.com/desktop/'
const message = {
error: '软件更新异常,请重试',
checking: '正在检查更新',
updateAva: '检测到新版本,准备下载',
updateDown: '软件下载中,请耐心等待',
updateSet: '下载完成,准备安装',
updateNotAva: '已经是最新版本',
}
//软件版本更新
ipcMain.handle('on-soft-update', (e) => {
autoUpdater.checkForUpdates()
})
ipcMain.on("updateDesktop", () => {
console.log("checkForUpdates");
autoUpdater.checkForUpdates()
// console.log("downloadUpdate");
})
ipcMain.on("updateDesktopping", () => {
autoUpdater.downloadUpdate()
})
// 检测更新,在你想要检查更新的时候执行,renderer事件触发后的操作自行编写
function handleUpdate(mainWindow, callback) {
// 设置是否自动下载,默认是true,当点击检测到新版本时,会自动下载安装包,所以设置为false
autoUpdater.autoDownload = false
// 如果安装包下载好了,当应用退出后是否自动安装更新
autoUpdater.autoInstallOnAppQuit = false
if (process.env.NODE_ENV == "development") {
autoUpdater.updateConfigPath = path.join(__dirname, "../../aaa/app-update.yml");
}
// 设置版本更新服务器地址
autoUpdater.setFeedURL(updateURL)
// 更新发生错误时触发
autoUpdater.on('error', function () {
console.log(" 更新发生错误时触发",);
sendUpdateMessage(message.error, "error")
})
// 开始检查更新事件
autoUpdater.on('checking-for-update', function () {
console.log(" 开始检查更新事件",);
sendUpdateMessage(message.checking, "checking")
})
// 没有可更新版本
autoUpdater.on('update-not-available', function (info) {
console.log(" 开始检查更新事件",);
sendUpdateMessage(message.updateNotAva, "updateNotAva")
})
// 发现可更新版本
autoUpdater.on('update-available', function (info) {
console.log(" 发现可更新版本",);
// autoUpdater.downloadUpdate()
sendUpdateMessage(message.updateAva, "updateAva")
})
// 更新下载进度事件
autoUpdater.on('download-progress', function (progressObj) {
console.log(" 更新下载进度事件",);
sendUpdateMessage(message.updateDown, "updateDown")
mainWindow.webContents.send('on-soft-download', progressObj.percent)
})
// 下载监听
autoUpdater.on(
'update-downloaded',
function (event, releaseNotes, releaseName, releaseDate, updateUrl, quitAndUpdate) {
mainWi