autoUpdater.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. 'use strict';
  2. const updater = require("electron-updater");
  3. const autoUpdater = updater.autoUpdater;
  4. const config = require('../config');
  5. const {app} = require('electron');
  6. const eLogger = require('./eLogger').get();
  7. const helper = require('./helper');
  8. const constant = require('./constant');
  9. /**
  10. * 安装模块
  11. */
  12. exports.setup = function () {
  13. console.log('[electron-lib-autoUpater] [setup]');
  14. const version = app.getVersion();
  15. eLogger.info('[autoUpdater] [setup] current version: ', version);
  16. // 设置下载服务器地址
  17. const updateConfig = config.get('autoUpdate');
  18. let server = updateConfig.options.url;
  19. let lastChar = server.substring(server.length - 1);
  20. server = lastChar === '/' ? server : server + "/";
  21. eLogger.info('[autoUpdater] [setup] server: ', server);
  22. updateConfig.options.url = server;
  23. // 是否自动下载
  24. autoUpdater.autoDownload = updateConfig.force ? true : false;
  25. try {
  26. autoUpdater.setFeedURL(updateConfig.options);
  27. } catch (error) {
  28. eLogger.error('[autoUpdater] [setup] setFeedURL error : ', error);
  29. }
  30. autoUpdater.on('checking-for-update', () => {
  31. //sendStatusToWindow('正在检查更新...');
  32. })
  33. autoUpdater.on('update-available', (info) => {
  34. info.status = constant.appUpdaterStatus.available;
  35. info.desc = '有可用更新';
  36. sendStatusToWindow(info);
  37. })
  38. autoUpdater.on('update-not-available', (info) => {
  39. info.status = constant.appUpdaterStatus.noAvailable;
  40. info.desc = '没有可用更新';
  41. sendStatusToWindow(info);
  42. })
  43. autoUpdater.on('error', (err) => {
  44. let info = {
  45. status: constant.appUpdaterStatus.error,
  46. desc: err
  47. }
  48. sendStatusToWindow(info);
  49. })
  50. autoUpdater.on('download-progress', (progressObj) => {
  51. let text = "下载进度: " + progressObj.bytesPerSecond;
  52. text = text + ' - 已下载 ' + progressObj.percent + '%';
  53. text = text + ' (' + progressObj.transferred + "/" + progressObj.total + ')';
  54. let info = {
  55. status: constant.appUpdaterStatus.downloading,
  56. desc: text
  57. }
  58. sendStatusToWindow(info);
  59. })
  60. autoUpdater.on('update-downloaded', (info) => {
  61. console.log('downloaded info:', info)
  62. info.status = constant.appUpdaterStatus.downloaded;
  63. info.desc = '下载完成';
  64. sendStatusToWindow(info);
  65. // quit and update
  66. helper.appQuit();
  67. autoUpdater.quitAndInstall();
  68. });
  69. };
  70. exports.checkUpdate = function () {
  71. autoUpdater.checkForUpdates();
  72. }
  73. exports.download = function () {
  74. autoUpdater.downloadUpdate();
  75. }
  76. function sendStatusToWindow(content = {}) {
  77. const textJson = JSON.stringify(content);
  78. eLogger.info(textJson);
  79. MAIN_WINDOW.webContents.send(constant.ipcChannels.appUpdater, textJson);
  80. }
  81. exports = module.exports;