11'use strict' ;
2- const { app, dialog } = require ( 'electron' ) ;
2+ const { app, dialog, shell } = require ( 'electron' ) ;
33const { autoUpdater } = require ( 'electron-updater' ) ;
44const isDev = require ( 'electron-is-dev' ) ;
55
66const ConfigUtil = require ( './../renderer/js/utils/config-util.js' ) ;
77
8- function appUpdater ( ) {
8+ function appUpdater ( updateFromMenu = false ) {
99 // Don't initiate auto-updates in development
1010 if ( isDev ) {
1111 return ;
@@ -17,6 +17,8 @@ function appUpdater() {
1717 return ;
1818 }
1919
20+ let updateAvailable = false ;
21+
2022 // Create Logs directory
2123 const LogsDir = `${ app . getPath ( 'userData' ) } /Logs` ;
2224
@@ -30,6 +32,55 @@ function appUpdater() {
3032 // Handle auto updates for beta/pre releases
3133 autoUpdater . allowPrerelease = ConfigUtil . getConfigItem ( 'betaUpdate' ) || false ;
3234
35+ const eventsListenerRemove = [ 'update-available' , 'update-not-available' ] ;
36+ autoUpdater . on ( 'update-available' , info => {
37+ if ( updateFromMenu ) {
38+ dialog . showMessageBox ( {
39+ message : `A new version ${ info . version } , of Zulip Desktop is available` ,
40+ detail : 'The update will be downloaded in the background. You will be notified when it is ready to be installed.'
41+ } ) ;
42+
43+ updateAvailable = true ;
44+
45+ // This is to prevent removal of 'update-downloaded' and 'error' event listener.
46+ eventsListenerRemove . forEach ( event => {
47+ autoUpdater . removeAllListeners ( event ) ;
48+ } ) ;
49+ }
50+ } ) ;
51+
52+ autoUpdater . on ( 'update-not-available' , ( ) => {
53+ if ( updateFromMenu ) {
54+ dialog . showMessageBox ( {
55+ message : 'No updates available' ,
56+ detail : `You are running the latest version of Zulip Desktop.\nVersion: ${ app . getVersion ( ) } `
57+ } ) ;
58+ // Remove all autoUpdator listeners so that next time autoUpdator is manually called these
59+ // listeners don't trigger multiple times.
60+ autoUpdater . removeAllListeners ( ) ;
61+ }
62+ } ) ;
63+
64+ autoUpdater . on ( 'error' , error => {
65+ if ( updateFromMenu ) {
66+ const messageText = ( updateAvailable ) ? ( 'Unable to download the updates' ) : ( 'Unable to check for updates' ) ;
67+ dialog . showMessageBox ( {
68+ type : 'error' ,
69+ buttons : [ 'Manual Download' , 'Cancel' ] ,
70+ message : messageText ,
71+ detail : ( error ) . toString ( ) + `\n\nThe latest version of Zulip Desktop is available at -\nhttps://zulipchat.com/apps/.\n
72+ Current Version: ${ app . getVersion ( ) } `
73+ } , response => {
74+ if ( response === 0 ) {
75+ shell . openExternal ( 'https://zulipchat.com/apps/' ) ;
76+ }
77+ } ) ;
78+ // Remove all autoUpdator listeners so that next time autoUpdator is manually called these
79+ // listeners don't trigger multiple times.
80+ autoUpdater . removeAllListeners ( ) ;
81+ }
82+ } ) ;
83+
3384 // Ask the user if update is available
3485 // eslint-disable-next-line no-unused-vars
3586 autoUpdater . on ( 'update-downloaded' , event => {
0 commit comments