autoUpdater.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. const path = require('path');
  10. /**
  11. * 安装模块
  12. */
  13. exports.setup = function () {
  14. console.log('[electron-lib-autoUpater] [setup]');
  15. const version = app.getVersion();
  16. eLogger.info('[autoUpdater] [setup] current version: ', version);
  17. // 设置下载服务器地址
  18. const updateConfig = config.get('autoUpdate');
  19. let server = updateConfig.options.url;
  20. let lastChar = server.substring(server.length - 1);
  21. server = lastChar === '/' ? server : server + "/";
  22. eLogger.info('[autoUpdater] [setup] server: ', server);
  23. updateConfig.options.url = server;
  24. // 是否后台自动下载
  25. autoUpdater.autoDownload = updateConfig.force ? true : false;
  26. if (process.env.EE_SERVER_ENV == 'local') {
  27. autoUpdater.updateConfigPath = path.join(__dirname, '../../out/dev-app-update.yml')
  28. }
  29. try {
  30. autoUpdater.setFeedURL(updateConfig.options);
  31. } catch (error) {
  32. eLogger.error('[autoUpdater] [setup] setFeedURL error : ', error);
  33. }
  34. autoUpdater.on('checking-for-update', () => {
  35. //sendStatusToWindow('正在检查更新...');
  36. })
  37. autoUpdater.on('update-available', (info) => {
  38. info.status = constant.appUpdaterStatus.available;
  39. info.desc = '有可用更新';
  40. sendStatusToWindow(info);
  41. })
  42. autoUpdater.on('update-not-available', (info) => {
  43. info.status = constant.appUpdaterStatus.noAvailable;
  44. info.desc = '没有可用更新';
  45. sendStatusToWindow(info);
  46. })
  47. autoUpdater.on('error', (err) => {
  48. let info = {
  49. status: constant.appUpdaterStatus.error,
  50. desc: err
  51. }
  52. sendStatusToWindow(info);
  53. })
  54. autoUpdater.on('download-progress', (progressObj) => {
  55. let percentNumber = parseInt(progressObj.percent);
  56. let totalSize = bytesChange(progressObj.total);
  57. let transferredSize = bytesChange(progressObj.transferred);
  58. let text = '已下载 ' + percentNumber + '%';
  59. text = text + ' (' + transferredSize + "/" + totalSize + ')';
  60. let info = {
  61. status: constant.appUpdaterStatus.downloading,
  62. desc: text,
  63. percentNumber: percentNumber,
  64. totalSize: totalSize,
  65. transferredSize: transferredSize
  66. }
  67. sendStatusToWindow(info);
  68. })
  69. autoUpdater.on('update-downloaded', (info) => {
  70. info.status = constant.appUpdaterStatus.downloaded;
  71. info.desc = '下载完成';
  72. sendStatusToWindow(info);
  73. // quit and update
  74. helper.appQuit();
  75. autoUpdater.quitAndInstall();
  76. });
  77. };
  78. exports.checkUpdate = function () {
  79. autoUpdater.checkForUpdates();
  80. }
  81. exports.download = function () {
  82. autoUpdater.downloadUpdate();
  83. }
  84. function sendStatusToWindow(content = {}) {
  85. const textJson = JSON.stringify(content);
  86. eLogger.info(textJson);
  87. MAIN_WINDOW.webContents.send(constant.ipcChannels.appUpdater, textJson);
  88. }
  89. function bytesChange (limit) {
  90. let size = "";
  91. if(limit < 0.1 * 1024){
  92. size = limit.toFixed(2) + "B"
  93. }else if(limit < 0.1 * 1024 * 1024){
  94. size = (limit/1024).toFixed(2) + "KB"
  95. }else if(limit < 0.1 * 1024 * 1024 * 1024){
  96. size = (limit/(1024 * 1024)).toFixed(2) + "MB"
  97. }else{
  98. size = (limit/(1024 * 1024 * 1024)).toFixed(2) + "GB"
  99. }
  100. let sizeStr = size + "";
  101. let index = sizeStr.indexOf(".");
  102. let dou = sizeStr.substring(index + 1 , index + 3)
  103. if(dou == "00"){
  104. return sizeStr.substring(0, index) + sizeStr.substring(index + 3, index + 5)
  105. }
  106. return size;
  107. }
  108. exports = module.exports;