Added uninstall command

This commit is contained in:
Kallum Jones 2022-08-04 14:24:39 +01:00
parent 129d7e7ad5
commit ab83374735
No known key found for this signature in database
GPG Key ID: D7F4589C4D7F81A9
4 changed files with 69 additions and 10 deletions

View File

@ -0,0 +1,20 @@
import Subcommand from "./subcommand.js";
import {Command} from "commander";
import ModManager from "../mod-manager.js";
import Mods from "../mods/mods.js";
export default class UninstallCommand implements Subcommand {
registerCommand(program: Command): void {
program.command("uninstall")
.description("Uninstalls the provided mods")
.argument("<mods...>")
.action((mods) => {
ModManager.execute(() => {
for (let mod of mods) {
Mods.uninstall(mod);
}
})
})
}
}

View File

@ -2,14 +2,13 @@ import path from "path";
import * as https from "https"; import * as https from "https";
import {createWriteStream} from "fs"; import {createWriteStream} from "fs";
import DownloadError from "../errors/download_error.js"; import DownloadError from "../errors/download_error.js";
import Mods from "../mods/mods.js";
export default class FileDownloader { export default class FileDownloader {
private static readonly MODS_FOLDER_PATH: string = path.join("mods");
static downloadMod(task: DownloadTask): void { static downloadMod(task: DownloadTask): void {
https.get(task.url, res => { https.get(task.url, res => {
const filePath = path.join(this.MODS_FOLDER_PATH, task.fileName); const filePath = path.join(Mods.MODS_FOLDER_PATH, task.fileName);
const writeStream = createWriteStream(filePath); const writeStream = createWriteStream(filePath);
res.pipe(writeStream); res.pipe(writeStream);
writeStream.on("finish", () => writeStream.close()); writeStream.on("finish", () => writeStream.close());

View File

@ -8,6 +8,7 @@ import PrintUtils from "./util/print_utils.js";
import path from "path"; import path from "path";
import {Logger, pino} from "pino" import {Logger, pino} from "pino"
import {ListCommand} from "./commands/list_command.js"; import {ListCommand} from "./commands/list_command.js";
import UninstallCommand from "./commands/uninstall_command.js";
export default class ModManager { export default class ModManager {
@ -19,7 +20,8 @@ export default class ModManager {
private static subcommands: Array<Subcommand> = [ private static subcommands: Array<Subcommand> = [
new InitCommand(), new InitCommand(),
new InstallCommand(), new InstallCommand(),
new ListCommand() new ListCommand(),
new UninstallCommand()
]; ];
static init() { static init() {

View File

@ -4,12 +4,13 @@ import PrintUtils from "../util/print_utils.js";
import ModrinthSource from "./sources/modrinth_source.js"; import ModrinthSource from "./sources/modrinth_source.js";
import ModSource from "./sources/mod_source.js"; import ModSource from "./sources/mod_source.js";
import ModNotFoundError from "../errors/mod_not_found_error.js"; import ModNotFoundError from "../errors/mod_not_found_error.js";
import {readFileSync, writeFileSync} from "fs"; import {readFileSync, unlinkSync, writeFileSync} from "fs";
import Util from "../util/util.js"; import Util from "../util/util.js";
export default class Mods { export default class Mods {
public static readonly MOD_FILE = "mods.json"; public static readonly MOD_FILE = "mods.json";
public static readonly MODS_FOLDER_PATH: string = path.join("mods");
private static readonly MOD_SOURCES: Array<ModSource> = [ private static readonly MOD_SOURCES: Array<ModSource> = [
new ModrinthSource() new ModrinthSource()
]; ];
@ -40,22 +41,23 @@ export default class Mods {
// If a mod is found, install it // If a mod is found, install it
if (id != undefined) { if (id != undefined) {
const projectName = await source.getProjectName(id);
// If mod is not already installed // If mod is not already installed
if (!this.isModInstalled(id)) { if (!this.isModInstalled(id)) {
spinner.updateText(`Installing ${mod}...`) spinner.updateText(`Installing ${projectName}...`)
try { try {
const modObj: Mod = await source.install(id); const modObj: Mod = await source.install(id);
this.trackMod(modObj); this.trackMod(modObj);
spinner.succeed(`Successfully installed ${mod}`); spinner.succeed(`Successfully installed ${projectName}`);
success = true; success = true;
} catch (e) { } catch (e) {
// Log the error, and continue to next source // Log the error, and continue to next source
spinner.error(e); spinner.error(e);
} }
} else { } else {
spinner.error(`Mod ${mod} is already installed.`) spinner.error(`Mod ${projectName} is already installed.`)
} }
} }
} }
@ -87,7 +89,43 @@ export default class Mods {
} }
private static isModInstalled(id: string): boolean { private static isModInstalled(id: string): boolean {
const modsFromSource: Array<Mod> = this.getTrackedMods().filter(mod => mod.id == id); const modsWithId: Array<Mod> = this.getTrackedMods().filter(mod => mod.id == id);
return !Util.isArrayEmpty(modsFromSource) return !Util.isArrayEmpty(modsWithId)
}
static uninstall(mod: string) {
let mods: Array<Mod> = this.getTrackedMods();
// Replace underscores with spaces
mod = mod.replaceAll("_", " ");
// Find mod to uninstall
const spinner = new PrintUtils.Spinner(`Uninstalling ${mod}...`)
let modToUninstall: Mod | undefined = undefined;
for (let modEle of mods) {
const id = modEle.id.toLowerCase();
const name = modEle.name.toLowerCase();
const query = mod.toLowerCase();
if (id == query || name == query) {
modToUninstall = modEle;
break;
}
}
// IF a matching mod is found, remove it
if (modToUninstall != undefined) {
// Remove mod from list and uninstall it
unlinkSync(path.join(this.MODS_FOLDER_PATH, modToUninstall.fileName));
mods = mods.filter(item => item !== modToUninstall);
this.writeFile(mods);
spinner.succeed(`${modToUninstall.name} successfully uninstalled!`)
} else {
spinner.error(`${mod} was not found.`)
}
} }
} }