diff options
Diffstat (limited to 'src/main/manaplus/manaApp/shared.ts')
-rw-r--r-- | src/main/manaplus/manaApp/shared.ts | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/src/main/manaplus/manaApp/shared.ts b/src/main/manaplus/manaApp/shared.ts new file mode 100644 index 0000000..106902c --- /dev/null +++ b/src/main/manaplus/manaApp/shared.ts @@ -0,0 +1,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; + } +} |