diff options
author | 2021-09-14 11:03:28 +0200 | |
---|---|---|
committer | 2021-09-14 11:03:28 +0200 | |
commit | 087113d8a1214ba4c7df03bfe66747d8d944280c (patch) | |
tree | 4d853a03057138dfa845cd6a7d91ccf63565a1a6 /src/electron/ipc-api/autoUpdate.ts | |
parent | chore: codebase improvements (#1930) (diff) | |
download | ferdium-app-087113d8a1214ba4c7df03bfe66747d8d944280c.tar.gz ferdium-app-087113d8a1214ba4c7df03bfe66747d8d944280c.tar.zst ferdium-app-087113d8a1214ba4c7df03bfe66747d8d944280c.zip |
chore: convert JS to TS (#1934)
Diffstat (limited to 'src/electron/ipc-api/autoUpdate.ts')
-rw-r--r-- | src/electron/ipc-api/autoUpdate.ts | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/src/electron/ipc-api/autoUpdate.ts b/src/electron/ipc-api/autoUpdate.ts new file mode 100644 index 000000000..70890539d --- /dev/null +++ b/src/electron/ipc-api/autoUpdate.ts | |||
@@ -0,0 +1,88 @@ | |||
1 | import { app, ipcMain, BrowserWindow } from 'electron'; | ||
2 | import { autoUpdater } from 'electron-updater'; | ||
3 | import { GITHUB_NIGHTLIES_REPO_NAME, GITHUB_ORG_NAME } from '../../config'; | ||
4 | import { isMac, isWindows } from '../../environment'; | ||
5 | |||
6 | const debug = require('debug')('Ferdi:ipcApi:autoUpdate'); | ||
7 | |||
8 | export default (params: { mainWindow: BrowserWindow; settings: any }) => { | ||
9 | const enableUpdate = Boolean(params.settings.app.get('automaticUpdates')); | ||
10 | |||
11 | if (!enableUpdate) { | ||
12 | autoUpdater.autoInstallOnAppQuit = false; | ||
13 | autoUpdater.autoDownload = false; | ||
14 | } else if (isMac || isWindows || process.env.APPIMAGE) { | ||
15 | ipcMain.on('autoUpdate', (event, args) => { | ||
16 | if (enableUpdate) { | ||
17 | try { | ||
18 | autoUpdater.autoInstallOnAppQuit = false; | ||
19 | autoUpdater.allowPrerelease = Boolean( | ||
20 | params.settings.app.get('beta'), | ||
21 | ); | ||
22 | autoUpdater.channel = autoUpdater.allowPrerelease ? 'beta' : 'latest'; | ||
23 | |||
24 | if (params.settings.app.get('nightly')) { | ||
25 | autoUpdater.allowPrerelease = Boolean( | ||
26 | params.settings.app.get('nightly'), | ||
27 | ); | ||
28 | autoUpdater.channel = 'alpha'; | ||
29 | autoUpdater.setFeedURL({ | ||
30 | provider: 'github', | ||
31 | owner: GITHUB_ORG_NAME, | ||
32 | repo: GITHUB_NIGHTLIES_REPO_NAME, | ||
33 | }); | ||
34 | } | ||
35 | |||
36 | if (args.action === 'check') { | ||
37 | debug('checking for update'); | ||
38 | autoUpdater.checkForUpdates(); | ||
39 | } else if (args.action === 'install') { | ||
40 | debug('installing update'); | ||
41 | autoUpdater.quitAndInstall(); | ||
42 | // we need to send a quit event | ||
43 | setTimeout(() => { | ||
44 | app.quit(); | ||
45 | }, 20); | ||
46 | } | ||
47 | } catch (e) { | ||
48 | console.error(e); | ||
49 | event.sender.send('autoUpdate', { error: true }); | ||
50 | } | ||
51 | } | ||
52 | }); | ||
53 | |||
54 | autoUpdater.on('update-not-available', () => { | ||
55 | debug('update-not-available'); | ||
56 | params.mainWindow.webContents.send('autoUpdate', { available: false }); | ||
57 | }); | ||
58 | |||
59 | autoUpdater.on('update-available', event => { | ||
60 | debug('update-available'); | ||
61 | |||
62 | if (enableUpdate) { | ||
63 | params.mainWindow.webContents.send('autoUpdate', { | ||
64 | version: event.version, | ||
65 | available: true, | ||
66 | }); | ||
67 | } | ||
68 | }); | ||
69 | |||
70 | autoUpdater.on('download-progress', progressObj => { | ||
71 | let logMessage = `Download speed: ${progressObj.bytesPerSecond}`; | ||
72 | logMessage = `${logMessage} - Downloaded ${progressObj.percent}%`; | ||
73 | logMessage = `${logMessage} (${progressObj.transferred}/${progressObj.total})`; | ||
74 | |||
75 | debug(logMessage); | ||
76 | }); | ||
77 | |||
78 | autoUpdater.on('update-downloaded', () => { | ||
79 | debug('update-downloaded'); | ||
80 | params.mainWindow.webContents.send('autoUpdate', { downloaded: true }); | ||
81 | }); | ||
82 | |||
83 | autoUpdater.on('error', () => { | ||
84 | debug('update-error'); | ||
85 | params.mainWindow.webContents.send('autoUpdate', { error: true }); | ||
86 | }); | ||
87 | } | ||
88 | }; | ||