• electron-updater


    前提备份:

    安装一下更新插件

    npm install electron-updater --save 

    app | Electron (electronjs.org)更多配置参考app | Electron (electronjs.org)

    自动更新 electron-updater
    1、安装依赖 yarn add electron-updater

    2、自定义更新的文件 handleUpdate.js

    注: 打包完会生成一个latest.yml文件,把这个文件和生成的.exe文件放在服务器上,会自动检测版本

     自定义更新的文件 handleUpdate.js-版本1:

    1. import {
    2. autoUpdater
    3. } from 'electron-updater'
    4. import {
    5. ipcMain,dialog
    6. } from 'electron'
    7. let mainWindow = null;
    8. export function handleUpdate(window, feedUrl) {
    9. mainWindow = window;
    10. let message = {
    11. error: '检查更新出错',
    12. checking: '正在检查更新……',
    13. updateAva: '检测到新版本,正在下载……',
    14. updateNotAva: '现在使用的就是最新版本,不用更新',
    15. };
    16. `autoUpdater.autoDownload = false;` //取消自动下载
    17. //设置更新包的地址
    18. autoUpdater.setFeedURL(feedUrl);
    19. //监听升级失败事件
    20. autoUpdater.on('error', function(error) {
    21. sendUpdateMessage({
    22. cmd: 'error',
    23. message: error
    24. })
    25. });
    26. //监听开始检测更新事件
    27. autoUpdater.on('checking-for-update', function(message) {
    28. sendUpdateMessage({
    29. cmd: 'checking-for-update',
    30. message: message
    31. })
    32. });
    33. //监听发现可用更新事件
    34. autoUpdater.on('update-available', function(message) {
    35. sendUpdateMessage({
    36. cmd: 'update-available',
    37. message: message
    38. })
    39. //新加内容
    40. /** `const options = {
    41. type: 'info',
    42. buttons: ['确定', '取消'],
    43. title: '更新提示',
    44. message: '发现有新版本,是否更新?',
    45. cancelId: 1
    46. }
    47. dialog.showMessageBox(options).then(res => {
    48. if (res.response === 0) {
    49. sendUpdateMessage({
    50. cmd: 'confimUpdate',
    51. message: message
    52. })
    53. autoUpdater.downloadUpdate()
    54. } else {
    55. return;
    56. }
    57. })`*/
    58. });
    59. //监听没有可用更新事件
    60. autoUpdater.on('update-not-available', function(message) {
    61. sendUpdateMessage({
    62. cmd: 'update-not-available',
    63. message: message
    64. })
    65. });
    66. // 更新下载进度事件
    67. autoUpdater.on('download-progress', function(progressObj) {
    68. sendUpdateMessage({
    69. cmd: 'download-progress',
    70. message: progressObj
    71. })
    72. });
    73. //监听下载完成事件
    74. autoUpdater.on('update-downloaded', function(event, releaseNotes, releaseName, releaseDate, updateUrl) {
    75. sendUpdateMessage({
    76. cmd: 'update-downloaded',
    77. message: {
    78. releaseNotes,
    79. releaseName,
    80. releaseDate,
    81. updateUrl
    82. }
    83. })
    84. //退出并安装更新包
    85. //autoUpdater.quitAndInstall();
    86. });
    87. //新增
    88. ipcMain.on("quit-install", (e, arg) => {
    89. autoUpdater.quitAndInstall();
    90. })
    91. //接收渲染进程消息,开始检查更新
    92. ipcMain.on("checkForUpdate", (e, arg) => {
    93. //执行自动更新检查
    94. // sendUpdateMessage({cmd:'checkForUpdate',message:arg})
    95. autoUpdater.checkForUpdates();
    96. })
    97. }
    98. //给渲染进程发送消息
    99. function sendUpdateMessage(text) {
    100. mainWindow.webContents.send('message', text)
    101. }

    版本2:

    1. const { ipcMain } = require('electron')
    2. const { autoUpdater } = require("electron-updater")
    3. const { build } = require("../../../package.json")
    4. // 用户反馈立即更新
    5. ipcMain.on('ev-update-now', () => {
    6. console.log('ev-update-now::: 用户同意更新,开始更新')
    7. autoUpdater.quitAndInstall()
    8. })
    9. // 用户也可以通过点击按钮去检测更新
    10. ipcMain.on('ev-check-for-update', () => {
    11. console.log('ev-check-for-update::: 执行自动更新检查')
    12. autoUpdater.checkForUpdates()
    13. })
    14. function handleUpdate(mainWindow) {
    15. const message = {
    16. error: '检查更新出错',
    17. checking: '正在检查更新……',
    18. updateAva: '检测到新版本,正在下载……',
    19. updateNotAva: '现在使用的就是最新版本,不用更新'
    20. }
    21. autoUpdater.setFeedURL(build.publish[0].url) // 设置下载地址
    22. // 检查更新出错
    23. autoUpdater.on('error', () => {
    24. console.log('autoUpdater-error:::', arguments)
    25. sendUpdateMessage(message.error)
    26. })
    27. // 检查是否有版本更新
    28. autoUpdater.on('checking-for-update', () => {
    29. console.log('checking-for-update:::', arguments)
    30. sendUpdateMessage(message.checking)
    31. })
    32. // 检测到有版本更新
    33. autoUpdater.on('update-available', () => {
    34. console.log('update-available:::', arguments)
    35. sendUpdateMessage(message.updateAva)
    36. })
    37. // 未发现有新版本
    38. autoUpdater.on('update-not-available', () => {
    39. console.log('update-not-available:::', arguments)
    40. sendUpdateMessage(message.updateNotAva)
    41. })
    42. // 更新下载进度事件
    43. autoUpdater.on('download-progress', progressObj => {
    44. console.log('download-progress:::', progressObj)
    45. mainWindow.setProgressBar(progressObj.percent / 100)
    46. })
    47. // 下载完成,询问用户是否更新
    48. autoUpdater.on('update-downloaded', (event, releaseNotes, releaseName, releaseDate, updateUrl, quitAndUpdate) => {
    49. console.log('update-downloaded::: 下载完成,询问用户是否更新')
    50. mainWindow.webContents.send('ev-should-update', {
    51. event,
    52. releaseNotes,
    53. releaseName,
    54. releaseDate,
    55. updateUrl,
    56. quitAndUpdate
    57. })
    58. })
    59. function sendUpdateMessage(text) {
    60. mainWindow.webContents.send('ev-message', text)
    61. }
    62. }
    63. module.exports = {
    64. handleUpdate
    65. }

     3、在主进程文件main.js里面引用 handleUpdate.js

    1. const {
    2. handleUpdate
    3. } = require('./handleUpdate.js') //根据自己路径引入 我跟main.js同级放的

    使用:

    1. mainWindow.on('closed', () => {
    2. mainWindow = null
    3. })
    4. //自动更新函数
    5. handleUpdate(mainWindow) // 在这里执行,并把mainWindow传入

  • 相关阅读:
    C Primer Plus(6) 中文版 第2章 C语言概述 2.5 进一步使用C
    神奇的 SQL ,同时实现小计与合计,阁下该如何应对
    卷积神经网络结合ESMM求解ctrcvr问题
    java+python离退休人员工资管理系统
    C++:类和对象(三)
    Web3.0简介
    Python常用命令总结【持续更新】
    10、C++设计模式与泛型编程
    高精度乘除法(超详细)
    Mysql基础【操作数据库表】
  • 原文地址:https://blog.csdn.net/Hei_lovely_cat/article/details/132846939