autoUpdater.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. 'use strict';
  2. const updater = require("electron-updater");
  3. const autoUpdater = updater.autoUpdater;
  4. const config = require('../config');
  5. const {app} = require('electron');
  6. const eLogger = require('./eLogger').get();
  7. const helper = require('./helper');
  8. /**
  9. * 安装模块
  10. */
  11. exports.setup = function () {
  12. console.log('[electron-lib-autoUpater] [setup]');
  13. const version = app.getVersion();
  14. eLogger.info('[autoUpdater] [setup] current version: ', version);
  15. // 设置下载服务器地址
  16. const updateConfig = config.get('autoUpdate');
  17. let server = updateConfig.options.url;
  18. let lastChar = server.substring(server.length - 1);
  19. server = lastChar === '/' ? server : server + "/";
  20. eLogger.info('[autoUpdater] [setup] server: ', server);
  21. updateConfig.options.url = server;
  22. // 是否自动下载
  23. autoUpdater.autoDownload = updateConfig.force ? true : false;
  24. try {
  25. autoUpdater.setFeedURL(updateConfig.options);
  26. } catch (error) {
  27. eLogger.error('[autoUpdater] [setup] setFeedURL error : ', error);
  28. }
  29. autoUpdater.on('checking-for-update', () => {
  30. sendStatusToWindow('正在检查更新...');
  31. })
  32. autoUpdater.on('update-available', (info) => {
  33. sendStatusToWindow('有可用更新');
  34. })
  35. autoUpdater.on('update-not-available', (info) => {
  36. sendStatusToWindow('没有可用更新');
  37. })
  38. autoUpdater.on('error', (err) => {
  39. sendStatusToWindow('更新异常: ' + err);
  40. })
  41. autoUpdater.on('download-progress', (progressObj) => {
  42. let log_message = "下载进度: " + progressObj.bytesPerSecond;
  43. log_message = log_message + ' - 已下载 ' + progressObj.percent + '%';
  44. log_message = log_message + ' (' + progressObj.transferred + "/" + progressObj.total + ')';
  45. sendStatusToWindow(log_message);
  46. })
  47. autoUpdater.on('update-downloaded', (info) => {
  48. sendStatusToWindow('下载完成');
  49. // quit and update
  50. if (updateConfig.force) {
  51. helper.appQuit();
  52. autoUpdater.quitAndInstall();
  53. }
  54. });
  55. };
  56. exports.checkUpdate = function () {
  57. autoUpdater.checkForUpdates();
  58. }
  59. function sendStatusToWindow(text) {
  60. eLogger.info(text);
  61. MAIN_WINDOW.webContents.send('public.message', text);
  62. }
  63. exports = module.exports;