1
1
'use strict' ;
2
- const { app, dialog } = require ( 'electron' ) ;
2
+ const { app, dialog, shell } = require ( 'electron' ) ;
3
3
const { autoUpdater } = require ( 'electron-updater' ) ;
4
4
const isDev = require ( 'electron-is-dev' ) ;
5
5
6
6
const ConfigUtil = require ( './../renderer/js/utils/config-util.js' ) ;
7
7
8
- function appUpdater ( ) {
8
+ function appUpdater ( updateFromMenu = false ) {
9
9
// Don't initiate auto-updates in development
10
10
if ( isDev ) {
11
11
return ;
@@ -17,6 +17,8 @@ function appUpdater() {
17
17
return ;
18
18
}
19
19
20
+ let updateAvailable = false ;
21
+
20
22
// Create Logs directory
21
23
const LogsDir = `${ app . getPath ( 'userData' ) } /Logs` ;
22
24
@@ -30,6 +32,55 @@ function appUpdater() {
30
32
// Handle auto updates for beta/pre releases
31
33
autoUpdater . allowPrerelease = ConfigUtil . getConfigItem ( 'betaUpdate' ) || false ;
32
34
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
+
33
84
// Ask the user if update is available
34
85
// eslint-disable-next-line no-unused-vars
35
86
autoUpdater . on ( 'update-downloaded' , event => {
0 commit comments