|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const { app } = require("electron"); |
| 4 | +const { autoUpdater } = require("electron-updater"); |
| 5 | + |
| 6 | +function createUpdateController() { |
| 7 | + const state = { |
| 8 | + supported: app.isPackaged, |
| 9 | + status: app.isPackaged ? "idle" : "unavailable", |
| 10 | + currentVersion: app.getVersion(), |
| 11 | + availableVersion: null, |
| 12 | + downloaded: false, |
| 13 | + progress: null, |
| 14 | + error: app.isPackaged ? null : "Updates are available only in the packaged desktop app.", |
| 15 | + lastCheckedAt: null |
| 16 | + }; |
| 17 | + |
| 18 | + autoUpdater.autoDownload = false; |
| 19 | + autoUpdater.autoInstallOnAppQuit = true; |
| 20 | + |
| 21 | + const merge = (patch) => Object.assign(state, patch); |
| 22 | + |
| 23 | + autoUpdater.on("checking-for-update", () => merge({ |
| 24 | + status: "checking", |
| 25 | + error: null, |
| 26 | + lastCheckedAt: new Date().toISOString() |
| 27 | + })); |
| 28 | + autoUpdater.on("update-available", (info) => merge({ |
| 29 | + status: "available", |
| 30 | + availableVersion: info.version || null, |
| 31 | + downloaded: false, |
| 32 | + progress: null, |
| 33 | + error: null |
| 34 | + })); |
| 35 | + autoUpdater.on("update-not-available", () => merge({ |
| 36 | + status: "current", |
| 37 | + availableVersion: null, |
| 38 | + downloaded: false, |
| 39 | + progress: null, |
| 40 | + error: null |
| 41 | + })); |
| 42 | + autoUpdater.on("download-progress", (progress) => merge({ |
| 43 | + status: "downloading", |
| 44 | + progress: { |
| 45 | + percent: Math.round(progress.percent || 0), |
| 46 | + transferred: progress.transferred || 0, |
| 47 | + total: progress.total || 0, |
| 48 | + bytesPerSecond: progress.bytesPerSecond || 0 |
| 49 | + } |
| 50 | + })); |
| 51 | + autoUpdater.on("update-downloaded", (info) => merge({ |
| 52 | + status: "downloaded", |
| 53 | + availableVersion: info.version || state.availableVersion, |
| 54 | + downloaded: true, |
| 55 | + progress: { percent: 100 } |
| 56 | + })); |
| 57 | + autoUpdater.on("error", (error) => merge({ |
| 58 | + status: "error", |
| 59 | + error: error.message || String(error) |
| 60 | + })); |
| 61 | + |
| 62 | + const ensureSupported = () => { |
| 63 | + if (!state.supported) { |
| 64 | + const error = new Error(state.error); |
| 65 | + error.code = "UPDATES_UNAVAILABLE"; |
| 66 | + throw error; |
| 67 | + } |
| 68 | + }; |
| 69 | + |
| 70 | + return { |
| 71 | + status() { |
| 72 | + return { ...state }; |
| 73 | + }, |
| 74 | + async check() { |
| 75 | + ensureSupported(); |
| 76 | + await autoUpdater.checkForUpdates(); |
| 77 | + return { ...state }; |
| 78 | + }, |
| 79 | + async download() { |
| 80 | + ensureSupported(); |
| 81 | + if (state.status !== "available") { |
| 82 | + return { ...state, error: state.status === "downloaded" ? null : "No update is ready to download." }; |
| 83 | + } |
| 84 | + merge({ status: "downloading", error: null }); |
| 85 | + await autoUpdater.downloadUpdate(); |
| 86 | + return { ...state }; |
| 87 | + }, |
| 88 | + install() { |
| 89 | + ensureSupported(); |
| 90 | + if (!state.downloaded) { |
| 91 | + const error = new Error("No downloaded update is ready to install."); |
| 92 | + error.code = "UPDATE_NOT_DOWNLOADED"; |
| 93 | + throw error; |
| 94 | + } |
| 95 | + merge({ status: "installing" }); |
| 96 | + setImmediate(() => autoUpdater.quitAndInstall(false, true)); |
| 97 | + return { ...state }; |
| 98 | + } |
| 99 | + }; |
| 100 | +} |
| 101 | + |
| 102 | +module.exports = { createUpdateController }; |
0 commit comments