From 78571d1799e8962cab30726e98bc08f888057647 Mon Sep 17 00:00:00 2001 From: Alvin Dimas Praditya Date: Mon, 26 Aug 2024 05:35:22 +0700 Subject: [PATCH] readable condition --- src/commandline.ts | 7 +++++-- src/patches/amdfriend.ts | 7 ++++--- src/patches/chromium.ts | 4 +++- src/patches/discord.ts | 3 ++- src/patches/firefox-dev.ts | 23 +++-------------------- src/patches/firefox.ts | 18 ++++++++++++------ src/types.ts | 5 +++++ 7 files changed, 34 insertions(+), 33 deletions(-) create mode 100644 src/types.ts diff --git a/src/commandline.ts b/src/commandline.ts index 35571c1..9a37c55 100644 --- a/src/commandline.ts +++ b/src/commandline.ts @@ -5,6 +5,8 @@ import inquirer from "inquirer"; // @ts-ignore import clear from "console-clear"; import path from "path"; +import {PatchType} from "@src/types"; + export default class CommandLine { basePath = "/Applications/"; @@ -53,12 +55,13 @@ export default class CommandLine { logSupportedApps(){ this.supportedApps.forEach((app, i) => { let status: string; + switch(app.patched()){ - case 1: + case PatchType.PATCHED: // @ts-ignore status = chalk.green("PATCHED"); break; - case 0: + case PatchType.UNPATCHED: // @ts-ignore status = chalk.red("NOT PATCHED"); break; diff --git a/src/patches/amdfriend.ts b/src/patches/amdfriend.ts index 6855907..d5cc4f9 100644 --- a/src/patches/amdfriend.ts +++ b/src/patches/amdfriend.ts @@ -7,6 +7,7 @@ import type {PatchOptions} from "amdfriend/src/types"; import {patchFile} from "amdfriend/src"; import {isRoot, patchOptions} from "@src/utils"; import AppPatch from "@patches/apppatch"; +import {PatchType} from "@src/types"; const amdfriends = ["Adobe Photoshop", "CorelDRAW"]; @@ -18,14 +19,14 @@ export default class Amdfriend extends AppPatch { } patched() { const fileExists = fs.existsSync(this.patchedPath); - if(!isRoot() && !fileExists) return -1; - return (fileExists ? 1 : 0); + if(!isRoot() && !fileExists) return PatchType.UNDETECTED; + return (fileExists ? PatchType.PATCHED : PatchType.UNPATCHED); } supported(){ return amdfriends.some(v => this.appName.includes(v) || v.includes(this.appName)); } async patch(){ - if(this.patched() === 1) return console.log(`${this.appName} already patched. Ignoring...`); + if(this.patched() === PatchType.PATCHED) return console.log(`${this.appName} already patched. Ignoring...`); await parallelizer(this.patchDirectories(), cpus().length); if(!isRoot()) fs.writeFileSync(this.patchedPath, ""); diff --git a/src/patches/chromium.ts b/src/patches/chromium.ts index 2769fbf..dddd960 100644 --- a/src/patches/chromium.ts +++ b/src/patches/chromium.ts @@ -2,6 +2,7 @@ import path from "path"; import os from "os"; import fs from "fs"; import AppPatch from "@patches/apppatch"; +import {PatchType} from "@src/types"; interface ChromiumConfig { browser?: { @@ -51,7 +52,8 @@ export default class Chromium extends AppPatch { patched() { return (this.config.browser !== undefined && this.config.browser.enabled_labs_experiments !== undefined && - this.config.browser.enabled_labs_experiments.includes(this.patchValue)) ? 1 : 0; + this.config.browser.enabled_labs_experiments.includes(this.patchValue)) ? + PatchType.PATCHED : PatchType.UNPATCHED; } patch(){ if(this.configPath == undefined || this.config == undefined){ diff --git a/src/patches/discord.ts b/src/patches/discord.ts index 8e4b1cb..3c1dcaa 100644 --- a/src/patches/discord.ts +++ b/src/patches/discord.ts @@ -4,6 +4,7 @@ import {homedir} from "os"; import {isRoot, patchOptions, searchFile} from "@src/utils"; import fs from "fs"; import {patchFile} from "amdfriend/src"; +import {PatchType} from "@src/types"; const discordPath = path.join(homedir(), "Library", "Application Support", "discord") export default class Discord extends AppPatch { @@ -24,7 +25,7 @@ export default class Discord extends AppPatch { } patched() { const fileExists = fs.existsSync(this.patchedPath); - return (fileExists ? 1 : 0); + return (fileExists ? PatchType.PATCHED : PatchType.UNPATCHED); } supported(): boolean { return this.appName === "Discord"; diff --git a/src/patches/firefox-dev.ts b/src/patches/firefox-dev.ts index 3206c29..90502f8 100644 --- a/src/patches/firefox-dev.ts +++ b/src/patches/firefox-dev.ts @@ -2,32 +2,15 @@ import AppPatch from "@patches/apppatch"; import path from "path"; import {homedir} from "os"; import fs from "fs"; +import Firefox from "@patches/firefox"; const firefoxPath = path.join(homedir(), "Library", "Application Support", "Firefox", "Profiles") -const patchCode = "user_pref(\"layers.acceleration.disabled\", true);" -export default class FirefoxDev extends AppPatch { - prefPath: string +export default class FirefoxDev extends Firefox { constructor(appName: string) { super(appName); - if(this.pathExists(firefoxPath)){ - fs.readdirSync(firefoxPath).forEach(dir => { - if(dir.endsWith(".dev-edition-default")){ - this.prefPath = path.join(firefoxPath, dir, "prefs.js"); - } - }); - } + this.setPrefPath(".dev-edition-default") } supported(): boolean { return this.appName === "Firefox Developer Edition"; } - patched() { - let pref = fs.readFileSync(this.prefPath, "utf8"); - return pref.includes(patchCode) ? 1 : 0; - } - patch() { - if(this.patched() === 1) return console.log(`${this.appName} already patched. Ignoring...`); - let pref = fs.readFileSync(this.prefPath, "utf8"); - pref += "\n" + patchCode; - fs.writeFileSync(this.prefPath, pref); - } } diff --git a/src/patches/firefox.ts b/src/patches/firefox.ts index 4f2e14f..11991f3 100644 --- a/src/patches/firefox.ts +++ b/src/patches/firefox.ts @@ -2,18 +2,24 @@ import AppPatch from "@patches/apppatch"; import path from "path"; import {homedir} from "os"; import fs from "fs"; +import {PatchType} from "@src/types"; const firefoxPath = path.join(homedir(), "Library", "Application Support", "Firefox", "Profiles") const patchCode = "user_pref(\"layers.acceleration.disabled\", true);" export default class Firefox extends AppPatch { + firefoxPath: string prefPath: string constructor(appName: string) { super(appName); - if(this.pathExists(firefoxPath)){ - fs.readdirSync(firefoxPath).forEach(dir => { - if(dir.endsWith(".default-release")){ - this.prefPath = path.join(firefoxPath, dir, "prefs.js"); + this.firefoxPath = firefoxPath; + this.setPrefPath(".default-release") + } + setPrefPath(dirname: string){ + if(this.pathExists(this.firefoxPath)){ + fs.readdirSync(this.firefoxPath).forEach(dir => { + if(dir.endsWith(dirname)){ + this.prefPath = path.join(this.firefoxPath, dir, "prefs.js"); } }); } @@ -23,10 +29,10 @@ export default class Firefox extends AppPatch { } patched() { let pref = fs.readFileSync(this.prefPath, "utf8"); - return pref.includes("user_pref(\"layers.acceleration.disabled\", true);") ? 1 : 0; + return pref.includes("user_pref(\"layers.acceleration.disabled\", true);") ? PatchType.PATCHED : PatchType.UNPATCHED; } patch() { - if(this.patched() === 1) return console.log(`${this.appName} already patched. Ignoring...`); + if(this.patched() === PatchType.PATCHED) return console.log(`${this.appName} already patched. Ignoring...`); let pref = fs.readFileSync(this.prefPath, "utf8"); pref += "\n" + patchCode; fs.writeFileSync(this.prefPath, pref); diff --git a/src/types.ts b/src/types.ts new file mode 100644 index 0000000..3fea2a4 --- /dev/null +++ b/src/types.ts @@ -0,0 +1,5 @@ +export enum PatchType { + PATCHED = 1, + UNPATCHED = 0, + UNDETECTED = -1 +} \ No newline at end of file