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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
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 type { ZBookmark } from "@hoarder/shared/types/bookmarks";
import {
BookmarkTypes,
MAX_NUM_BOOKMARKS_PER_PAGE,
} from "@hoarder/shared/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 == BookmarkTypes.LINK && ret.content.htmlContent) {
if (ret.content.htmlContent.length > 10) {
ret.content.htmlContent =
ret.content.htmlContent.substring(0, 10) + "... <CROPPED>";
}
}
return ret;
}
function printBookmark(bookmark: ZBookmark) {
printObject(normalizeBookmark(bookmark));
}
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 results: object[] = [];
const promises = [
...opts.link.map((url) =>
api.bookmarks.createBookmark
.mutate({ type: BookmarkTypes.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: BookmarkTypes.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: BookmarkTypes.TEXT, text })
.then((bookmark: ZBookmark) => {
results.push(normalizeBookmark(bookmark));
})
.catch(
printError(
`Failed to add a text bookmark with text "${text.substring(0, 50)}"`,
),
),
);
}
await Promise.allSettled(promises);
printObject(results);
});
bookmarkCmd
.command("get")
.description("fetch information about a bookmark")
.argument("<id>", "The id of the bookmark to get")
.action(async (id) => {
const api = getAPIClient();
await api.bookmarks.getBookmark
.query({ bookmarkId: id })
.then(printBookmark)
.catch(printError(`Failed to get the bookmark with id "${id}"`));
});
bookmarkCmd
.command("update")
.description("update a bookmark")
.option("--title <title>", "if set, the bookmark's title will be updated")
.option("--note <note>", "if set, the bookmark's note will be updated")
.option("--archive", "if set, the bookmark will be archived")
.option("--no-archive", "if set, the bookmark will be unarchived")
.option("--favourite", "if set, the bookmark will be favourited")
.option("--no-favourite", "if set, the bookmark will be unfavourited")
.argument("<id>", "the id of the bookmark to get")
.action(async (id, opts) => {
const api = getAPIClient();
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
.command("list")
.description("list all bookmarks")
.option(
"--include-archived",
"If set, archived bookmarks will be fetched as well",
false,
)
.option("--list-id <id>", "if set, only items from that list will be fetched")
.action(async (opts) => {
const api = getAPIClient();
const request = {
archived: opts.includeArchived ? undefined : false,
listId: opts.listId,
limit: MAX_NUM_BOOKMARKS_PER_PAGE,
useCursorV2: true,
};
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];
}
printObject(results.map(normalizeBookmark), { maxArrayLength: null });
} catch (e) {
printStatusMessage(false, "Failed to query bookmarks");
}
});
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 })
.then(printSuccess(`Bookmark with id '${id}' got deleted`))
.catch(printError(`Failed to delete bookmark with id "${id}"`));
});
|