electron应用更新
# 搭建一个服务放置安装包
也就是通过 http://服务地址/latest.ym
能够访问到就行
文件的版本号是根据 package.json
中 version
自动识别的
# win需要的文件
latest.yml
Demo Setup 1.0.1.exe
Demo Setup 1.0.1.exe.blockmap
# 检测更新
yarn add electron-updater
1
# 第一步:在build
中配置publish
字段
"build": {
...
"publish": [
{
"provider": "generic",
"url": "http://服务地址/"
}
]
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# 第二步:在应用程序主进程中调用electron-updater
模块检测更新
import {dialog} from 'electron'
const { autoUpdater } = require('electron-updater')
function checkUpdate(){
if(process.platform == 'darwin'){
// 安装包放在了服务端`darwin`目录下
autoUpdater.setFeedURL('http://服务地址/darwin') //设置要检测更新的路径
}else{
// 同理
autoUpdater.setFeedURL('http://服务地址/win32')
}
//检测更新
autoUpdater.checkForUpdates()
//监听'error'事件
autoUpdater.on('error', (err) => {
console.log(err)
})
//监听'update-available'事件,发现有新版本时触发
autoUpdater.on('update-available', () => {
console.log('found new version')
})
//默认会自动下载新版本,如果不想自动下载,设置autoUpdater.autoDownload = false
//监听'update-downloaded'事件,新版本下载完成时触发
autoUpdater.on('update-downloaded', () => {
dialog.showMessageBox({
type: 'info',
title: '应用更新',
message: '发现新版本,是否更新?',
buttons: ['是', '否']
}).then((buttonIndex) => {
if(buttonIndex.response == 0) { //选择是,则退出程序,安装新版本
autoUpdater.quitAndInstall()
app.quit()
}
})
})
}
app.on('ready', () => {
//每次启动程序,就检查更新
checkUpdate()
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 原理
electron-updater
会根据上面setFeedURL
指定路径下的latest.yml
中的version
来判断是否需要更新,大于当前版本的version
则需要更新,否则不更新。
上次更新: 2022/06/15, 10:06:00