/
check-for-updates.js
96 lines (80 loc) · 2.32 KB
/
check-for-updates.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
const {app, dialog, shell } = require('electron')
const {autoUpdater} = require('electron-updater')
const ProgressBar = require('electron-progressbar')
const log = require('electron-log')
const isDev = require('electron-is-dev')
const downloadAndInstall = () => {
const config = {
text: 'Downloading update',
indeterminate: true,
}
const isNotWindows = (process.platform !== 'win32')
if (isNotWindows) {
config.detail = '0%'
config.indeterminate = true
}
const progressBar = new ProgressBar(config)
autoUpdater
.on('download-progress', ({ percent }) => {
progressBar.detail = `${percent.toFixed(1)}%`
progressBar.value = percent
})
.on('update-downloaded', () => {
progressBar.setCompleted()
autoUpdater.quitAndInstall()
})
autoUpdater.downloadUpdate()
}
const notifyNoUpdate = () => {
dialog.showMessageBox({
message: 'No update is available.',
})
}
const notifyUpdate = (resolve) => (updateInfo) => {
dialog.showMessageBox({
message: `Update ${updateInfo.version} available. (Current version ${app.getVersion()})`,
buttons: [
'See Release Notes',
'Remind Me Later',
'Download and Install Update',
],
cancelId: 1,
defaultId: 2,
}, (data) => {
switch (data) {
case 2:
downloadAndInstall()
break
case 0:
shell.openExternal('https://github.com/AugurProject/augur-app/releases')
resolve()
break
case 1:
resolve()
break
default:
resolve()
}
})
}
// this only needs to be done once.
autoUpdater.logger = log
autoUpdater.autoDownload = false
module.exports = (notifyUpdateNotAvailable = false) => {
if(isDev) return Promise.resolve()
if(process.platform == "linux" && !process.env.APPIMAGE) return Promise.resolve()
const p = new Promise((resolve) => {
autoUpdater
.once('update-not-available', resolve)
.once('update-available', notifyUpdate(resolve))
})
autoUpdater
.removeListener('update-not-available', notifyNoUpdate)
if (notifyUpdateNotAvailable) autoUpdater
.once('update-not-available', notifyNoUpdate)
return autoUpdater.checkForUpdates().catch((e) => {
log.error('There was an error updating app. This is expected if using deb package.')
return Promise.resolve(p);
});
return p;
}