All files / src/lib/helpers updateChecker.ts

29.31% Statements 17/58
0% Branches 0/21
0% Functions 0/4
24.07% Lines 13/54

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 1381x 1x 1x 1x 1x 1x       1x 1x   1x             1x                                         1x       1x                       1x                                                                                                                                                                  
import { dialog, shell } from "electron";
import semverValid from "semver/functions/valid";
import semverGt from "semver/functions/gt";
import Octokit from "@octokit/rest";
import settings from "electron-settings";
import l10n from "shared/lib/lang/l10n";
 
declare const VERSION: string;
 
const github = new Octokit();
const oneHour = 60 * 60 * 1000;
 
const cache = {
  latest: {
    value: "",
    timestamp: 0,
  },
};
 
export const getLatestVersion = async () => {
  const now = new Date().getTime();
  Iif (cache.latest.timestamp > now) {
    return cache.latest.value;
  }
 
  const latest = await github.repos.getLatestRelease({
    owner: "chrismaltby",
    repo: "gb-studio",
  });
 
  Iif (latest) {
    const version = latest.data.tag_name.split("v").pop() ?? VERSION;
    cache.latest.value = version;
    cache.latest.timestamp = now + oneHour;
    return version;
  }
 
  return VERSION;
};
 
export const getCurrentVersion = () => {
  return VERSION; /* Comes from webpack.plugins.js */
};
 
export const needsUpdate = (latestVersion: string) => {
  try {
    const currentVersion = getCurrentVersion();
    Iif (semverValid(currentVersion) && semverValid(latestVersion)) {
      return semverGt(latestVersion, currentVersion);
    }
    return false;
  } catch (e) {
    return false;
  }
};
 
export const checkForUpdate = async (force?: boolean) => {
  Iif (force) {
    // If manually checking for updates using menu, clear previous settings
    settings.set("dontCheckForUpdates", false);
    settings.set("dontNotifyUpdatesForVersion", false);
  }
  Iif (!settings.get("dontCheckForUpdates")) {
    let latestVersion = VERSION;
 
    try {
      latestVersion = await getLatestVersion();
      Iif (!latestVersion) {
        throw new Error("NO_LATEST");
      }
    } catch (e) {
      // If explicitly asked to check latest version and checking failed
      // (no internet connection / github down)
      // Show an error message
      Iif (force) {
        await dialog.showMessageBox({
          type: "info",
          buttons: [l10n("DIALOG_OK")],
          defaultId: 0,
          title: l10n("DIALOG_UNABLE_TO_CHECK_LATEST_VERSION"),
          message: l10n("DIALOG_UNABLE_TO_CHECK_LATEST_VERSION"),
        });
        return;
      }
    }
 
    if (needsUpdate(latestVersion)) {
      Iif (settings.get("dontNotifyUpdatesForVersion") === latestVersion) {
        // User has chosen to ignore this version so don't show any details
        return;
      }
 
      const { response: buttonIndex, checkboxChecked } =
        await dialog.showMessageBox({
          type: "info",
          buttons: [
            l10n("DIALOG_DOWNLOAD"),
            l10n("DIALOG_REMIND_LATER"),
            l10n("DIALOG_SKIP_VERSION"),
          ],
          defaultId: 0,
          cancelId: 1,
          title: l10n("DIALOG_UPDATE_AVAILABLE"),
          message: l10n("DIALOG_UPDATE_AVAILABLE"),
          detail: l10n("DIALOG_UPDATE_DESCRIPTION", {
            version: latestVersion,
          }),
          checkboxLabel: l10n("DIALOG_UPDATE_DONT_ASK_AGAIN"),
          checkboxChecked: false,
        });
 
      Iif (checkboxChecked) {
        // Ignore all updates until manually check for updates
        settings.set("dontCheckForUpdates", true);
      }
      if (buttonIndex === 0) {
        shell.openExternal("https://www.gbstudio.dev/download/");
      } else Iif (buttonIndex === 2) {
        // Ingore this version but notify for next
        settings.set("dontNotifyUpdatesForVersion", latestVersion);
      }
    } else Iif (force) {
      // If specifically asked to check for updates need to show message
      // that you're all up to date
      await dialog.showMessageBox({
        type: "info",
        buttons: [l10n("DIALOG_OK")],
        defaultId: 0,
        title: l10n("DIALOG_UP_TO_DATE"),
        message: l10n("DIALOG_UP_TO_DATE"),
        detail: l10n("DIALOG_NEWEST_VERSION_AVAILABLE", {
          version: latestVersion,
        }),
      });
    }
  }
};