This repository has been archived by the owner on Nov 17, 2023. It is now read-only.
/
updater.js
106 lines (95 loc) · 2.71 KB
/
updater.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
97
98
99
100
101
102
103
104
105
106
import { dialog, ipcMain } from 'electron'
import { autoUpdater } from 'electron-updater'
import isDev from 'electron-is-dev'
import config from 'config'
import delay from '@zap/utils/delay'
import createScheduler from '@zap/utils/scheduler'
import { updaterLog } from '@zap/utils/log'
autoUpdater.logger = updaterLog
/**
* @class ZapUpdater
*
* The ZapUpdater class manages the electron auto update process.
*/
class ZapUpdater {
constructor(mainWindow, options = {}) {
this.mainWindow = mainWindow
this.isActive = false
this.scheduler = createScheduler()
this.settings = {}
this.configure({ ...config.autoupdate, ...options })
autoUpdater.on('update-downloaded', () => {
const opt = {
type: 'question',
buttons: ['Install', 'Later'],
title: 'Update available',
message: 'An update is available. Restart the app and install?',
}
dialog.showMessageBox(this.mainWindow, opt, async choice => {
if (choice !== 0) {
return
}
await delay(100)
autoUpdater.quitAndInstall()
})
})
// Enable updates if needed.
if (this.settings.active && !isDev) {
this.enableAutoUpdates()
} else {
updaterLog.info('Automatic updates not enabled')
}
ipcMain.on('configureAutoUpdater', (event, settings) => {
this.configure(settings)
this.settings.active ? this.enableAutoUpdates() : this.disableAutoUpdates()
})
}
/**
* configure - Configure the auto updater.
*
* @param {object} settings Settings
*/
configure(settings) {
Object.assign(this.settings, settings)
autoUpdater.channel = this.settings.channel
autoUpdater.allowDowngrade = false
}
/**
* enableAutoUpdates - Enable automatic updates.
*/
enableAutoUpdates() {
if (this.isActive || isDev) {
return
}
try {
updaterLog.info('Automatic updates enabled')
autoUpdater.checkForUpdates()
this.scheduler.addTask({
taskId: 'checkForUpdates',
task: () => autoUpdater.checkForUpdates(),
baseDelay: this.settings.interval,
})
this.isActive = true
} catch (error) {
updaterLog.warn('There was a problem enabling auto updates: %s', error.message)
this.isActive = false
this.scheduler.removeAllTasks()
}
}
/**
* disableAutoUpdates - Disable automatic updates.
*/
disableAutoUpdates() {
if (!this.isActive || isDev) {
return
}
try {
this.scheduler.removeAllTasks()
this.isActive = false
updaterLog.info('Automatic updates disabled')
} catch (error) {
updaterLog.warn('There was a problem disabling auto updates: %s', error.message)
}
}
}
export default ZapUpdater