|
1 | 1 | import { app, dialog, shell } from 'electron';
|
2 | 2 | import GhReleases from 'electron-gh-releases';
|
| 3 | +import fetch from 'electron-fetch'; |
| 4 | + |
| 5 | +const repo = 'jhen0409/react-native-debugger'; |
| 6 | + |
| 7 | +const getFeed = () => |
| 8 | + fetch(`https://raw.githubusercontent.com/${repo}/master/auto_updater.json`).then(res => |
| 9 | + res.json() |
| 10 | + ); |
| 11 | + |
| 12 | +const showDialog = ({ icon, buttons, message, detail }) => |
| 13 | + dialog.showMessageBox({ |
| 14 | + type: 'info', |
| 15 | + buttons, |
| 16 | + title: 'React Native Debugger', |
| 17 | + icon, |
| 18 | + message, |
| 19 | + detail, |
| 20 | + }); |
| 21 | + |
| 22 | +const notifyUpdateAvailable = ({ icon, detail }) => { |
| 23 | + const index = showDialog({ |
| 24 | + message: 'A newer version is available.', |
| 25 | + buttons: ['Download', 'Later'], |
| 26 | + icon, |
| 27 | + detail, |
| 28 | + }); |
| 29 | + return index === 0; |
| 30 | +}; |
| 31 | + |
| 32 | +const notifyUpdateDownloaded = ({ icon }) => { |
| 33 | + const index = showDialog({ |
| 34 | + message: |
| 35 | + 'The newer version has been downloaded. ' + |
| 36 | + 'Please restart the application to apply the update.', |
| 37 | + buttons: ['Restart', 'Later'], |
| 38 | + icon, |
| 39 | + }); |
| 40 | + return index === 0; |
| 41 | +}; |
3 | 42 |
|
4 | 43 | let checking = false;
|
5 | 44 |
|
6 | 45 | export default (icon, notify) => {
|
7 |
| - if (checking) { |
8 |
| - console.log('[Updater] Already checking...'); |
9 |
| - return; |
10 |
| - } |
| 46 | + if (checking) return; |
11 | 47 |
|
12 | 48 | checking = true;
|
13 | 49 | const updater = new GhReleases({
|
14 |
| - repo: 'jhen0409/react-native-debugger', |
| 50 | + repo, |
15 | 51 | currentVersion: app.getVersion(),
|
16 | 52 | });
|
17 | 53 |
|
18 |
| - updater.check((err, status) => { |
| 54 | + updater.check(async (err, status) => { |
19 | 55 | if (process.platform === 'linux' && err.message === 'This platform is not supported.') {
|
20 | 56 | err = null; // eslint-disable-line
|
21 | 57 | status = true; // eslint-disable-line
|
22 | 58 | }
|
23 | 59 | if (notify && err) {
|
24 |
| - dialog.showMessageBox({ |
25 |
| - type: 'info', |
26 |
| - buttons: ['OK'], |
27 |
| - title: 'React Native Debugger', |
28 |
| - icon, |
29 |
| - message: err.message, |
30 |
| - }); |
31 |
| - console.log('[Updater]', err.message); |
| 60 | + showDialog({ message: err.message, buttons: ['OK'] }); |
32 | 61 | checking = false;
|
33 | 62 | return;
|
34 | 63 | }
|
35 | 64 | if (err || !status) {
|
36 |
| - console.log('[Updater] Error', err && err.message, status); |
37 | 65 | checking = false;
|
38 | 66 | return;
|
39 | 67 | }
|
| 68 | + const feed = await getFeed(); |
| 69 | + const detail = `${feed.name}\n\n${feed.notes}`; |
40 | 70 | if (notify) {
|
41 |
| - const index = dialog.showMessageBox({ |
42 |
| - type: 'info', |
43 |
| - buttons: ['Download', 'Later'], |
44 |
| - title: 'React Native Debugger', |
45 |
| - icon, |
46 |
| - message: 'A newer version is available.', |
47 |
| - }); |
48 |
| - checking = false; |
49 |
| - if (index === 1) return; |
50 |
| - shell.openExternal('https://github.com/jhen0409/react-native-debugger/releases'); |
51 |
| - console.log('[Updater] Open external link.'); |
52 |
| - return; |
53 |
| - } |
54 |
| - if (process.env.NODE_ENV === 'production' && process.platform === 'darwin') { |
| 71 | + const open = notifyUpdateAvailable({ icon, detail }); |
| 72 | + if (open) shell.openExternal('https://github.com/jhen0409/react-native-debugger/releases'); |
| 73 | + } else if ( |
| 74 | + process.env.NODE_ENV === 'production' && |
| 75 | + process.platform === 'darwin' && |
| 76 | + notifyUpdateAvailable({ icon, detail }) |
| 77 | + ) { |
55 | 78 | updater.download();
|
56 |
| - console.log('[Updater] Starting download...'); |
57 | 79 | }
|
| 80 | + checking = false; |
58 | 81 | });
|
59 |
| - console.log('[Updater] Checking updates...'); |
60 | 82 |
|
61 |
| - updater.on('update-downloaded', ([, releaseNotes, releaseName]) => { |
62 |
| - console.log('[Updater] Downloaded.'); |
63 |
| - const index = dialog.showMessageBox({ |
64 |
| - type: 'info', |
65 |
| - buttons: ['Restart', 'Later'], |
66 |
| - title: 'React Native Debugger', |
67 |
| - icon, |
68 |
| - message: 'The newer version has been downloaded. ' + |
69 |
| - 'Please restart the application to apply the update.', |
70 |
| - detail: `${releaseName}\n\n${releaseNotes}`, |
71 |
| - }); |
72 |
| - checking = false; |
73 |
| - if (index === 1) return; |
74 |
| - updater.install(); |
| 83 | + updater.on('update-downloaded', () => { |
| 84 | + if (notifyUpdateDownloaded({ icon })) { |
| 85 | + updater.install(); |
| 86 | + } |
75 | 87 | });
|
76 | 88 | };
|
0 commit comments