index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. const { app } = require('electron');
  2. const { autoUpdater } = require("electron-updater");
  3. const is = require('electron-is');
  4. /**
  5. * 自动升级插件
  6. * @class
  7. */
  8. class AutoUpdaterAddon {
  9. constructor(app) {
  10. this.app = app;
  11. this.cfg = app.config.addons.autoUpdater;
  12. this.mainWindow = app.electron.mainWindow;
  13. }
  14. /**
  15. * 创建
  16. */
  17. create () {
  18. this.app.console.info('[addon:autoUpdater] load');
  19. if ((is.windows() && this.cfg.windows)
  20. || (is.macOS() && this.cfg.macOS)
  21. || (is.linux() && this.cfg.linux))
  22. {
  23. // continue
  24. } else {
  25. return
  26. }
  27. // 是否检查更新
  28. if (this.cfg.force) {
  29. this.checkUpdate();
  30. }
  31. const status = {
  32. error: -1,
  33. available: 1,
  34. noAvailable: 2,
  35. downloading: 3,
  36. downloaded: 4,
  37. }
  38. const updateConfig = this.cfg;
  39. const version = app.getVersion();
  40. this.app.logger.info('[addon:autoUpdater] current version: ', version);
  41. // 设置下载服务器地址
  42. let server = updateConfig.options.url;
  43. let lastChar = server.substring(server.length - 1);
  44. server = lastChar === '/' ? server : server + "/";
  45. //this.app.logger.info('[addon:autoUpdater] server: ', server);
  46. updateConfig.options.url = server;
  47. // 是否后台自动下载
  48. autoUpdater.autoDownload = updateConfig.force ? true : false;
  49. try {
  50. autoUpdater.setFeedURL(updateConfig.options);
  51. } catch (error) {
  52. this.app.logger.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. this.app.logger.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. // quit and update
  95. this.app.appQuit();
  96. autoUpdater.quitAndInstall();
  97. });
  98. }
  99. /**
  100. * 检查更新
  101. */
  102. checkUpdate () {
  103. autoUpdater.checkForUpdates();
  104. }
  105. /**
  106. * 下载更新
  107. */
  108. download () {
  109. autoUpdater.downloadUpdate();
  110. }
  111. /**
  112. * 向前端发消息
  113. */
  114. sendStatusToWindow(content = {}) {
  115. const textJson = JSON.stringify(content);
  116. const channel = 'app.updater';
  117. this.mainWindow.webContents.send(channel, textJson);
  118. }
  119. /**
  120. * 单位转换
  121. */
  122. bytesChange (limit) {
  123. let size = "";
  124. if(limit < 0.1 * 1024){
  125. size = limit.toFixed(2) + "B";
  126. }else if(limit < 0.1 * 1024 * 1024){
  127. size = (limit/1024).toFixed(2) + "KB";
  128. }else if(limit < 0.1 * 1024 * 1024 * 1024){
  129. size = (limit/(1024 * 1024)).toFixed(2) + "MB";
  130. }else{
  131. size = (limit/(1024 * 1024 * 1024)).toFixed(2) + "GB";
  132. }
  133. let sizeStr = size + "";
  134. let index = sizeStr.indexOf(".");
  135. let dou = sizeStr.substring(index + 1 , index + 3);
  136. if(dou == "00"){
  137. return sizeStr.substring(0, index) + sizeStr.substring(index + 3, index + 5);
  138. }
  139. return size;
  140. }
  141. }
  142. AutoUpdaterAddon.toString = () => '[class AutoUpdaterAddon]';
  143. module.exports = AutoUpdaterAddon;