aboutsummaryrefslogtreecommitdiffstats
path: root/apps/cli/commands/bookmarks.ts
blob: 1727db22bda0c9a9f8d835cf6d180c1dc4508272 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import * as fs from "node:fs";
import { Command } from "@commander-js/extra-typings";
import chalk from "chalk";
import { getAPIClient } from "lib/trpc";

import type { ZBookmark } from "@hoarder/trpc/types/bookmarks";

export const bookmarkCmd = new Command()
  .name("bookmarks")
  .description("Manipulating bookmarks");

function collect<T>(val: T, acc: T[]) {
  acc.push(val);
  return acc;
}

function normalizeBookmark(bookmark: ZBookmark) {
  const ret = {
    ...bookmark,
    tags: bookmark.tags.map((t) => t.name),
  };

  if (ret.content.type == "link" && ret.content.htmlContent) {
    if (ret.content.htmlContent.length > 10) {
      ret.content.htmlContent =
        ret.content.htmlContent.substring(0, 10) + "... <CROPPED>";
    }
  }
  return ret;
}

bookmarkCmd
  .command("add")
  .description("Creates a new bookmark")
  .option(
    "--link <link>",
    "the link to add. Specify multiple times to add multiple links",
    collect<string>,
    [],
  )
  .option(
    "--note <note>",
    "the note text to add. Specify multiple times to add multiple notes",
    collect<string>,
    [],
  )
  .option("--stdin", "reads the data from stdin and store it as a note")
  .action(async (opts) => {
    const api = getAPIClient();

    const promises = [
      ...opts.link.map((url) =>
        api.bookmarks.createBookmark.mutate({ type: "link", url }),
      ),
      ...opts.note.map((text) =>
        api.bookmarks.createBookmark.mutate({ type: "text", text }),
      ),
    ];

    if (opts.stdin) {
      const text = fs.readFileSync(0, "utf-8");
      promises.push(
        api.bookmarks.createBookmark.mutate({ type: "text", text }),
      );
    }

    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}`));
      }
    }
  });

bookmarkCmd
  .command("get")
  .description("fetch information about a bookmark")
  .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));
  });

bookmarkCmd
  .command("list")
  .description("list all bookmarks")
  .option(
    "--include-archived",
    "If set, archived bookmarks will be fetched as well",
    false,
  )
  .action(async (opts) => {
    const api = getAPIClient();
    const resp = await api.bookmarks.getBookmarks.query({
      archived: opts.includeArchived ? undefined : false,
    });
    console.log(resp.bookmarks.map(normalizeBookmark));
  });

bookmarkCmd
  .command("delete")
  .description("delete a bookmark")
  .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`);
  });