-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdater.js
88 lines (70 loc) · 2.32 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
// Modules
const {dialog, BrowserWindow, ipcMain} = require('electron')
const {autoUpdater} = require('electron-updater')
// // Enable logging
// autoUpdater.logger = require('electron-log')
// autoUpdater.logger.transports.file.level = 'info'
// Disable auto downloading
autoUpdater.autoDownload = false
// Check for updates
exports.check = () => {
// Start update check
autoUpdater.checkForUpdates()
// Listen for download (update) found
autoUpdater.on('update-available', () => {
// Track progress percent
let downloadProgress = 0
// Prompt user to update
dialog.showMessageBox({
type: 'info',
title: 'Update Available',
message: 'A new version of Readit is available. Do you want to update now?',
buttons: ['Update', 'No']
}, (buttonIndex) => {
// If not 'Update' button, return
if(buttonIndex !== 0) return
// Else start download and show download progress in new window
autoUpdater.downloadUpdate()
// Create progress window
let progressWin = new BrowserWindow({
width: 350,
height: 35,
useContentSize: true,
autoHideMenuBar: true,
maximizable: false,
fullscreen: false,
fullscreenable: false,
resizable: false
})
// Load progress HTML
progressWin.loadURL(`file://${__dirname}/renderer/progress.html`)
// Handle win close
progressWin.on('closed', () => {
progressWin = null
})
// Listen for preogress request from progressWin
ipcMain.on('download-progress-request', (e) => {
e.returnValue = downloadProgress
})
// Track download progress on autoUpdater
autoUpdater.on('download-progress', (d) => {
downloadProgress = d.percent
})
// Listen for completed update download
autoUpdater.on('update-downloaded', () => {
// Close progressWin
if(progressWin) progressWin.close()
// Prompt user to quit and install update
dialog.showMessageBox({
type: 'info',
title: 'Update Ready',
message: 'A new version of Readit is ready. Quit and install now?',
buttons: ['Yes', 'Later']
}, (buttonIndex) => {
// Update if 'Yes'
if(buttonIndex === 0) autoUpdater.quitAndInstall()
})
})
})
})
}