diff options
author | LawnCable <git@lawncable.net> | 2021-02-28 23:59:30 +0100 |
---|---|---|
committer | LawnCable <git@lawncable.net> | 2021-02-28 23:59:30 +0100 |
commit | f781beecbc13ad5166dddfc65cbabda68dd1e219 (patch) | |
tree | 4f9c125e08df632241f25bdbe58c7b0e4c7bc88e | |
parent | 476adc1800c5581d0ba5d62f15127614ee6df24f (diff) | |
download | electron-f781beecbc13ad5166dddfc65cbabda68dd1e219.tar.gz electron-f781beecbc13ad5166dddfc65cbabda68dd1e219.tar.bz2 electron-f781beecbc13ad5166dddfc65cbabda68dd1e219.tar.xz electron-f781beecbc13ad5166dddfc65cbabda68dd1e219.zip |
deduplicate some shared stuff in manaplus controll
code (platform specific implementations)
-rw-r--r-- | src/main/manaplus/manaApp/linux.ts | 44 | ||||
-rw-r--r-- | src/main/manaplus/manaApp/shared.ts | 71 | ||||
-rw-r--r-- | src/main/manaplus/manaApp/windows.ts | 45 |
3 files changed, 87 insertions, 73 deletions
diff --git a/src/main/manaplus/manaApp/linux.ts b/src/main/manaplus/manaApp/linux.ts index da37de0..1a5d828 100644 --- a/src/main/manaplus/manaApp/linux.ts +++ b/src/main/manaplus/manaApp/linux.ts @@ -1,10 +1,14 @@ import { ManaPlusApp } from "./manaApp.interface"; import { app } from "electron"; import * as fs from "fs-extra"; -import { getRequest } from "../../util/webrequest"; import { Status } from "../../status"; import { download, Progress as ProgressType } from "../../util/downloader"; import { promisify } from "util"; +import { + getInstalledManaplusVersion, + getVersionInfoForPlatform, + updateAvailable, +} from "./shared"; export class ManaPlusAppLinux implements ManaPlusApp { private path: string; @@ -22,23 +26,7 @@ export class ManaPlusAppLinux implements ManaPlusApp { throw new Error("getGameDir() is windows only!"); } getVersion(): Promise<string> { - return new Promise((res, rej) => { - let output: string; - const child = require("child_process").execFile( - this.startCommand, - ["-v"], - function (err: Error, data: any) { - output = data.toString(); - } - ); - child.on("close", () => { - output = output.replace(this.versionRegEx, "$1"); - res(output); - }); - child.on("error", () => { - rej(new Error("Version check failed")); - }); - }); + return getInstalledManaplusVersion(this.startCommand, this.versionRegEx); } isInstalled(): boolean { return fs.existsSync(this.path + "/Mana.AppImage"); @@ -47,20 +35,7 @@ export class ManaPlusAppLinux implements ManaPlusApp { isNewVersion: boolean; newestVersion: string; }> { - try { - let versions = await getRequest( - "https://tmw2.org/manalauncher/versions.json?" + Date.now() - ); - let currect_version = (await this.isInstalled) - ? await this.getVersion() - : "-"; - return { - isNewVersion: currect_version.indexOf(versions.AppImage.version) === -1, - newestVersion: versions.AppImage.version, - }; - } catch (e) { - throw e; - } + return updateAvailable("AppImage", this); } async update(): Promise<any> { fs.existsSync(app.getPath("userData") + "/temp") || @@ -71,10 +46,7 @@ export class ManaPlusAppLinux implements ManaPlusApp { Status.setActivity("Fetching Download URL"); let downloadURL; try { - let versions = await getRequest( - "https://tmw2.org/manalauncher/versions.json?" + Date.now() - ); - downloadURL = versions.AppImage.file; + downloadURL = (await getVersionInfoForPlatform("AppImage")).file; } catch (e) { console.log(e); Status.showError( 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; + } +} diff --git a/src/main/manaplus/manaApp/windows.ts b/src/main/manaplus/manaApp/windows.ts index ed9e70c..7bf5adb 100644 --- a/src/main/manaplus/manaApp/windows.ts +++ b/src/main/manaplus/manaApp/windows.ts @@ -4,7 +4,11 @@ import * as fs from "fs-extra"; import { Status } from "../../status"; import { download, Progress as ProgressType } from "../../util/downloader"; import * as extract from "extract-zip"; -import { getRequest } from "../../util/webrequest"; +import { + getInstalledManaplusVersion, + getVersionInfoForPlatform, + updateAvailable, +} from "./shared"; export class ManaPlusAppWindows implements ManaPlusApp { private path: string; @@ -23,23 +27,7 @@ export class ManaPlusAppWindows implements ManaPlusApp { return this.path + "\\Mana\\"; } getVersion(): Promise<string> { - return new Promise((res, rej) => { - let output: string; - const child = require("child_process").execFile( - this.startCommand, - ["-v"], - function (err: Error, data: any) { - output = data.toString(); - } - ); - child.on("close", () => { - output = output.replace(this.versionRegEx, "$1"); - res(output); - }); - child.on("error", () => { - rej(new Error("Version check failed")); - }); - }); + return getInstalledManaplusVersion(this.startCommand, this.versionRegEx); } isInstalled(): boolean { return fs.existsSync(this.path + "\\Mana\\manaplus.exe"); @@ -53,10 +41,7 @@ export class ManaPlusAppWindows implements ManaPlusApp { Status.setActivity("Fetching Download URL"); let downloadURL; try { - let versions = await getRequest( - "https://tmw2.org/manalauncher/versions.json?" + Date.now() - ); - downloadURL = versions.windows64.file; + downloadURL = (await getVersionInfoForPlatform("windows64")).file; } catch (e) { console.log(e); throw new Error("Download Url fetching error"); @@ -147,20 +132,6 @@ export class ManaPlusAppWindows implements ManaPlusApp { isNewVersion: boolean; newestVersion: string; }> { - try { - let versions = await getRequest( - "https://tmw2.org/manalauncher/versions.json?" + Date.now() - ); - let currect_version = (await this.isInstalled) - ? await this.getVersion() - : "-"; - return { - isNewVersion: - currect_version.indexOf(versions.windows64.version) === -1, - newestVersion: versions.windows64.version, - }; - } catch (e) { - throw e; - } + return updateAvailable("windows64", this); } } |