cli3 + electron-updater 自动升级dome
1、cli3创建项目
2、添加 vue-cli-plugin-electron-builder、electron-updater
package.json
配置build
下publish
参数如下:
3、主进程添加代码background.js
// 检测更新
!function updateHandle() {
let message = {
error: {type: 1, info: '检查更新出错'},
checking: {type: 2, info: '正在检查更新……'},
updateAva: {type: 3, info: '检测到新版本,正在下载……'},
updateNotAva: {type: 4, info: '现在使用的就是最新版本,不用更新'},
};
const uploadUrl = "http://192.168.1.119:8000/"; // 下载地址,不加后面的**.exe
autoUpdater.setFeedURL(uploadUrl);
autoUpdater.on('error', function (error) {
console.log(error);
sendUpdateMessage(message.error)
});
autoUpdater.on('checking-for-update', function () {
console.log("检测版本信息");
sendUpdateMessage(message.checking)
});
autoUpdater.on('update-available', function (info) {
console.log("检测到新版本,正在下载:****");
console.log(info);
sendUpdateMessage(message.updateAva)
});
autoUpdater.on('update-not-available', function () {
sendUpdateMessage(message.updateNotAva)
});
// 更新下载进度事件
autoUpdater.on('download-progress', function (progressObj) {
win.webContents.send('downloadProgress', progressObj)
});
autoUpdater.on('update-downloaded', function (event, releaseNotes, releaseName, releaseDate, updateUrl, quitAndUpdate) {
console.log(event);
console.log(releaseNotes);
console.log(releaseName);
console.log(releaseDate);
console.log(updateUrl);
console.log(quitAndUpdate);
ipcMain.on('isUpdateNow', (e, arg) => {
console.log(e);
console.log(arg);
console.log(arguments);
console.log("开始更新");
//some code here to handle event
autoUpdater.quitAndInstall();
});
win.webContents.send('isUpdateNow')
});
ipcMain.on("checkForUpdate", () => {
//执行自动更新检查
autoUpdater.checkForUpdates();
})
}();
// 通过main进程发送事件给renderer进程,提示更新信息
function sendUpdateMessage(text) {
win.webContents.send('message', text)
}
4、渲染进程添加代码bottomBar.vue
async checkUpdate() {
this.updateDialogVisible = true;
this.tips = {};
this.downloadPercent = 0;
const _this = this;
_this.$electron.ipcRenderer.send("checkForUpdate");
await _this.$electron.ipcRenderer.on("message", (event, text) => {
_this.tips = text;
});
_this.$electron.ipcRenderer.on("downloadProgress", (event, progressObj) => {
_this.downloadPercent = progressObj.percent || 0;
});
_this.$electron.ipcRenderer.on("isUpdateNow", () => {
_this.$electron.ipcRenderer.send("isUpdateNow");
});
},
效果图
源代码:https://github.com/waveTan/wallet_2.0