-
Notifications
You must be signed in to change notification settings - Fork 904
Expand file tree
/
Copy pathindex.js
More file actions
208 lines (183 loc) · 6.31 KB
/
index.js
File metadata and controls
208 lines (183 loc) · 6.31 KB
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
const { shell, app, BrowserWindow, Notification } = require('electron')
const { ipcMain } = require('electron')
const { autoUpdater } = require('electron-updater')
const i18n = require('i18next')
const CONFIG_KEYS = require('../common/config-keys')
const { IS_MAC, IS_WIN, IS_APPIMAGE } = require('../common/consts')
const ipcMainEvents = require('../common/ipc-main-events')
const logger = require('../common/logger')
const store = require('../common/store')
const getCtx = require('../context')
const { showDialog } = require('../dialogs')
function isAutoUpdateSupported () {
if (store.get(CONFIG_KEYS.DISABLE_AUTO_UPDATE, false)) {
logger.info('[updater] auto update explicitly disabled, not checking for updates automatically')
return false
}
// atm only macOS, windows and AppImage builds support autoupdate mechanism,
// everything else needs to be updated manually or via a third-party package manager
return IS_MAC || IS_WIN || IS_APPIMAGE
}
let updateNotification = null // must be a global to avoid gc
let feedback = false
function setup () {
const ctx = getCtx()
// we download manually in 'update-available'
autoUpdater.autoDownload = false
autoUpdater.autoInstallOnAppQuit = true
autoUpdater.logger = logger
autoUpdater.on('error', err => {
logger.error(`[updater] ${err.toString()}`)
if (!feedback) {
return
}
feedback = false
showDialog({
title: i18n.t('updateErrorDialog.title'),
message: i18n.t('updateErrorDialog.message'),
type: 'error',
buttons: [
i18n.t('close')
]
})
})
autoUpdater.on('update-available', async ({ version, releaseNotes }) => {
logger.info(`[updater] update to ${version} available, download will start`)
try {
await autoUpdater.downloadUpdate()
} catch (err) {
logger.error(`[updater] ${String(err)}`)
}
if (!feedback) {
return
}
// do not toggle feedback off here so we can show a dialog once the download
// is finished.
const opt = showDialog({
title: i18n.t('updateAvailableDialog.title'),
message: i18n.t('updateAvailableDialog.message', { version, releaseNotes }),
type: 'info',
buttons: [
i18n.t('close'),
i18n.t('readReleaseNotes')
]
})
if (opt === 1) {
shell.openExternal(`https://github.com/ipfs-shipyard/ipfs-desktop/releases/v${version}`)
}
})
autoUpdater.on('update-not-available', ({ version }) => {
logger.info('[updater] update not available')
if (!feedback) {
return
}
feedback = false
showDialog({
title: i18n.t('updateNotAvailableDialog.title'),
message: i18n.t('updateNotAvailableDialog.message', { version }),
type: 'info',
buttons: [
i18n.t('close')
]
})
})
let progressPercentTimeout = null
autoUpdater.on('download-progress', ({ percent, bytesPerSecond }) => {
const logDownloadProgress = () => {
logger.info(`[updater] download progress is ${percent.toFixed(2)}% at ${bytesPerSecond} bps.`)
}
// log the percent, but not too often to avoid spamming the logs, but we should
// be sure we're logging at what percent any hiccup is occurring.
clearTimeout(progressPercentTimeout)
if (percent === 100) {
logDownloadProgress()
return
}
progressPercentTimeout = setTimeout(logDownloadProgress, 2000)
})
autoUpdater.on('update-downloaded', ({ version }) => {
logger.info(`[updater] update to ${version} downloaded`)
const feedbackDialog = () => {
const opt = showDialog({
title: i18n.t('updateDownloadedDialog.title'),
message: i18n.t('updateDownloadedDialog.message', { version }),
type: 'info',
buttons: [
i18n.t('updateDownloadedDialog.later'),
i18n.t('updateDownloadedDialog.now')
]
})
if (opt === 1) { // now
setImmediate(async () => {
await beforeQuitCleanup() // just to be sure (we had regressions before)
autoUpdater.quitAndInstall()
})
}
}
if (feedback) {
feedback = false
// when in instant feedback mode, show dialog immediately
feedbackDialog()
} else {
// show unobtrusive notification + dialog on click
updateNotification = new Notification({
title: i18n.t('updateDownloadedNotification.title'),
body: i18n.t('updateDownloadedNotification.message', { version })
})
updateNotification.on('click', feedbackDialog)
updateNotification.show()
}
})
const stopIpfs = ctx.getFn('stopIpfs')
// In some cases before-quit event is not emitted before all windows are closed,
// and we need to do cleanup here
const beforeQuitCleanup = async () => {
BrowserWindow.getAllWindows().forEach(w => w.removeAllListeners('close'))
app.removeAllListeners('window-all-closed')
try {
const s = await stopIpfs()
logger.info(`[beforeQuitCleanup] stopIpfs had finished with status: ${s}`)
} catch (err) {
logger.error('[beforeQuitCleanup] stopIpfs had an error', err)
}
}
// built-in updater != electron-updater
// Added in https://github.com/electron-userland/electron-builder/pull/6395
require('electron').autoUpdater.on('before-quit-for-update', beforeQuitCleanup)
}
async function checkForUpdates () {
logger.info('[updater] checking for updates')
ipcMain.emit(ipcMainEvents.UPDATING)
try {
await autoUpdater.checkForUpdates()
} catch (_) {
// Ignore. The errors are already handled on 'error' event.
}
ipcMain.emit(ipcMainEvents.UPDATING_ENDED)
}
module.exports = async function () {
if (['test', 'development'].includes(process.env.NODE_ENV ?? '')) {
getCtx().setProp('manualCheckForUpdates', () => {
showDialog({
title: 'Not available in development',
message: 'Yes, you called this function successfully.',
buttons: [i18n.t('close')]
})
})
return
}
if (!isAutoUpdateSupported()) {
getCtx().setProp('manualCheckForUpdates', () => {
shell.openExternal('https://github.com/ipfs/ipfs-desktop/releases/latest')
})
return
}
setup()
checkForUpdates() // background check
setInterval(checkForUpdates, 43200000) // every 12 hours
// enable on-demand check via About submenu
getCtx().setProp('manualCheckForUpdates', () => {
feedback = true
checkForUpdates()
})
}