From 3352a3ea393849550573deff8d774ba6bf149471 Mon Sep 17 00:00:00 2001 From: MohamedBassem Date: Wed, 24 Apr 2024 11:37:35 +0100 Subject: build(cli): Prepare for publishing CLI to npm --- apps/cli/src/commands/lists.ts | 74 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 apps/cli/src/commands/lists.ts (limited to 'apps/cli/src/commands/lists.ts') diff --git a/apps/cli/src/commands/lists.ts b/apps/cli/src/commands/lists.ts new file mode 100644 index 00000000..abf6f78c --- /dev/null +++ b/apps/cli/src/commands/lists.ts @@ -0,0 +1,74 @@ +import { getAPIClient } from "@/lib/trpc"; +import { Command } from "@commander-js/extra-typings"; +import { getBorderCharacters, table } from "table"; + +import { listsToTree } from "@hoarder/shared/utils/listUtils"; + +export const listsCmd = new Command() + .name("lists") + .description("Manipulating lists"); + +listsCmd + .command("list") + .description("Lists all lists") + .action(async () => { + const api = getAPIClient(); + + const resp = await api.lists.list.query(); + const { allPaths } = listsToTree(resp.lists); + + const data: string[][] = [["Id", "Name"]]; + + allPaths.forEach((path) => { + const name = path.map((p) => `${p.icon} ${p.name}`).join(" / "); + const id = path[path.length - 1].id; + data.push([id, name]); + }); + console.log( + table(data, { border: getBorderCharacters("ramac"), singleLine: true }), + ); + }); + +listsCmd + .command("delete") + .description("Deletes a list") + .argument("", "The id of the list") + .action(async (id) => { + const api = getAPIClient(); + + await api.lists.delete.mutate({ + listId: id, + }); + console.log("Successfully deleted list with id:", id); + }); + +listsCmd + .command("add-bookmark") + .description("Add a bookmark to list") + .requiredOption("--list ", "The id of the list") + .requiredOption("--bookmark ", "The id of the bookmark") + .action(async (opts) => { + const api = getAPIClient(); + + await api.lists.addToList.mutate({ + listId: opts.list, + bookmarkId: opts.bookmark, + }); + console.log("Successfully added bookmark from list"); + }); + +listsCmd + .command("remove-bookmark") + .description("Remove a bookmark from list") + .requiredOption("--list ", "The id of the list") + .requiredOption("--bookmark ", "The id of the bookmark") + .action(async (opts) => { + const api = getAPIClient(); + + await api.lists.removeFromList.mutate({ + listId: opts.list, + bookmarkId: opts.bookmark, + }); + + console.log("Successfully removed bookmark from list"); + }); -- cgit v1.2.3-70-g09d2