- npm 安装 electron-updater@4.0.0 版本 注意 4.0.0版
- 先打包一次,把打包下的win-unpacked/resources/app-update.yml 的内容复制一份,在main下边新建default-app-update.yml 然后粘贴内容。
- 将main/index.js 的新建的BrowserWindow 对象 导出,修改代码中 mainWindow 为你导出的名字。
- 在packjson 中,配置更新地址:(把地址替换成你自己的服务器地址,该地址下放的是 打包后的安装包 和 latest.yml 文件)
"mac": {
"icon": "build/icons/icon.icns",
"publish": [
{
"provider": "generic",
"url": "https://www.*******/mac/"
}
]
},
"win": {
"icon": "build/icons/256x256.png",
"publish": [
{
"provider": "generic",
"url": "https://www.*******/win/"
}
]
},
- 在main下边新建 update.js 文件,下边是代码:
import { autoUpdater } from "electron-updater"
import { ipcMain } from "electron"
import { mainWindow } from "./index.js"
// 更新
function handleUpadate(){
// 定义更新版本的几种状态
const returnData = {
error: { status: -1, msg: '检测更新查询异常' },
checking: { status: 0, msg: '正在检查应用程序更新' },
updateAva: { status: 1, msg: '检测到新版本,正在下载,请稍后' },
updateNotAva: { status: -1, msg: '您现在使用的版本为最新版本,无需更新!' }
}
// 如果是开发版调试需要准备工作:
// 1. 将打包后的 win-unpacked下的 app-udate.yml的内容复制,在main下新建default-app-update.yml文件,粘贴内容
// 2. 指定版本为 0.0.1 版,因为在开发环境下,electron-update会读取 electron的版本
if(process.env.NODE_ENV === 'development'){
autoUpdater.updateConfigPath = require('path').join(__dirname, 'default-app-update.yml')
autoUpdater.currentVersion = "1.0.0"
}else{
autoUpdater.updateConfigPath = require('path').join(__dirname, '../../../app-update.yml')
}
// 设置更新地址
let feedurl = 'https://www.*******/win/'
if(process.platform == 'darwin'){
feedurl = 'https://www.*******/mac/'
}
autoUpdater.setFeedURL(feedurl)
// 更新错误
autoUpdater.on('error',function(error){
sendUpdateMessage(returnData.error)
})
// 检查中
autoUpdater.on('checking-for-update',function(){
sendUpdateMessage(returnData.checking)
})
// 发现新版本
autoUpdater.on('update-available',function(info){
sendUpdateMessage(returnData.updateAva)
})
// 当前版本为最新版
autoUpdater.on('update-not-available',function(info){
sendUpdateMessage(returnData.updateNotAva)
})
// 正在更新下载,返回进度
autoUpdater.on('download-progress', function(progressObj){
mainWindow.webContents.send('downloadProgress', progressObj)
})
// 下载完成,给主进程绑定isUpdateNow 事件,用户选择安装可以(调用isUpdateNow方法)
autoUpdater.on('update-downloaded', function (event, releaseNotes, releaseName, releaseDate, updateUrl, quitAndUpdate) {
ipcMain.on('isUpdateNow', (e, arg) => {
//autoUpdater.quitAndInstall()方法,可实现立即关闭程序并安装
autoUpdater.quitAndInstall();
});
})
// 执行更新检查,将执行上边你的各项更新配置
autoUpdater.checkForUpdates()
// 应用在关闭后自动安装(默认是false)
autoUpdater.autoInstallOnAppQuit = true
}
// 发送给renderer进行,更新状态
function sendUpdateMessage(text){
mainWindow.webContents.send('message',text)
}
export { handleUpadate }
- 在index.js 中引入,并绑定事件。
import { ipcMain } from 'electron'
import { handleUpadate } from './update.js'
ipcMain.on('updateClient',function(){
handleUpadate()
})
- 在你需要更新的页面中,执行主进程的 updateClient事件。
// 获取客户端版本信息
getClientAva(){
// 使用主进程获取更新
this.$electron.ipcRenderer.send('updateClient')
// 获取主进程返回的检测或更新状态
this.$electron.ipcRenderer.on('message',(event,data)=>{
switch(data.status){
case -1:
console.log(data.msg)
break
case 0:
console.log(data.msg)
break
case 1:
console.log(data.msg)
this.isShowUpdate = true
break
}
})
// 更新时,获取更新进度
this.$electron.ipcRenderer.on('downloadProgress',(event,data)=>{
this.percent = (data.percent).toFixed(2)+''
if(data.percent >= 100){
this.isShowUpdate = false
}
})
}
- 最终效果:
触发 getClientAva方法,执行更新检查。
在有新版本时执行全量下载
应用关闭后,自动安装最新版(如需用户手动安装,自己调用isUpdateNow 事件 就行)
以上均在 window 上实践完成,mac版更新应用可能需要签名证书的问题(待实践),增量更新(待续…)