From 66f4c2b2896083b031c6be2fef0b629c123629fc Mon Sep 17 00:00:00 2001 From: Kallum Jones Date: Wed, 3 Aug 2022 20:42:51 +0100 Subject: [PATCH] Add list command --- package-lock.json | 27 ++++++++++++ package.json | 1 + src/commands/list_command.ts | 26 ++++++++++++ src/mod-manager.ts | 4 +- src/mods/sources/modrinth_source.ts | 64 +++++++++++++++++++++++++++++ src/util/util.ts | 7 ++++ 6 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 src/commands/list_command.ts diff --git a/package-lock.json b/package-lock.json index a548671..52b4341 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "1.0.0", "license": "ISC", "dependencies": { + "as-table": "^1.0.55", "axios": "^0.27.2", "chalk": "^5.0.1", "commander": "^9.4.0", @@ -51,6 +52,14 @@ "url": "https://github.com/chalk/ansi-regex?sponsor=1" } }, + "node_modules/as-table": { + "version": "1.0.55", + "resolved": "https://registry.npmjs.org/as-table/-/as-table-1.0.55.tgz", + "integrity": "sha512-xvsWESUJn0JN421Xb9MQw6AsMHRCUknCe0Wjlxvjud80mU4E6hQf1A6NzQKcYNmYw62MfzEtXc+badstZP3JpQ==", + "dependencies": { + "printable-characters": "^1.0.42" + } + }, "node_modules/asynckit": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", @@ -573,6 +582,11 @@ "renderkid": "^3.0.0" } }, + "node_modules/printable-characters": { + "version": "1.0.42", + "resolved": "https://registry.npmjs.org/printable-characters/-/printable-characters-1.0.42.tgz", + "integrity": "sha512-dKp+C4iXWK4vVYZmYSd0KBH5F/h1HoZRsbJ82AVKRO3PEo8L4lBS/vLwhVtpwwuYcoIsVY+1JYKR268yn480uQ==" + }, "node_modules/process-warning": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/process-warning/-/process-warning-2.0.0.tgz", @@ -793,6 +807,14 @@ "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-6.0.1.tgz", "integrity": "sha512-n5M855fKb2SsfMIiFFoVrABHJC8QtHwVx+mHWP3QcEqBHYienj5dHSgjbxtC0WEZXYt4wcD6zrQElDPhFuZgfA==" }, + "as-table": { + "version": "1.0.55", + "resolved": "https://registry.npmjs.org/as-table/-/as-table-1.0.55.tgz", + "integrity": "sha512-xvsWESUJn0JN421Xb9MQw6AsMHRCUknCe0Wjlxvjud80mU4E6hQf1A6NzQKcYNmYw62MfzEtXc+badstZP3JpQ==", + "requires": { + "printable-characters": "^1.0.42" + } + }, "asynckit": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", @@ -1134,6 +1156,11 @@ "renderkid": "^3.0.0" } }, + "printable-characters": { + "version": "1.0.42", + "resolved": "https://registry.npmjs.org/printable-characters/-/printable-characters-1.0.42.tgz", + "integrity": "sha512-dKp+C4iXWK4vVYZmYSd0KBH5F/h1HoZRsbJ82AVKRO3PEo8L4lBS/vLwhVtpwwuYcoIsVY+1JYKR268yn480uQ==" + }, "process-warning": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/process-warning/-/process-warning-2.0.0.tgz", diff --git a/package.json b/package.json index 3c0da0a..d313b77 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "author": "", "license": "ISC", "dependencies": { + "as-table": "^1.0.55", "axios": "^0.27.2", "chalk": "^5.0.1", "commander": "^9.4.0", diff --git a/src/commands/list_command.ts b/src/commands/list_command.ts new file mode 100644 index 0000000..066107f --- /dev/null +++ b/src/commands/list_command.ts @@ -0,0 +1,26 @@ +import Subcommand from "./subcommand.js"; +import {Command} from "commander"; +import ModManager from "../mod-manager.js"; +import asTable from "as-table"; +import Mods from "../mods/mods.js"; +import PrintUtils from "../util/print_utils.js"; +import chalk from "chalk"; +import Util from "../util/util.js"; + +export class ListCommand implements Subcommand { + registerCommand(program: Command): void { + program.command("list") + .description("Lists installed mods") + .action(() => { + ModManager.execute(() => { + let tableFunc = asTable.configure ({ + title: x => chalk.cyanBright(Util.stringPrettyify(x)), + delimiter: chalk.blueBright(' | '), + dash: chalk.blueBright('-') + }) + PrintUtils.info(tableFunc(Mods.getTrackedMods())) + }) + }) + } + +} \ No newline at end of file diff --git a/src/mod-manager.ts b/src/mod-manager.ts index 99547a7..b83696e 100644 --- a/src/mod-manager.ts +++ b/src/mod-manager.ts @@ -7,6 +7,7 @@ import Initialiser from "./util/initialiser.js"; import PrintUtils from "./util/print_utils.js"; import path from "path"; import {Logger, pino} from "pino" +import {ListCommand} from "./commands/list_command.js"; export default class ModManager { @@ -17,7 +18,8 @@ export default class ModManager { private static subcommands: Array = [ new InitCommand(), - new InstallCommand() + new InstallCommand(), + new ListCommand() ]; static init() { diff --git a/src/mods/sources/modrinth_source.ts b/src/mods/sources/modrinth_source.ts index 434a549..2277b35 100644 --- a/src/mods/sources/modrinth_source.ts +++ b/src/mods/sources/modrinth_source.ts @@ -171,6 +171,70 @@ export default class ModrinthSource implements ModSource { return "Modrinth"; } + /** + * Gets the name of the project with the provided id + * Example Shape of data from the query: + * { + * "id": "gvQqBUqZ", + * "slug": "lithium", + * "project_type": "mod", + * "team": "peSx5UYg", + * "title": "Lithium", + * "description": "No-compromises game logic/server optimization mod", + * "body": "...", + * "body_url": "https://cdn.modrinth.com/data/gvQqBUqZ/description.md", + * "published": "2021-01-03T00:56:52.292581Z", + * "updated": "2022-07-29T22:18:05.703354Z", + * "approved": "2021-01-03T00:56:52.292581Z", + * "status": "approved", + * "moderator_message": null, + * "license": { + * "id": "lgpl-3", + * "name": "GNU Lesser General Public License v3", + * "url": "https://cdn.modrinth.com/licenses/lgpl-3.txt" + * }, + * "client_side": "optional", + * "server_side": "optional", + * "downloads": 225038, + * "followers": 1872, + * "categories": [ + * "optimization" + * ], + * "additional_categories": [], + * "versions": [ + * "2w527DB2", + * "ZRR9yqHD", + * "aZ0JFf08", + * "cTZv31gu", + * "igqdFUYG", + * "nVR7Q63z", + * "ouTdXXWj", + * "pGhOMdTm", + * "pHl1Vi6k", + * "pXdccFQf", + * "rvsW1zhb", + * "sIKhU9s4", + * "5fmGl08Y", + * "7jxErppe", + * "EhG1mQzx", + * "Ehsd7YUl", + * "FHFKMKeu", + * "Le0tKjFX", + * "2CbyxeU0", + * "MoF1cn6g", + * "2aoHIXuK" + * ], + * "icon_url": "https://cdn.modrinth.com/data/gvQqBUqZ/icon.png", + * "issues_url": "https://github.com/jellysquid3/lithium-fabric/issues", + * "source_url": "https://github.com/jellysquid3/lithium-fabric", + * "wiki_url": null, + * "discord_url": "https://jellysquid.me/discord", + * "donation_urls": [], + * "gallery": [] + * } + * @param id the id to get the name of + * @return The found project name + */ async getProjectName(id: string): Promise { const response = await axios.get(format(ModrinthSource.PROJECT_URL, id)); return await response.data.title; diff --git a/src/util/util.ts b/src/util/util.ts index 1869f76..a2dbae3 100644 --- a/src/util/util.ts +++ b/src/util/util.ts @@ -2,4 +2,11 @@ export default class Util { static isArrayEmpty(array: Array | undefined): boolean { return array === undefined || array.length == 0; } + + static stringPrettyify(str: string): string { + return str // insert a space before all caps + .replace(/([A-Z])/g, ' $1') + // uppercase the first character + .replace(/^./, function(str){ return str.toUpperCase(); }) + } }