autoUpdater.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. 'use strict';
  2. const updater = require("electron-updater");
  3. const autoUpdater = updater.autoUpdater;
  4. const config = require('../config');
  5. const path = require('path');
  6. const {app} = require('electron');
  7. const eLogger = require('./eLogger').get();
  8. exports.setup = function () {
  9. const pkgInfo = require(path.join(app.getAppPath(), 'package.json'));
  10. eLogger.info('[autoUpdater] [setup] current version: ', pkgInfo.version);
  11. const updateConfig = config.get('autoUpdate');
  12. autoUpdater.setFeedURL(updateConfig.options);
  13. autoUpdater.on('checking-for-update', () => {
  14. sendStatusToWindow('Checking for update...');
  15. })
  16. autoUpdater.on('update-available', (info) => {
  17. sendStatusToWindow('Update available.');
  18. })
  19. autoUpdater.on('update-not-available', (info) => {
  20. sendStatusToWindow('Update not available.');
  21. })
  22. autoUpdater.on('error', (err) => {
  23. sendStatusToWindow('Error in auto-updater. ' + err);
  24. })
  25. autoUpdater.on('download-progress', (progressObj) => {
  26. let log_message = "Download speed: " + progressObj.bytesPerSecond;
  27. log_message = log_message + ' - Downloaded ' + progressObj.percent + '%';
  28. log_message = log_message + ' (' + progressObj.transferred + "/" + progressObj.total + ')';
  29. sendStatusToWindow(log_message);
  30. })
  31. autoUpdater.on('update-downloaded', (info) => {
  32. sendStatusToWindow('Update downloaded');
  33. // quit and update
  34. MAIN_WINDOW.destroy();
  35. app.quit();
  36. autoUpdater.quitAndInstall();
  37. });
  38. };
  39. exports.checkUpdate = function () {
  40. autoUpdater.checkForUpdatesAndNotify();
  41. }
  42. function sendStatusToWindow(text) {
  43. eLogger.info(text);
  44. MAIN_WINDOW.webContents.send('message', text);
  45. }
  46. exports = module.exports;