index.js 4.4 KB

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