electron-updater
# 开发者模式下测试 electron-updater 自动升级
const { app } = require('electron');
//手动设置app isPackaged
Object.defineProperty(app, 'isPackaged', {
get() {
return true;
}
});
1
2
3
4
5
6
7
2
3
4
5
6
7
自动升级脚本
import { autoUpdater } from 'electron-updater';
import { ipcMain } from 'electron';
//全量更新
let mainWindow = null;
export function updateHandle(window, feedUrl) {
mainWindow = window;
//设置更新包的地址
autoUpdater.setFeedURL(feedUrl);
//监听升级失败事件
autoUpdater.on('error', function (message) {
sendUpdateMessage({
cmd: 'error',
message: message,
});
});
//监听开始检测更新事件
autoUpdater.on('checking-for-update', function (message) {
sendUpdateMessage({
cmd: 'checking-for-update',
message: message,
});
});
//监听发现可用更新事件
autoUpdater.on('update-available', function (message) {
sendUpdateMessage({
cmd: 'update-available',
message: message,
});
});
//监听没有可用更新事件
autoUpdater.on('update-not-available', function (message) {
sendUpdateMessage({
cmd: 'update-not-available',
message: message,
});
});
// 更新下载进度事件
autoUpdater.on('download-progress', function (progressObj) {
sendUpdateMessage({
cmd: 'download-progress',
message: progressObj,
});
});
//监听下载完成事件
autoUpdater.on(
'update-downloaded',
function (event, releaseNotes, releaseName, releaseDate, updateUrl) {
sendUpdateMessage({
cmd: 'update-downloaded',
message: {
releaseNotes,
releaseName,
releaseDate,
updateUrl,
},
});
//退出并安装更新包
autoUpdater.quitAndInstall();
}
);
//接收渲染进程消息,开始检查更新
ipcMain.on('checkForUpdate', () => {
//执行自动更新检查
autoUpdater.checkForUpdates();
});
}
//给渲染进程发送消息
function sendUpdateMessage(text) {
mainWindow.webContents.send('message', text);
}
//渲染进程监听
ipcRenderer.on('message', (event, arg) => {
console.log(arg);
if ('update-available' == arg.cmd) {
//显示升级对话框
this.update.dialogVisible = true;
this.update.title = '正在更新,请稍候...';
this.update.find = true;
this.update.version = arg.message.version;
this.update.html = arg.message.releaseNotes;
} else if ('download-progress' == arg.cmd) {
let percent = Math.round(parseFloat(arg.message.percent));
this.update.percentage = percent;
} else if ('update-not-available' == arg.cmd) {
this.update.version = arg.message.version;
this.update.dialogVisible = true;
this.update.showClose = true;
}
});
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
上次更新: 2023/08/13, 16:38:25
