index.js 4.3 KB

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