|
1 | 1 | import { dialog } from "electron";
|
2 | 2 | import isDev from "electron-is-dev";
|
3 | 3 | import { autoUpdater } from "electron-updater";
|
4 |
| -import ProgressBar from "electron-progressbar"; |
5 | 4 |
|
6 |
| -let progressBar = null; |
7 | 5 | let isUpdating = false;
|
8 |
| -let downloadAndInstall = false; |
9 | 6 |
|
10 |
| -function checkForUpdate(onQuitAndInstall) { |
| 7 | +async function checkForUpdate(getMainWindow, onQuitAndInstall) { |
11 | 8 | // Disable for development
|
12 | 9 | if (isDev) {
|
13 | 10 | return;
|
14 | 11 | }
|
15 | 12 |
|
16 |
| - autoUpdater.logger = console; |
17 |
| - autoUpdater.autoDownload = false; |
18 |
| - |
19 |
| - autoUpdater.on("error", err => { |
20 |
| - if (isUpdating) { |
21 |
| - dialog.showErrorBox("Update Error: ", err == null ? "unknown" : err.message); |
22 |
| - isUpdating = false; |
23 |
| - console.error("Error in auto-updater.", err.message); |
24 |
| - } |
25 |
| - }); |
26 |
| - |
27 |
| - autoUpdater.on("update-available", info => { |
28 |
| - console.log(`Update available: ${info.version}`); |
| 13 | + if (isUpdating) { |
| 14 | + return; |
| 15 | + } |
29 | 16 |
|
30 |
| - const message = `Update ${info.version} found. Do you want to download the update?`; |
31 |
| - const detail = `View the release notes at: https://github.com/loki-project/loki-electron-gui-wallet/releases/tag/v${info.version}`; |
| 17 | + autoUpdater.logger = console; |
32 | 18 |
|
33 |
| - dialog.showMessageBox( |
34 |
| - { |
35 |
| - type: "info", |
36 |
| - title: "Update available", |
37 |
| - message, |
38 |
| - detail, |
39 |
| - buttons: ["Download and Install", "Download and Install Later", "No"], |
40 |
| - defaultId: 0 |
41 |
| - }, |
42 |
| - buttonIndex => { |
43 |
| - // Download and install |
44 |
| - if (buttonIndex === 0) { |
45 |
| - downloadAndInstall = true; |
46 |
| - if (!progressBar) { |
47 |
| - progressBar = new ProgressBar({ |
48 |
| - indeterminate: false, |
49 |
| - title: "Downloading...", |
50 |
| - text: `Downloading wallet v${info.version}` |
51 |
| - }); |
52 |
| - } |
53 |
| - } |
| 19 | + try { |
| 20 | + // Get the update using electron-updater |
| 21 | + try { |
| 22 | + const info = await autoUpdater.checkForUpdates(); |
| 23 | + if (!info.downloadPromise) { |
| 24 | + console.info("auto-update: no update to download"); |
54 | 25 |
|
55 |
| - // Download |
56 |
| - if (buttonIndex !== 2) { |
57 |
| - isUpdating = true; |
58 |
| - autoUpdater.downloadUpdate(); |
59 |
| - } |
| 26 | + return; |
60 | 27 | }
|
61 |
| - ); |
62 |
| - }); |
63 |
| - |
64 |
| - autoUpdater.on("download-progress", progress => { |
65 |
| - if (progressBar) { |
66 |
| - progressBar.value = progress.percent; |
| 28 | + await info.downloadPromise; |
| 29 | + } catch (error) { |
| 30 | + await showCannotUpdateDialog(getMainWindow()); |
| 31 | + throw error; |
67 | 32 | }
|
68 |
| - }); |
69 | 33 |
|
70 |
| - autoUpdater.on("update-downloaded", () => { |
71 |
| - console.log("Update downloaded"); |
72 |
| - isUpdating = false; |
73 |
| - |
74 |
| - if (progressBar) { |
75 |
| - progressBar.setCompleted(); |
76 |
| - progressBar = null; |
| 34 | + // Update downloaded successfully, we should ask the user to update |
| 35 | + console.info("auto-update: showing update dialog..."); |
| 36 | + const shouldUpdate = await showUpdateDialog(getMainWindow()); |
| 37 | + if (!shouldUpdate) { |
| 38 | + return; |
77 | 39 | }
|
78 | 40 |
|
79 |
| - // If download and install was selected then quit and install |
80 |
| - if (downloadAndInstall && onQuitAndInstall) { |
| 41 | + console.info("auto-update: calling quitAndInstall..."); |
| 42 | + if (onQuitAndInstall) { |
81 | 43 | onQuitAndInstall(autoUpdater);
|
82 |
| - downloadAndInstall = false; |
83 | 44 | }
|
| 45 | + } catch (error) { |
| 46 | + console.error("auto-update error:", getPrintableError(error)); |
| 47 | + } finally { |
| 48 | + isUpdating = false; |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +function getPrintableError(error) { |
| 53 | + return error && error.stack ? error.stack : error; |
| 54 | +} |
| 55 | + |
| 56 | +async function showUpdateDialog(mainWindow) { |
| 57 | + const RESTART_BUTTON = 0; |
| 58 | + const LATER_BUTTON = 1; |
| 59 | + const options = { |
| 60 | + type: "info", |
| 61 | + buttons: ["Restart Wallet", "Later"], |
| 62 | + title: "Loki Electron Wallet update available", |
| 63 | + message: "There is a new version of Loki Electron Wallet available.", |
| 64 | + detail: "Press Restart Wallet to apply the update", |
| 65 | + defaultId: LATER_BUTTON, |
| 66 | + cancelId: RESTART_BUTTON |
| 67 | + }; |
| 68 | + return new Promise(resolve => { |
| 69 | + dialog.showMessageBox(mainWindow, options, response => { |
| 70 | + resolve(response === RESTART_BUTTON); |
| 71 | + }); |
84 | 72 | });
|
| 73 | +} |
85 | 74 |
|
86 |
| - autoUpdater.checkForUpdates(); |
| 75 | +async function showCannotUpdateDialog(mainWindow) { |
| 76 | + const options = { |
| 77 | + type: "error", |
| 78 | + buttons: ["Ok"], |
| 79 | + title: "Cannot update", |
| 80 | + message: |
| 81 | + "Loki Electron Wallet failed to update but there is a new version available. Please go to https://loki.network/ and install the new version manually." |
| 82 | + }; |
| 83 | + |
| 84 | + return new Promise(resolve => { |
| 85 | + dialog.showMessageBox(mainWindow, options, () => { |
| 86 | + resolve(); |
| 87 | + }); |
| 88 | + }); |
87 | 89 | }
|
88 | 90 |
|
89 | 91 | export { checkForUpdate };
|
0 commit comments