autoUpdater.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 percentStr = String(progressObj.percent);
  56. let endIndex = percentStr.indexOf('.') != -1 ? percentStr.indexOf('.') : percentStr.length;
  57. let percentNumber = percentStr.substring(0, endIndex);
  58. let totalSize = bytesChange(progressObj.total);
  59. let transferredSize = bytesChange(progressObj.transferred);
  60. let text = '已下载 ' + percentNumber + '%';
  61. text = text + ' (' + transferredSize + "/" + totalSize + ')';
  62. let info = {
  63. status: constant.appUpdaterStatus.downloading,
  64. desc: text,
  65. percentNumber: percentNumber,
  66. totalSize: totalSize,
  67. transferredSize: transferredSize
  68. }
  69. sendStatusToWindow(info);
  70. })
  71. autoUpdater.on('update-downloaded', (info) => {
  72. info.status = constant.appUpdaterStatus.downloaded;
  73. info.desc = '下载完成';
  74. sendStatusToWindow(info);
  75. // quit and update
  76. helper.appQuit();
  77. autoUpdater.quitAndInstall();
  78. });
  79. };
  80. exports.checkUpdate = function () {
  81. autoUpdater.checkForUpdates();
  82. }
  83. exports.download = function () {
  84. autoUpdater.downloadUpdate();
  85. }
  86. function sendStatusToWindow(content = {}) {
  87. const textJson = JSON.stringify(content);
  88. eLogger.info(textJson);
  89. MAIN_WINDOW.webContents.send(constant.ipcChannels.appUpdater, textJson);
  90. }
  91. function bytesChange (limit) {
  92. let size = "";
  93. if(limit < 0.1 * 1024){
  94. size = limit.toFixed(2) + "B"
  95. }else if(limit < 0.1 * 1024 * 1024){
  96. size = (limit/1024).toFixed(2) + "KB"
  97. }else if(limit < 0.1 * 1024 * 1024 * 1024){
  98. size = (limit/(1024 * 1024)).toFixed(2) + "MB"
  99. }else{
  100. size = (limit/(1024 * 1024 * 1024)).toFixed(2) + "GB"
  101. }
  102. let sizeStr = size + "";
  103. let index = sizeStr.indexOf(".");
  104. let dou = sizeStr.substring(index + 1 , index + 3)
  105. if(dou == "00"){
  106. return sizeStr.substring(0, index) + sizeStr.substring(index + 3, index + 5)
  107. }
  108. return size;
  109. }
  110. exports = module.exports;