index.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. const { app: electronApp } = require('electron');
  2. const { autoUpdater } = require("electron-updater");
  3. const is = require('ee-core/utils/is');
  4. const Log = require('ee-core/log');
  5. const Conf = require('ee-core/config');
  6. const CoreWindow = require('ee-core/electron/window');
  7. const Electron = require('ee-core/electron');
  8. autoUpdater.forceDevUpdateConfig = true
  9. /**
  10. * 自动升级插件
  11. * @class
  12. */
  13. class AutoUpdaterAddon {
  14. constructor() {
  15. }
  16. /**
  17. * 创建
  18. */
  19. create () {
  20. Log.info('[addon:autoUpdater] load');
  21. const cfg = Conf.getValue('addons.autoUpdater');
  22. if ((is.windows() && cfg.windows)
  23. || (is.macOS() && cfg.macOS)
  24. || (is.linux() && cfg.linux))
  25. {
  26. // continue
  27. } else {
  28. return
  29. }
  30. // 是否检查更新
  31. if (cfg.force) {
  32. this.checkUpdate();
  33. }
  34. const status = {
  35. error: -1,
  36. available: 1,
  37. noAvailable: 2,
  38. downloading: 3,
  39. downloaded: 4,
  40. }
  41. const version = electronApp.getVersion();
  42. Log.info('[addon:autoUpdater] current version: ', version);
  43. // 设置下载服务器地址
  44. let server = cfg.options.url;
  45. let lastChar = server.substring(server.length - 1);
  46. server = lastChar === '/' ? server : server + "/";
  47. //Log.info('[addon:autoUpdater] server: ', server);
  48. cfg.options.url = server;
  49. // 是否后台自动下载
  50. autoUpdater.autoDownload = cfg.force ? true : false;
  51. try {
  52. autoUpdater.setFeedURL(cfg.options);
  53. } catch (error) {
  54. Log.error('[addon:autoUpdater] setFeedURL error : ', error);
  55. }
  56. autoUpdater.on('checking-for-update', () => {
  57. //sendStatusToWindow('正在检查更新...');
  58. })
  59. autoUpdater.on('update-available', (info) => {
  60. info.status = status.available;
  61. info.desc = '有可用更新';
  62. this.sendStatusToWindow(info);
  63. })
  64. autoUpdater.on('update-not-available', (info) => {
  65. info.status = status.noAvailable;
  66. info.desc = '没有可用更新';
  67. this.sendStatusToWindow(info);
  68. })
  69. autoUpdater.on('error', (err) => {
  70. let info = {
  71. status: status.error,
  72. desc: err
  73. }
  74. this.sendStatusToWindow(info);
  75. })
  76. autoUpdater.on('download-progress', (progressObj) => {
  77. let percentNumber = parseInt(progressObj.percent);
  78. let totalSize = this.bytesChange(progressObj.total);
  79. let transferredSize = this.bytesChange(progressObj.transferred);
  80. let text = '已下载 ' + percentNumber + '%';
  81. text = text + ' (' + transferredSize + "/" + totalSize + ')';
  82. let info = {
  83. status: status.downloading,
  84. desc: text,
  85. percentNumber: percentNumber,
  86. totalSize: totalSize,
  87. transferredSize: transferredSize
  88. }
  89. Log.info('[addon:autoUpdater] progress: ', text);
  90. this.sendStatusToWindow(info);
  91. })
  92. autoUpdater.on('update-downloaded', (info) => {
  93. info.status = status.downloaded;
  94. info.desc = '下载完成';
  95. this.sendStatusToWindow(info);
  96. // 托盘插件默认会阻止窗口关闭,这里设置允许关闭窗口
  97. Electron.extra.closeWindow = true;
  98. autoUpdater.quitAndInstall();
  99. // const mainWindow = CoreWindow.getMainWindow();
  100. // if (mainWindow) {
  101. // mainWindow.destroy()
  102. // }
  103. // electronApp.appQuit()
  104. });
  105. }
  106. /**
  107. * 检查更新
  108. */
  109. checkUpdate () {
  110. autoUpdater.checkForUpdates();
  111. }
  112. /**
  113. * 下载更新
  114. */
  115. download () {
  116. autoUpdater.downloadUpdate();
  117. }
  118. /**
  119. * 向前端发消息
  120. */
  121. sendStatusToWindow(content = {}) {
  122. const textJson = JSON.stringify(content);
  123. const channel = 'app.updater';
  124. const win = CoreWindow.getMainWindow();
  125. win.webContents.send(channel, textJson);
  126. }
  127. /**
  128. * 单位转换
  129. */
  130. bytesChange (limit) {
  131. let size = "";
  132. if(limit < 0.1 * 1024){
  133. size = limit.toFixed(2) + "B";
  134. }else if(limit < 0.1 * 1024 * 1024){
  135. size = (limit/1024).toFixed(2) + "KB";
  136. }else if(limit < 0.1 * 1024 * 1024 * 1024){
  137. size = (limit/(1024 * 1024)).toFixed(2) + "MB";
  138. }else{
  139. size = (limit/(1024 * 1024 * 1024)).toFixed(2) + "GB";
  140. }
  141. let sizeStr = size + "";
  142. let index = sizeStr.indexOf(".");
  143. let dou = sizeStr.substring(index + 1 , index + 3);
  144. if(dou == "00"){
  145. return sizeStr.substring(0, index) + sizeStr.substring(index + 3, index + 5);
  146. }
  147. return size;
  148. }
  149. }
  150. AutoUpdaterAddon.toString = () => '[class AutoUpdaterAddon]';
  151. module.exports = AutoUpdaterAddon;