-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathupdater.js
213 lines (179 loc) · 6.1 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
'use strict'
const Sentry = require('@sentry/node')
const { BrowserWindow, Notification, app, shell } = require('electron')
const { autoUpdater } = require('electron-updater')
const { ipcMain } = require('electron/main')
const { ipcMainEvents } = require('./ipc')
const ms = require('ms')
const log = require('electron-log').scope('updater')
const { showDialogSync } = require('./dialog')
const Store = require('electron-store')
// must be global to avoid gc
let updateNotification = null
let checkingManually = false
let readyToUpdate = false
/** @type {string | undefined} */
let nextVersion
const store = new Store({ name: 'updater' })
function quitAndInstall () {
log.info('Restarting Station to install the new version')
beforeQuitCleanup()
store.set('upgradeToVersion', nextVersion)
autoUpdater.quitAndInstall()
}
function beforeQuitCleanup () {
BrowserWindow.getAllWindows().forEach(w => w.removeAllListeners('close'))
app.removeAllListeners('window-all-closed')
}
function setup (/** @type {import('./typings').Context} */ ctx) {
autoUpdater.logger = log
autoUpdater.autoDownload = false // we download manually in 'update-available'
autoUpdater.on('error', onUpdaterError)
autoUpdater.on('update-available', onUpdateAvailable)
autoUpdater.on('update-not-available', onUpdateNotAvailable)
autoUpdater.on('update-downloaded', (event) => onUpdateDownloaded(ctx, event))
// built-in updater != electron-updater
// https://github.com/electron-userland/electron-builder/pull/6395
require('electron')
.autoUpdater
.on('before-quit-for-update', beforeQuitCleanup)
}
module.exports = async function setupUpdater (
/** @type {import('./typings').Context} */ ctx
) {
ctx.getUpdaterStatus = function getUpdaterStatus () {
return { readyToUpdate }
}
ctx.openReleaseNotes = openReleaseNotes
if (['test', 'development'].includes(process.env.NODE_ENV ?? '')) {
ctx.manualCheckForUpdates = () => {
showDialogSync({
title: 'Not available in development',
message: 'Yes, you called this function successfully.',
type: 'info',
buttons: ['Close']
})
}
ctx.restartToUpdate = () => {
showDialogSync({
title: 'Not available in development',
message: 'Yes, you called this function successfully.',
type: 'info',
buttons: ['Close']
})
}
return
}
setup(ctx)
checkForUpdatesInBackground() // async check on startup
setInterval(checkForUpdatesInBackground, ms('12h'))
// enable on-demand check via Tray menu
ctx.manualCheckForUpdates = () => {
checkingManually = true
checkForUpdatesInBackground()
}
ctx.restartToUpdate = () => {
quitAndInstall()
}
}
function checkForUpdatesInBackground () {
ipcMain.emit(ipcMainEvents.UPDATE_CHECK_STARTED)
// TODO: replace this with autoUpdater.checkForUpdatesAndNotify()
autoUpdater.checkForUpdates()
.catch(err => {
log.error('error', err)
})
.finally(() => ipcMain.emit(ipcMainEvents.UPDATE_CHECK_FINISHED))
}
/**
* @param {any} err
*/
function onUpdaterError (err) {
log.error('error', err)
Sentry.captureException(err)
if (!checkingManually) { return }
checkingManually = false
showDialogSync({
title: 'Could not download update',
message: 'It was not possible to download the update. Please check your ' +
'Internet connection and try again.',
type: 'error',
buttons: ['Close']
})
}
/**
* @param {import('electron-updater').UpdateInfo} info
*/
function onUpdateAvailable ({ version /*, releaseNotes */ }) {
nextVersion = version
log.info(`Update to version ${version} is available, downloading..`)
autoUpdater.downloadUpdate().then(
_ => log.info('Update downloaded'),
err => log.error('Cannot download the update.', err)
)
}
function openReleaseNotes () {
const version = nextVersion ? `v${nextVersion}` : 'latest'
shell.openExternal(`https://github.com/filecoin-station/desktop/releases/${version}`)
}
/**
* @param {import('electron-updater').UpdateInfo} info
*/
function onUpdateNotAvailable ({ version }) {
log.info(`update not available from version ${version}`)
if (!checkingManually) { return }
checkingManually = false
showDialogSync({
title: 'Update not available',
message: `You are on the latest version of Filecoin Station (${version}).`,
type: 'info',
buttons: ['Close']
})
}
/**
* @param {import('./typings').Context} ctx
* @param {import('electron-updater').UpdateDownloadedEvent} event
*/
function onUpdateDownloaded (ctx, { version /*, releaseNotes */ }) {
readyToUpdate = true
log.info(`update to ${version} downloaded`)
const showUpdateDialog = () => {
const buttonIx = showDialogSync({
title: 'Update Filecoin Station',
message: `An update to Filecoin Station ${version} is available. ` +
'Would you like to install it now?',
type: 'info',
buttons: ['Later', 'Show Release Notes', 'Install now']
})
if (buttonIx === 1) { // show release notes
openReleaseNotes()
} else if (buttonIx === 2) { // install now
setImmediate(quitAndInstall)
}
}
if (checkingManually) {
// when checking manually, show the dialog immediately
showUpdateDialog()
// also don't trigger the automatic Station restart
// showUpdateDialog() offers the user to restart
} else if (ctx.isShowingUI) {
// show unobtrusive notification + dialog on click
ipcMain.emit(ipcMainEvents.READY_TO_UPDATE)
updateNotification = new Notification({
title: 'Filecoin Station Update',
body: `An update to Filecoin Station ${version} is available.`
})
updateNotification.on('click', showUpdateDialog)
updateNotification.show()
} else if (version !== store.get('upgradeToVersion')) {
// We are running in tray, the user is not interacting with the app
// We have a new version that we did not tried to install previously
// Let's go ahead and restart the app to update
updateNotification = new Notification({
title: 'Restarting Filecoin Station',
body: `Updating to version ${version}.`
})
updateNotification.show()
setImmediate(quitAndInstall)
}
}