diff options
| author | kamtschatka <sschatka@gmail.com> | 2024-06-09 23:30:24 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-06-09 22:30:24 +0100 |
| commit | cde97267a90802c6a367aa61ff157983506deead (patch) | |
| tree | 7fc6dbca60ab537ce3e3be657f4d510dcdeb5fdb /apps/cli/src/commands/bookmarks.ts | |
| parent | 6928800a604f05ef62234cb5c3ee1e60fb27ea1a (diff) | |
| download | karakeep-cde97267a90802c6a367aa61ff157983506deead.tar.zst | |
fix(cli): Bookmark list output is not a valid JSON. Fixes #150 (#181)
* bookmark list output is not a valid JSON #150
Reworked the cli to switch over to json output
* changed the logging to log created bookmarks as an array
switch all log output that is just a status to stderr
---------
Co-authored-by: kamtschatka <simon.schatka@gmx.at>
Diffstat (limited to 'apps/cli/src/commands/bookmarks.ts')
| -rw-r--r-- | apps/cli/src/commands/bookmarks.ts | 104 |
1 files changed, 70 insertions, 34 deletions
diff --git a/apps/cli/src/commands/bookmarks.ts b/apps/cli/src/commands/bookmarks.ts index 0f557120..40442ec1 100644 --- a/apps/cli/src/commands/bookmarks.ts +++ b/apps/cli/src/commands/bookmarks.ts @@ -1,7 +1,12 @@ import * as fs from "node:fs"; +import { + printError, + printObject, + printStatusMessage, + printSuccess, +} from "@/lib/output"; import { getAPIClient } from "@/lib/trpc"; import { Command } from "@commander-js/extra-typings"; -import chalk from "chalk"; import type { ZBookmark } from "@hoarder/shared/types/bookmarks"; import { MAX_NUM_BOOKMARKS_PER_PAGE } from "@hoarder/shared/types/bookmarks"; @@ -30,6 +35,10 @@ function normalizeBookmark(bookmark: ZBookmark) { return ret; } +function printBookmark(bookmark: ZBookmark) { + printObject(normalizeBookmark(bookmark)); +} + bookmarkCmd .command("add") .description("creates a new bookmark") @@ -49,31 +58,49 @@ bookmarkCmd .action(async (opts) => { const api = getAPIClient(); + const results: object[] = []; + const promises = [ ...opts.link.map((url) => - api.bookmarks.createBookmark.mutate({ type: "link", url }), + api.bookmarks.createBookmark + .mutate({ type: "link", url }) + .then((bookmark: ZBookmark) => { + results.push(normalizeBookmark(bookmark)); + }) + .catch(printError(`Failed to add a link bookmark for url "${url}"`)), ), ...opts.note.map((text) => - api.bookmarks.createBookmark.mutate({ type: "text", text }), + api.bookmarks.createBookmark + .mutate({ type: "text", text }) + .then((bookmark: ZBookmark) => { + results.push(normalizeBookmark(bookmark)); + }) + .catch( + printError( + `Failed to add a text bookmark with text "${text.substring(0, 50)}"`, + ), + ), ), ]; if (opts.stdin) { const text = fs.readFileSync(0, "utf-8"); promises.push( - api.bookmarks.createBookmark.mutate({ type: "text", text }), + api.bookmarks.createBookmark + .mutate({ type: "text", text }) + .then((bookmark: ZBookmark) => { + results.push(normalizeBookmark(bookmark)); + }) + .catch( + printError( + `Failed to add a text bookmark with text "${text.substring(0, 50)}"`, + ), + ), ); } - const results = await Promise.allSettled(promises); - - for (const res of results) { - if (res.status == "fulfilled") { - console.log(normalizeBookmark(res.value)); - } else { - console.log(chalk.red(`Error: ${res.reason}`)); - } - } + await Promise.allSettled(promises); + printObject(results); }); bookmarkCmd @@ -82,8 +109,10 @@ bookmarkCmd .argument("<id>", "The id of the bookmark to get") .action(async (id) => { const api = getAPIClient(); - const resp = await api.bookmarks.getBookmark.query({ bookmarkId: id }); - console.log(normalizeBookmark(resp)); + await api.bookmarks.getBookmark + .query({ bookmarkId: id }) + .then(printBookmark) + .catch(printError(`Failed to get the bookmark with id "${id}"`)); }); bookmarkCmd @@ -98,13 +127,15 @@ bookmarkCmd .argument("<id>", "the id of the bookmark to get") .action(async (id, opts) => { const api = getAPIClient(); - const resp = await api.bookmarks.updateBookmark.mutate({ - bookmarkId: id, - archived: opts.archive, - favourited: opts.favourite, - title: opts.title, - }); - console.log(resp); + await api.bookmarks.updateBookmark + .mutate({ + bookmarkId: id, + archived: opts.archive, + favourited: opts.favourite, + title: opts.title, + }) + .then(printObject) + .catch(printError(`Failed to update bookmark with id "${id}"`)); }); bookmarkCmd @@ -126,18 +157,21 @@ bookmarkCmd useCursorV2: true, }; - let resp = await api.bookmarks.getBookmarks.query(request); - let results: ZBookmark[] = resp.bookmarks; + try { + let resp = await api.bookmarks.getBookmarks.query(request); + let results: ZBookmark[] = resp.bookmarks; - while (resp.nextCursor) { - resp = await api.bookmarks.getBookmarks.query({ - ...request, - cursor: resp.nextCursor, - }); - results = [...results, ...resp.bookmarks]; + while (resp.nextCursor) { + resp = await api.bookmarks.getBookmarks.query({ + ...request, + cursor: resp.nextCursor, + }); + results = [...results, ...resp.bookmarks]; + } + printObject(results.map(normalizeBookmark), { maxArrayLength: null }); + } catch (e) { + printStatusMessage(false, "Failed to query bookmarks"); } - - console.dir(results.map(normalizeBookmark), { maxArrayLength: null }); }); bookmarkCmd @@ -146,6 +180,8 @@ bookmarkCmd .argument("<id>", "the id of the bookmark to delete") .action(async (id) => { const api = getAPIClient(); - await api.bookmarks.deleteBookmark.mutate({ bookmarkId: id }); - console.log(`Bookmark ${id} got deleted`); + await api.bookmarks.deleteBookmark + .mutate({ bookmarkId: id }) + .then(printSuccess(`Bookmark with id '${id}' got deleted`)) + .catch(printError(`Failed to delete bookmark with id "${id}"`)); }); |
