electron-updater实现更新electron应用程序

摘要:
electron-updater实现更新electron应用程序第一步安装"electron-updater":"^4.3.5",打开package.json文件在build对象下添加publish配置,"build":{"productName":"xxx","appId":"org.simulatedgreg.electron-vue","directories":{"output":"bui
electron-updater实现更新electron应用程序

第一步

  1. 安装"electron-updater": "^4.3.5",
  2. 打开package.json文件在build对象下添加publish配置,
  "build": {
"productName": "xxx",
"appId": "org.simulatedgreg.electron-vue",
"directories": {
"output": "build"
},
---------------------------------------------
"publish": [
{
"provider": "generic",
"url": "https://xxx.com"
//注:这个url就是放.yml文件和安装包的服务器地址,我这里用的是阿里云oss地址
}
],
--------------------------------------------------
"files": [
"dist/electron/**/*"
]

第二步

  1. 在main文件夹下面创建更新文件update.js
import { autoUpdater } from 'electron-updater'
import { ipcMain } from 'electron'
let mainWindow = null;
export function updateHandle(window, feedUrl) {
mainWindow = window;
let message = {
error: '检查更新出错',
checking: '正在检查更新',
updateAva: '检测到新版本,正在下载',
updateNotAva: '您已经更新到最新版本了',
};
//设置更新包的地址
autoUpdater.setFeedURL(feedUrl);
//监听升级失败事件
autoUpdater.on('error', function (error) {
sendUpdateMessage({
cmd: 'error',
message: error
})
});
//监听开始检测更新事件
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", (e, arg) => {
//执行自动更新检查
// sendUpdateMessage({cmd:'checkForUpdate',message:arg})
autoUpdater.checkForUpdates();
})
}
//给渲染进程发送消息
function sendUpdateMessage(text) {
mainWindow.webContents.send('message', text)
}
  1. 在main文件夹下面的index.js中引入update.js
import { updateHandle } from './update'
mainWindow.on('closed', () => {
mainWindow = null
})
----------------------------------------------------
let feedUrl = "https://xxxxx.com";
updateHandle(mainWindow,feedUrl);
-----------------------------------------------------
}

第三步

  1. APP.js文件中检测更新
  <template>
<div id="app">
<router-view></router-view>
<el-dialog
title="正在更新新版本,请稍候..."
:visible.sync="dialogVisible"
width="60%"
:close-on-click-modal="closeOnClickModal"
:close-on-press-escape="closeOnPressEscape"
:show-close="showClose"
center
>
<div style="100%;height:15vh;line-height:15vh;text-align:center">
<el-progress
status="success"
:text-inside="true"
:stroke-width="20"
:percentage="percentage"
:width="strokeWidth"
:show-text="true"
></el-progress>
</div>
</el-dialog>
</div>
</template>
<script>
let ipcRenderer = require("electron").ipcRenderer;
let _this = this;
//接收主进程版本更新消息
ipcRenderer.on("message", (event, arg) => {
// for (var i = 0; i < arg.length; i++) {
console.log(arg);
if ("update-available" == arg.cmd) {
//显示升级对话框
_this.dialogVisible = true;
} else if ("download-progress" == arg.cmd) {
//更新升级进度
/**
* 
* message{bytesPerSecond: 47673
delta: 48960
percent: 0.11438799862426002
total: 42801693
transferred: 48960
}
*/
console.log(arg.message.percent);
let percent = Math.round(parseFloat(arg.message.percent));
_this.percentage = percent;
} else if ("error" == arg.cmd) {
_this.dialogVisible = false;
_this.$message("更新失败");
}
// }
});
ipcRenderer.send("checkForUpdate");
//20秒后开始检测新版本
// let timeOut = window.setTimeout(() => {
//   ipcRenderer.send("checkForUpdate");
// }, 20000);
clearTimeout;
//间隔1小时检测一次
// let interval = window.setInterval(() => {
//   ipcRenderer.send("checkForUpdate");
// }, 3600000);
export default {
name: 'App',
data() {
return {
dialogVisible: false,
closeOnClickModal: false,
closeOnPressEscape: false,
showClose: false,
percentage: 0,
strokeWidth:200
};
},
mounted() {
_this = this;
},
destroyed() {
window.clearInterval(interval);
window.clearInterval(timeOut);
}
}
</script>

第四步

将项目打包yarn build,将打包后生成的build目录下的latest.yml和安装包.exe文件传入服务器即可

坑!!!

注:我在开发的时候遇到了一个坑,打包成功后打开项目报错 A JavaScript error occurrend in the main process

这是electron版本过低导致的,后来我将版本升级到4.0.1,再打包就正常运行了。

本文使用 mdnice 排版

免责声明:文章转载自《electron-updater实现更新electron应用程序》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇快速开发框架jeesiteGit-更新数据下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

软件产品案例分析(团队)

任务分配流程图 第一部分 调研,评测 【评测】 下载并使用,描述最简单直观的个人第一次上手体验。 移动端 软件登录界面,色彩清新,操作流畅,这种风格看着还是蛮舒服的。不过登录、注册等按钮距离间隔太近容易误操作。 注册界面直接以web网页形式加载,响应速度较慢,体验并不是很好。 用户的主界面非常简洁清新,“我的”界面除了用户名、区域看不到用户的其他信息。...

利用less监视模式实时预览样式刷新浏览器

【前言】此处介绍的方法只是我个人的用法,相信大家有更好更简洁的方式。 上次写到利用LiveReload解放F5。而且LiveReload可以编辑sass/less/stylus。但是可惜发现LiveReload在编译的时候不能抛错,这就很麻烦了,少了个标点less编译就不过,查找起来太麻烦。 我目前的解决方法: 利用Less自带的客户端开发模式(devel...

软件过程

典型软件生存周期: 需求分析——软件分析——软件设计——编码(测试)——软件测试——运行维护 软件过程 1.瀑布模型: 2.快速原型模型 快速原型模型需要迅速建造一个可以运行的软件原型 ,以便理解和澄清问题,使开发人员与用户达成共识,最终在确定的客户需求基础上开发客户满意的软件产品。 快速原型模型允许在需求分析阶段对软件的需求进行初步而非完全的...

vue:app.vue中添加监听beforeunload事件,即当浏览器窗口关闭或刷新时删除vuex中的数据

当打开页面时,添加事件监听,即监听beforeunload事件,beforeunload事件在关闭页面时触发。即当关闭页面时,手动删除localStorage中的数据。 app.vue中的代码如下: <template> <div id="app"> <router-view /> </div>...

移动App设计之分层架构+MVC

场景分析:我们知道,一个移动设备的应用大多与网络有关,也就是说,我在移动设备上看到的数据,一般都是从Server上”拉“过来,显示在我们的移动设备(ios androiud、wpohone等)上。那我们就这个”拉“的过程分析,拉什么样的数据?去哪里拉?拉过来的数据怎么处理?用编程(开发)的思维看,就是定义什么实体(业务实体)、发送请求、解析数据。当然这也只...

【FLASH教程】Adobe Flash CS4 官方中下载及安装

http://yaorao.16789.net/index.asp?ActionX=ReadArt&NewsID=2021330 Adobe Flash CS4 官方中文版的下载及安装说明          一、下载:[Adobe Flash CS4官方简体中文版]       软件大小:1.26G    软件安装要求:内存 1G     电驴或...