Yolofyi's Guide
首页
  • 前端文章

    • JavaScript
    • HTML
    • CSS
  • 学习笔记

    • 《JavaScript教程》
    • 《JavaScript高级程序设计》
    • 《ES6 教程》
    • 《Vue》
    • 《React》
    • 《TypeScript 从零实现 axios》
    • 《Git》
    • TypeScript
    • JS设计模式总结
  • Mysql

    • Mysql
  • Java

    • Java基础
  • 技术文档
  • GitHub技巧
  • Nodejs
  • 博客搭建
  • 学习
  • 面试
  • 助手
收藏
  • 分类
  • 标签
  • 归档

Yolofyi

船是自己,灯塔是自己,岸也是自己
首页
  • 前端文章

    • JavaScript
    • HTML
    • CSS
  • 学习笔记

    • 《JavaScript教程》
    • 《JavaScript高级程序设计》
    • 《ES6 教程》
    • 《Vue》
    • 《React》
    • 《TypeScript 从零实现 axios》
    • 《Git》
    • TypeScript
    • JS设计模式总结
  • Mysql

    • Mysql
  • Java

    • Java基础
  • 技术文档
  • GitHub技巧
  • Nodejs
  • 博客搭建
  • 学习
  • 面试
  • 助手
收藏
  • 分类
  • 标签
  • 归档
  • HTML

  • CSS

  • JavaScript文章

  • 学习笔记

  • Electron

    • node获取桌面快捷图标
    • electron-updater
    • electron-builder打包相关配置
  • 前端
  • Electron
yolofyi
2023-08-04

electron-updater

# 开发者模式下测试 electron-updater 自动升级

const { app } = require('electron');
//手动设置app isPackaged
Object.defineProperty(app, 'isPackaged', {
  get() {
    return true;
  }
});
1
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
上次更新: 2023/08/13, 16:38:25
node获取桌面快捷图标
electron-builder打包相关配置

← node获取桌面快捷图标 electron-builder打包相关配置→

最近更新
01
MySQL开发规范及慢查询优化
08-25
02
linux增加swap交换空间
08-16
03
uni-app云打包Android Apk
08-13
更多文章>
| Copyright © 2022-2023 yolofyi.com - All rights reserved | 鄂ICP备2022003053号 |
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式