|
1 | 1 | const { dialog } = require('electron'); |
2 | 2 | const { autoUpdater } = require('electron-updater'); |
| 3 | +const notifier = require('node-notifier'); |
| 4 | +const path = require('path'); |
| 5 | +const appId = 'com.criptext.criptextmail'; |
3 | 6 |
|
4 | | -const appUpdater = () => { |
5 | | - autoUpdater.checkForUpdatesAndNotify(); |
| 7 | +let currentUpdaterType; |
| 8 | +let isDownloadingUpdate = false; |
| 9 | +const updaterTypes = { |
| 10 | + AUTO: 'auto', |
| 11 | + MANUAL: 'manual', |
| 12 | + NONE: 'none' |
| 13 | +}; |
| 14 | +const iconPath = path.join(__dirname, './../resources/launch-icons/icon.png'); |
| 15 | +autoUpdater.autoDownload = false; |
6 | 16 |
|
7 | | - autoUpdater.on('error', error => { |
8 | | - dialog.showErrorBox( |
9 | | - 'Error: ', |
10 | | - error == null ? 'unknown' : (error.stack || error).toString() |
11 | | - ); |
12 | | - }); |
| 17 | +autoUpdater.on('error', error => { |
| 18 | + const errorMessage = process.env.DEBUG |
| 19 | + ? error == null |
| 20 | + ? 'unknown' |
| 21 | + : (error.stack || error).toString() |
| 22 | + : 'An error occurred while downloading the update. Try again later.'; |
| 23 | + dialog.showErrorBox('Error: ', errorMessage); |
| 24 | + currentUpdaterType = updaterTypes.NONE; |
| 25 | + isDownloadingUpdate = false; |
| 26 | +}); |
13 | 27 |
|
14 | | - autoUpdater.on('update-not-available', () => { |
| 28 | +autoUpdater.on('update-not-available', () => { |
| 29 | + if (currentUpdaterType === updaterTypes.MANUAL) { |
15 | 30 | dialog.showMessageBox({ |
16 | 31 | title: 'No Updates', |
17 | | - message: 'Current version is up-to-date.' |
| 32 | + message: 'Current version is up-to-date.', |
| 33 | + buttons: ['Ok'] |
18 | 34 | }); |
19 | | - }); |
| 35 | + } |
| 36 | + currentUpdaterType = updaterTypes.NONE; |
| 37 | +}); |
20 | 38 |
|
21 | | - autoUpdater.on('update-available', () => { |
| 39 | +autoUpdater.on('update-available', () => { |
| 40 | + if (currentUpdaterType === updaterTypes.MANUAL) { |
22 | 41 | dialog.showMessageBox( |
23 | 42 | { |
24 | 43 | type: 'info', |
25 | | - title: 'Found Updates', |
26 | | - message: 'Found updates, do you want update now?', |
27 | | - buttons: ['Sure', 'No'] |
| 44 | + title: 'Update found', |
| 45 | + message: |
| 46 | + 'A new version of Criptext is available. Do you want download it now?', |
| 47 | + buttons: ['Sure', 'No'], |
| 48 | + noLink: true |
28 | 49 | }, |
29 | 50 | buttonIndex => { |
30 | 51 | if (buttonIndex === 0) { |
31 | 52 | autoUpdater.downloadUpdate(); |
| 53 | + isDownloadingUpdate = true; |
32 | 54 | } |
33 | 55 | } |
34 | 56 | ); |
35 | | - }); |
| 57 | + } |
| 58 | + if (currentUpdaterType === updaterTypes.AUTO) { |
| 59 | + const title = 'A new version of Criptext is available!'; |
| 60 | + const message = 'Click here to download or dismiss to update later'; |
| 61 | + const notifyOptions = { |
| 62 | + appName: appId, |
| 63 | + title, |
| 64 | + message, |
| 65 | + icon: iconPath, |
| 66 | + timeout: 15, |
| 67 | + wait: true |
| 68 | + }; |
| 69 | + notifier.notify(notifyOptions); |
36 | 70 |
|
37 | | - autoUpdater.on('update-downloaded', () => { |
38 | | - dialog.showMessageBox( |
39 | | - { |
40 | | - title: 'Install Updates', |
41 | | - message: 'Updates downloaded, application will be quit for update...' |
42 | | - }, |
43 | | - () => { |
44 | | - setImmediate(() => autoUpdater.quitAndInstall()); |
45 | | - } |
46 | | - ); |
47 | | - }); |
| 71 | + notifier.on('click', () => { |
| 72 | + const downloadingNotifyOptions = { |
| 73 | + appName: appId, |
| 74 | + title: 'Downloading update', |
| 75 | + message: "When it's ready we will notify you", |
| 76 | + icon: iconPath |
| 77 | + }; |
| 78 | + notifier.notify(downloadingNotifyOptions); |
| 79 | + autoUpdater.downloadUpdate(); |
| 80 | + isDownloadingUpdate = true; |
| 81 | + }); |
| 82 | + } |
| 83 | +}); |
| 84 | + |
| 85 | +autoUpdater.on('download-progress', data => { |
| 86 | + const { bytesPerSecond, percent, transferred, total } = data; |
| 87 | + const downloadStatus = `Downloading: ${percent}% - [${transferred}/${total}] - (${bytesPerSecond} b/s)`; |
| 88 | + if (process.env.DEBUG) { |
| 89 | + // To do |
| 90 | + console.log(downloadStatus); |
| 91 | + } |
| 92 | +}); |
| 93 | + |
| 94 | +autoUpdater.on('update-downloaded', () => { |
| 95 | + dialog.showMessageBox( |
| 96 | + { |
| 97 | + title: 'Install Update', |
| 98 | + message: |
| 99 | + 'Update downloaded, application will be quit for install update...', |
| 100 | + buttons: ['Ok'] |
| 101 | + }, |
| 102 | + () => { |
| 103 | + currentUpdaterType = updaterTypes.NONE; |
| 104 | + isDownloadingUpdate = false; |
| 105 | + setImmediate(() => autoUpdater.quitAndInstall()); |
| 106 | + } |
| 107 | + ); |
| 108 | +}); |
| 109 | + |
| 110 | +const appUpdater = () => { |
| 111 | + currentUpdaterType = updaterTypes.AUTO; |
| 112 | + autoUpdater.checkForUpdates(); |
48 | 113 | }; |
49 | 114 |
|
50 | 115 | const checkForUpdates = () => { |
51 | | - autoUpdater.checkForUpdates(); |
| 116 | + if (!isDownloadingUpdate) { |
| 117 | + currentUpdaterType = updaterTypes.MANUAL; |
| 118 | + autoUpdater.checkForUpdates(); |
| 119 | + } else { |
| 120 | + dialog.showMessageBox({ |
| 121 | + title: 'Downloading update', |
| 122 | + message: 'An update is currently being downloaded', |
| 123 | + buttons: ['Ok'] |
| 124 | + }); |
| 125 | + } |
52 | 126 | }; |
53 | 127 |
|
54 | 128 | module.exports = { |
|
0 commit comments