autoUpdater.js 1.6 KB

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