summaryrefslogtreecommitdiff
path: root/src/main/manaplus/manaApp/shared.ts
blob: 106902cec5a1325d7bcf24debcdb97ba6b0fcbb3 (plain) (blame)
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
import { getRequest } from "../../util/webrequest";
import { ManaPlusApp } from "./manaApp.interface";

export async function getVersions(): Promise<{
  [platform_name: string]: {
    version: string;
    file: string;
  };
}> {
  return await getRequest(
    "https://tmw2.org/manalauncher/versions.json?" + Date.now()
  );
}

export async function getVersionInfoForPlatform(
  platformKey: string
): Promise<{
  version: string;
  file: string;
}> {
  const versions = await getVersions();
  if (versions[platformKey]) {
    return versions[platformKey];
  } else {
    throw new Error("Platform not found");
  }
}

export function getInstalledManaplusVersion(
  startCommand: string,
  versionRegEx: RegExp
): Promise<string> {
  return new Promise((res, rej) => {
    let output: string;
    const child = require("child_process").execFile(
      startCommand,
      ["-v"],
      function (err: Error, data: any) {
        output = data.toString();
      }
    );
    child.on("close", () => {
      output = output.replace(versionRegEx, "$1");
      res(output);
    });
    child.on("error", () => {
      rej(new Error("Version check failed"));
    });
  });
}

export async function updateAvailable(
  platformKey: string,
  instance: ManaPlusApp
): Promise<{
  isNewVersion: boolean;
  newestVersion: string;
}> {
  try {
    let newestVersion = await getVersionInfoForPlatform(platformKey);
    let currect_version = (await instance.isInstalled())
      ? await instance.getVersion()
      : "-";
    return {
      isNewVersion: currect_version.indexOf(newestVersion.version) === -1,
      newestVersion: newestVersion.version,
    };
  } catch (e) {
    throw e;
  }
}