autoUpdater.js 4.0 KB

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