Skip to content

Commit 554dd05

Browse files
authored
Do notify first before autoUpdater.checkForUpdates (#272)
* Do notify before autoUpdater.checkForUpdates * Show confirm message when update downloaded
1 parent cbadd83 commit 554dd05

File tree

3 files changed

+65
-46
lines changed

3 files changed

+65
-46
lines changed

electron/update.js

Lines changed: 57 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,88 @@
11
import { app, dialog, shell } from 'electron';
22
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+
};
342

443
let checking = false;
544

645
export default (icon, notify) => {
7-
if (checking) {
8-
console.log('[Updater] Already checking...');
9-
return;
10-
}
46+
if (checking) return;
1147

1248
checking = true;
1349
const updater = new GhReleases({
14-
repo: 'jhen0409/react-native-debugger',
50+
repo,
1551
currentVersion: app.getVersion(),
1652
});
1753

18-
updater.check((err, status) => {
54+
updater.check(async (err, status) => {
1955
if (process.platform === 'linux' && err.message === 'This platform is not supported.') {
2056
err = null; // eslint-disable-line
2157
status = true; // eslint-disable-line
2258
}
2359
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'] });
3261
checking = false;
3362
return;
3463
}
3564
if (err || !status) {
36-
console.log('[Updater] Error', err && err.message, status);
3765
checking = false;
3866
return;
3967
}
68+
const feed = await getFeed();
69+
const detail = `${feed.name}\n\n${feed.notes}`;
4070
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+
) {
5578
updater.download();
56-
console.log('[Updater] Starting download...');
5779
}
80+
checking = false;
5881
});
59-
console.log('[Updater] Checking updates...');
6082

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+
}
7587
});
7688
};

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
"dependencies": {
7272
"adbkit": "^2.11.0",
7373
"electron-context-menu": "jhen0409/electron-context-menu#async-popup",
74+
"electron-fetch": "^1.2.1",
7475
"electron-gh-releases": "^2.0.4",
7576
"electron-store": "^1.2.0",
7677
"jsan": "^3.1.9",

yarn.lock

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2735,6 +2735,12 @@ electron-download@^4.0.0, electron-download@^4.1.0:
27352735
semver "^5.3.0"
27362736
sumchecker "^2.0.1"
27372737

2738+
electron-fetch@^1.2.1:
2739+
version "1.2.1"
2740+
resolved "https://registry.yarnpkg.com/electron-fetch/-/electron-fetch-1.2.1.tgz#08a033a23cc47febf05457f3e0e0a26598d02b95"
2741+
dependencies:
2742+
encoding "^0.1.12"
2743+
27382744
electron-gh-releases@^2.0.4:
27392745
version "2.0.4"
27402746
resolved "https://registry.yarnpkg.com/electron-gh-releases/-/electron-gh-releases-2.0.4.tgz#198c07a0970fb8e80fcc67bd0b4198a010923dc3"
@@ -2856,7 +2862,7 @@ encodeurl@~1.0.2:
28562862
version "1.0.2"
28572863
resolved "https://registry.yarnpkg.com/encodeurl/-/encodeurl-1.0.2.tgz#ad3ff4c86ec2d029322f5a02c3a9a606c95b3f59"
28582864

2859-
encoding@^0.1.11:
2865+
encoding@^0.1.11, encoding@^0.1.12:
28602866
version "0.1.12"
28612867
resolved "https://registry.yarnpkg.com/encoding/-/encoding-0.1.12.tgz#538b66f3ee62cd1ab51ec323829d1f9480c74beb"
28622868
dependencies:

0 commit comments

Comments
 (0)