index.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. // if (Utils.getEnv() == 'local') {
  50. // autoUpdater.updateConfigPath = path.join(__dirname, '../../out/dev-app-update.yml')
  51. // }
  52. try {
  53. autoUpdater.setFeedURL(updateConfig.options);
  54. } catch (error) {
  55. this.app.logger.error('[addon:autoUpdater] setFeedURL error : ', error);
  56. }
  57. autoUpdater.on('checking-for-update', () => {
  58. //sendStatusToWindow('正在检查更新...');
  59. })
  60. autoUpdater.on('update-available', (info) => {
  61. info.status = status.available;
  62. info.desc = '有可用更新';
  63. this.sendStatusToWindow(info);
  64. })
  65. autoUpdater.on('update-not-available', (info) => {
  66. info.status = status.noAvailable;
  67. info.desc = '没有可用更新';
  68. this.sendStatusToWindow(info);
  69. })
  70. autoUpdater.on('error', (err) => {
  71. let info = {
  72. status: status.error,
  73. desc: err
  74. }
  75. this.sendStatusToWindow(info);
  76. })
  77. autoUpdater.on('download-progress', (progressObj) => {
  78. let percentNumber = parseInt(progressObj.percent);
  79. let totalSize = this.bytesChange(progressObj.total);
  80. let transferredSize = this.bytesChange(progressObj.transferred);
  81. let text = '已下载 ' + percentNumber + '%';
  82. text = text + ' (' + transferredSize + "/" + totalSize + ')';
  83. let info = {
  84. status: status.downloading,
  85. desc: text,
  86. percentNumber: percentNumber,
  87. totalSize: totalSize,
  88. transferredSize: transferredSize
  89. }
  90. this.app.logger.info('[addon:autoUpdater] progress: ', text);
  91. this.sendStatusToWindow(info);
  92. })
  93. autoUpdater.on('update-downloaded', (info) => {
  94. info.status = status.downloaded;
  95. info.desc = '下载完成';
  96. this.sendStatusToWindow(info);
  97. // quit and update
  98. this.app.appQuit();
  99. autoUpdater.quitAndInstall();
  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. this.mainWindow.webContents.send(channel, textJson);
  121. }
  122. /**
  123. * 单位转换
  124. */
  125. bytesChange (limit) {
  126. let size = "";
  127. if(limit < 0.1 * 1024){
  128. size = limit.toFixed(2) + "B";
  129. }else if(limit < 0.1 * 1024 * 1024){
  130. size = (limit/1024).toFixed(2) + "KB";
  131. }else if(limit < 0.1 * 1024 * 1024 * 1024){
  132. size = (limit/(1024 * 1024)).toFixed(2) + "MB";
  133. }else{
  134. size = (limit/(1024 * 1024 * 1024)).toFixed(2) + "GB";
  135. }
  136. let sizeStr = size + "";
  137. let index = sizeStr.indexOf(".");
  138. let dou = sizeStr.substring(index + 1 , index + 3);
  139. if(dou == "00"){
  140. return sizeStr.substring(0, index) + sizeStr.substring(index + 3, index + 5);
  141. }
  142. return size;
  143. }
  144. }
  145. AutoUpdaterAddon.toString = () => '[class AutoUpdaterAddon]';
  146. module.exports = AutoUpdaterAddon;