-
-
Notifications
You must be signed in to change notification settings - Fork 811
/
Copy pathupdate.js
88 lines (78 loc) · 2.23 KB
/
update.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
import { app, dialog, shell } from 'electron'
import GhReleases from 'electron-gh-releases'
import fetch from 'electron-fetch'
const repo = 'jhen0409/react-native-debugger'
const getFeed = () => fetch(`https://raw.githubusercontent.com/${repo}/master/auto_update.json`).then((res) => res.json())
const showDialog = ({
icon, buttons, message, detail,
}) => dialog.showMessageBoxSync({
type: 'info',
buttons,
title: 'React Native Debugger',
icon,
message,
detail,
})
const notifyUpdateAvailable = ({ icon, detail }) => {
const index = showDialog({
message: 'A newer version is available.',
buttons: ['Download', 'Later'],
icon,
detail,
})
return index === 0
}
const notifyUpdateDownloaded = ({ icon }) => {
const index = showDialog({
message:
'The newer version has been downloaded. '
+ 'Please restart the application to apply the update.',
buttons: ['Restart', 'Later'],
icon,
})
return index === 0
}
let checking = false
export default (icon, notify) => {
if (checking) return
checking = true
const updater = new GhReleases({
repo,
currentVersion: app.getVersion(),
})
updater.check(async (err, status) => {
if (process.platform === 'linux' && err.message === 'This platform is not supported.') {
err = null; // eslint-disable-line
status = true; // eslint-disable-line
}
if (notify && err) {
showDialog({ message: err.message, buttons: ['OK'] })
checking = false
return
}
if (err || !status) {
checking = false
return
}
const feed = await getFeed()
const detail = `${feed.name}\n\n${feed.notes}`
if (notify) {
const open = notifyUpdateAvailable({ icon, detail })
if (open) shell.openExternal('https://github.com/jhen0409/react-native-debugger/releases')
} else if (
process.env.NODE_ENV === 'production'
&& process.platform === 'darwin'
&& notifyUpdateAvailable({ icon, detail })
) {
updater.download()
console.log('[RNDebugger] Update downloading...')
}
checking = false
})
updater.on('update-downloaded', () => {
console.log('[RNDebugger] Update downloaded')
if (notifyUpdateDownloaded({ icon })) {
updater.install()
}
})
}