electron-builder icon indicating copy to clipboard operation
electron-builder copied to clipboard

How to properly type updater events

Open Stanzilla opened this issue 1 year ago • 3 comments

  • Electron-Builder Version: 24.13.3
  • Node Version: 21
  • Electron Version: 30.0.3
  • Electron Type (current, beta, nightly): current

6.1.8

  • Target: Windows

I'm using Vue and am trying to handle an ipcRenderer event that uses different electron-updater args but am failing to properly type them, currently I'm using this:

type UpdaterEventArg =
  | { status: "error"; error: Error; message?: string }
  | { status: "download-progress"; progressInfo: ProgressInfo }
  | { status: "update-downloaded"; event: UpdateDownloadedEvent }
  | { status: "update-not-available"; updateInfo: UpdateInfo }
  | { status: "checking-for-update" }
  | { status: "update-available"; updateInfo: UpdateInfo };

ipcRenderer.on(
      "updaterHandler",
      (_event, status: string, arg: UpdaterEventArg) => {
        console.log(`updaterHandler: ${status}`);

        if (status === "checking-for-update") {
          // No additional data for this status
          return;
        }

        this.updater.status = status;

        if (status === "download-progress" && "progressInfo" in arg) {
          this.updater.progress = Math.floor(arg.progressInfo.percent);
        }

        if (
          (status === "update-available" ||
            status === "update-not-available" ||
            status === "update-downloaded") &&
          "updateInfo" in arg
        ) {
          this.updater.path = `https://github.com/WeakAuras/WeakAuras-Companion/releases/download/v${arg.updateInfo.version}/${arg.updateInfo.path}`;
          this.updater.version = arg.updateInfo.version;

          // List if `updater.fullChangelog` is set to `true`, `string` otherwise.
          if (typeof arg.updateInfo.releaseNotes === "string") {
            this.updater.releaseNotes = arg.updateInfo.releaseNotes;
          } else if (Array.isArray(arg.updateInfo.releaseNotes)) {
            // Convert the array of ReleaseNoteInfo to a string
            this.updater.releaseNotes = arg.updateInfo.releaseNotes
              .map((note) => note.note)
              .join("\n");
          } else {
            this.updater.releaseNotes = "";
          }

          console.log(JSON.stringify(arg));
        }

        if (status === "error" && "error" in arg) {
          console.error(arg.error);
        }
      },
    );

There are several problems with this:

  1. UpdateInfo is not available on update-available, instead the fields are on arc directly, see this dump:
{
    "tag": "v5.2.4",
    "version": "5.2.4",
    "files": [
        {
            "url": "WeakAuras-Companion-Setup-5.2.4.exe",
            "sha512": "rZTRAK77HoJzDxpvEFc7JgsT4tFoiuU+RymZ7rIsHmz51X/BhtnaAUf59dp272XfteZr6izGNOgD8apH2j1OcA==",
            "size": 95556822
        }
    ],
    "path": "WeakAuras-Companion-Setup-5.2.4.exe",
    "sha512": "rZTRAK77HoJzDxpvEFc7JgsT4tFoiuU+RymZ7rIsHmz51X/BhtnaAUf59dp272XfteZr6izGNOgD8apH2j1OcA==",
    "releaseDate": "2024-05-08T23:25:27.812Z",
    "releaseName": "5.2.4",
    "releaseNotes": "<p>Update wow version dropdown for Cataclysm &amp; TWW<br>\nHousekeeping</p>"
}

But when I try to adjust my code to use arg.version instead of arg.updateInfo.version the types fail. I'm unsure how to fix this type declaration and was hoping for some help from the community. Also the docs confused me here, https://www.electron.build/auto-update.html#event-update-available says there should be updateInfo.

Stanzilla avatar May 11 '24 02:05 Stanzilla

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Jul 11 '24 00:07 github-actions[bot]

not stale

Stanzilla avatar Jul 11 '24 00:07 Stanzilla

I think you could actually import directly the type from electron-updater/out/AppUpdater and pull the event typing directly https://github.com/electron-userland/electron-builder/blob/fa3275c05b334f59453d04551fffa24bfa558e48/packages/electron-updater/src/AppUpdater.ts#L39-L49

Instead of using ipcRenderer.on("updaterHandler", ...), I'm wondering if it'd be easier to use multiple types of events so that you don't need to check specific args. Instead, you would have events like:

("update:update-info", (info: UpdateInfo) => { ... })
("update:download-progress", (info: ProgressInfo) => { ... })
/// etc.

mmaietta avatar Jul 12 '24 01:07 mmaietta

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Sep 17 '24 00:09 github-actions[bot]

This issue was closed because it has been stalled for 30 days with no activity.

github-actions[bot] avatar Oct 18 '24 00:10 github-actions[bot]