From b043383ce17706975d52acffa89876224bca6598 Mon Sep 17 00:00:00 2001 From: Louis Date: Mon, 2 Oct 2023 16:29:14 +0700 Subject: [PATCH] fix: version diff --- electron/utils/menu.ts | 9 ++++++++- electron/utils/versionDiff.ts | 21 +++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 electron/utils/versionDiff.ts diff --git a/electron/utils/menu.ts b/electron/utils/menu.ts index bfd93eaff..65e009aef 100644 --- a/electron/utils/menu.ts +++ b/electron/utils/menu.ts @@ -2,6 +2,7 @@ const { app, Menu, dialog } = require("electron"); const isMac = process.platform === "darwin"; const { autoUpdater } = require("electron-updater"); +import { compareSemanticVersions } from "./versionDiff"; const template: (Electron.MenuItemConstructorOptions | Electron.MenuItem)[] = [ ...(isMac @@ -14,7 +15,13 @@ const template: (Electron.MenuItemConstructorOptions | Electron.MenuItem)[] = [ label: "Check for Updates...", click: () => autoUpdater.checkForUpdatesAndNotify().then((e) => { - if (!e || e.updateInfo.files.length === 0) + if ( + !e || + compareSemanticVersions( + app.getVersion(), + e.updateInfo.version + ) >= 0 + ) dialog.showMessageBox({ message: `There are currently no updates available.`, }); diff --git a/electron/utils/versionDiff.ts b/electron/utils/versionDiff.ts new file mode 100644 index 000000000..25934e87f --- /dev/null +++ b/electron/utils/versionDiff.ts @@ -0,0 +1,21 @@ +export const compareSemanticVersions = (a: string, b: string) => { + + // 1. Split the strings into their parts. + const a1 = a.split('.'); + const b1 = b.split('.'); + // 2. Contingency in case there's a 4th or 5th version + const len = Math.min(a1.length, b1.length); + // 3. Look through each version number and compare. + for (let i = 0; i < len; i++) { + const a2 = +a1[ i ] || 0; + const b2 = +b1[ i ] || 0; + + if (a2 !== b2) { + return a2 > b2 ? 1 : -1; + } + } + + // 4. We hit this if the all checked versions so far are equal + // + return b1.length - a1.length; +}; \ No newline at end of file