@refhub/cli 1.8.0 → 1.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  // src/commands/import.ts
2
2
  import { readFileSync } from 'fs';
3
- import { resolveClient, run } from '../client.js';
3
+ import { UsageError, action } from '../client.js';
4
4
  import { format } from '../format.js';
5
5
  function parseTags(tagsCsv) {
6
6
  if (!tagsCsv)
@@ -27,11 +27,10 @@ export function registerImport(program) {
27
27
  .requiredOption('--vault <id>')
28
28
  .requiredOption('--doi <doi>')
29
29
  .option('--tags <ids>', 'comma-separated tag IDs to attach')
30
- .action(async (opts, cmd) => {
31
- const g = cmd.optsWithGlobals();
32
- const client = resolveClient(g.apiKey);
33
- await run(() => handleImportDoi(client, opts.vault, opts.doi, opts.tags, g.table ?? false));
34
- });
30
+ .action(action(async ({ opts, g, getClient }) => {
31
+ const client = getClient();
32
+ await handleImportDoi(client, opts.vault, opts.doi, opts.tags, g.table ?? false);
33
+ }));
35
34
  imp
36
35
  .command('bibtex')
37
36
  .description('Import references from a BibTeX string or file')
@@ -39,36 +38,31 @@ export function registerImport(program) {
39
38
  .option('--bibtex <string>', 'inline BibTeX string')
40
39
  .option('--file <path>', 'path to .bib file')
41
40
  .option('--tags <ids>', 'comma-separated tag IDs to attach')
42
- .action(async (opts, cmd) => {
43
- const g = cmd.optsWithGlobals();
41
+ .action(action(async ({ opts, g, getClient }) => {
44
42
  if (!opts.bibtex && !opts.file) {
45
- process.stderr.write(JSON.stringify({ error: { code: 'missing_input', message: 'Provide --bibtex <string> or --file <path>.' } }) + '\n');
46
- process.exit(2);
43
+ throw new UsageError('missing_input', 'Provide --bibtex <string> or --file <path>.');
47
44
  }
48
45
  if (opts.bibtex && opts.file) {
49
- process.stderr.write(JSON.stringify({ error: { code: 'conflicting_input', message: 'Pass --bibtex <string> or --file <path>, not both.' } }) + '\n');
50
- process.exit(2);
46
+ throw new UsageError('conflicting_input', 'Pass --bibtex <string> or --file <path>, not both.');
51
47
  }
52
48
  let bibtex;
53
49
  try {
54
50
  bibtex = opts.file ? readFileSync(opts.file, 'utf8') : opts.bibtex;
55
51
  }
56
52
  catch (e) {
57
- process.stderr.write(JSON.stringify({ error: { code: 'file_read_error', message: `Cannot read file: ${String(e)}` } }) + '\n');
58
- process.exit(2);
53
+ throw new UsageError('file_read_error', `Cannot read file: ${String(e)}`);
59
54
  }
60
- const client = resolveClient(g.apiKey);
61
- await run(() => handleImportBibtex(client, opts.vault, bibtex, opts.tags, g.table ?? false));
62
- });
55
+ const client = getClient();
56
+ await handleImportBibtex(client, opts.vault, bibtex, opts.tags, g.table ?? false);
57
+ }));
63
58
  imp
64
59
  .command('url')
65
60
  .description('Import a reference from a URL (Open Graph scrape)')
66
61
  .requiredOption('--vault <id>')
67
62
  .requiredOption('--url <url>')
68
63
  .option('--tags <ids>', 'comma-separated tag IDs to attach')
69
- .action(async (opts, cmd) => {
70
- const g = cmd.optsWithGlobals();
71
- const client = resolveClient(g.apiKey);
72
- await run(() => handleImportUrl(client, opts.vault, opts.url, opts.tags, g.table ?? false));
73
- });
64
+ .action(action(async ({ opts, g, getClient }) => {
65
+ const client = getClient();
66
+ await handleImportUrl(client, opts.vault, opts.url, opts.tags, g.table ?? false);
67
+ }));
74
68
  }
@@ -1,6 +1,6 @@
1
1
  // src/commands/inbox.ts
2
2
  import { readFileSync } from 'fs';
3
- import { resolveClient, run } from '../client.js';
3
+ import { UsageError, action } from '../client.js';
4
4
  import { format } from '../format.js';
5
5
  export async function handleInboxList(client, opts, tableMode) {
6
6
  const result = await client.listInbox(opts);
@@ -25,16 +25,14 @@ export async function handleInboxAccept(client, itemId, vaultId, tagsCsv, tableM
25
25
  }
26
26
  export async function handleInboxReject(client, itemId, confirmed) {
27
27
  if (!confirmed) {
28
- process.stderr.write(JSON.stringify({ error: { code: 'confirm_required', message: 'Pass --confirm to acknowledge there is no way to un-reject an inbox item.' } }) + '\n');
29
- process.exit(2);
28
+ throw new UsageError('confirm_required', 'Pass --confirm to acknowledge there is no way to un-reject an inbox item.');
30
29
  }
31
30
  const result = await client.rejectInboxItem(itemId);
32
31
  format(result, false);
33
32
  }
34
33
  export async function handleInboxMerge(client, itemId, confirmed, tableMode) {
35
34
  if (!confirmed) {
36
- process.stderr.write(JSON.stringify({ error: { code: 'confirm_required', message: 'Pass --confirm to acknowledge there is no way to un-merge an inbox item.' } }) + '\n');
37
- process.exit(2);
35
+ throw new UsageError('confirm_required', 'Pass --confirm to acknowledge there is no way to un-merge an inbox item.');
38
36
  }
39
37
  const result = await client.mergeInboxItem(itemId);
40
38
  format(result, tableMode);
@@ -45,8 +43,7 @@ export async function handleInboxPostpone(client, itemId, tableMode) {
45
43
  }
46
44
  export async function handleInboxDelete(client, itemId, confirmed) {
47
45
  if (!confirmed) {
48
- process.stderr.write(JSON.stringify({ error: { code: 'confirm_required', message: 'Pass --confirm to acknowledge this is a hard delete with no undo.' } }) + '\n');
49
- process.exit(2);
46
+ throw new UsageError('confirm_required', 'Pass --confirm to acknowledge this is a hard delete with no undo.');
50
47
  }
51
48
  const result = await client.deleteInboxItem(itemId);
52
49
  format(result, false);
@@ -58,102 +55,92 @@ export function registerInbox(program) {
58
55
  .description('List pending inbox items')
59
56
  .option('--page <n>', 'page number', (v) => parseInt(v, 10))
60
57
  .option('--limit <n>', 'results per page (server default 50, max 200)', (v) => parseInt(v, 10))
61
- .action(async (opts, cmd) => {
62
- const g = cmd.optsWithGlobals();
63
- const client = resolveClient(g.apiKey);
64
- await run(() => handleInboxList(client, { page: opts.page, limit: opts.limit }, g.table ?? false));
65
- });
58
+ .action(action(async ({ opts, g, getClient }) => {
59
+ const client = getClient();
60
+ await handleInboxList(client, { page: opts.page, limit: opts.limit }, g.table ?? false);
61
+ }));
66
62
  const capture = inbox.command('capture').description('Capture a paper into the inbox');
67
63
  capture
68
64
  .command('doi')
69
65
  .description('Capture by DOI')
70
66
  .argument('<doi>')
71
- .action(async (doi, _opts, cmd) => {
72
- const g = cmd.optsWithGlobals();
73
- const client = resolveClient(g.apiKey);
74
- await run(() => handleInboxCaptureDoi(client, doi, g.table ?? false));
75
- });
67
+ .action(action(async ({ args: [doi], g, getClient }) => {
68
+ const client = getClient();
69
+ await handleInboxCaptureDoi(client, doi, g.table ?? false);
70
+ }));
76
71
  capture
77
72
  .command('bibtex')
78
73
  .description('Capture from a BibTeX string or file (bulk -- one inbox item per entry)')
79
74
  .option('--bibtex <string>', 'inline BibTeX string')
80
75
  .option('--file <path>', 'path to .bib file')
81
- .action(async (opts, cmd) => {
82
- const g = cmd.optsWithGlobals();
76
+ .action(action(async ({ opts, g, getClient }) => {
83
77
  if (!opts.bibtex && !opts.file) {
84
- process.stderr.write(JSON.stringify({ error: { code: 'missing_input', message: 'Provide --bibtex <string> or --file <path>.' } }) + '\n');
85
- process.exit(2);
78
+ throw new UsageError('missing_input', 'Provide --bibtex <string> or --file <path>.');
86
79
  }
87
80
  if (opts.bibtex && opts.file) {
88
- process.stderr.write(JSON.stringify({ error: { code: 'conflicting_input', message: 'Pass --bibtex <string> or --file <path>, not both.' } }) + '\n');
89
- process.exit(2);
81
+ throw new UsageError('conflicting_input', 'Pass --bibtex <string> or --file <path>, not both.');
90
82
  }
91
83
  let bibtex;
92
84
  try {
93
85
  bibtex = opts.file ? readFileSync(opts.file, 'utf8') : opts.bibtex;
94
86
  }
95
87
  catch (e) {
96
- process.stderr.write(JSON.stringify({ error: { code: 'file_read_error', message: `Cannot read file: ${String(e)}` } }) + '\n');
97
- process.exit(2);
88
+ throw new UsageError('file_read_error', `Cannot read file: ${String(e)}`);
98
89
  }
99
- const client = resolveClient(g.apiKey);
100
- await run(() => handleInboxCaptureBibtex(client, bibtex, g.table ?? false));
101
- });
90
+ const client = getClient();
91
+ await handleInboxCaptureBibtex(client, bibtex, g.table ?? false);
92
+ }));
102
93
  capture
103
94
  .command('manual')
104
95
  .description('Capture a manual entry (title only)')
105
96
  .requiredOption('--title <title>')
106
- .action(async (opts, cmd) => {
107
- const g = cmd.optsWithGlobals();
108
- const client = resolveClient(g.apiKey);
109
- await run(() => handleInboxCaptureManual(client, opts.title, g.table ?? false));
110
- });
97
+ .action(action(async ({ opts, g, getClient }) => {
98
+ const client = getClient();
99
+ await handleInboxCaptureManual(client, opts.title, g.table ?? false);
100
+ }));
111
101
  inbox
112
102
  .command('accept')
113
103
  .argument('<itemId>')
114
104
  .description('File a pending item into a vault')
115
105
  .requiredOption('--vault <id>')
116
106
  .option('--tags <ids>', 'comma-separated tag IDs to attach')
117
- .action(async (itemId, opts, cmd) => {
118
- const g = cmd.optsWithGlobals();
119
- const client = resolveClient(g.apiKey);
120
- await run(() => handleInboxAccept(client, itemId, opts.vault, opts.tags, g.table ?? false));
121
- });
107
+ .action(action(async ({ args: [itemId], opts, g, getClient }) => {
108
+ const client = getClient();
109
+ await handleInboxAccept(client, itemId, opts.vault, opts.tags, g.table ?? false);
110
+ }));
122
111
  inbox
123
112
  .command('reject')
124
113
  .argument('<itemId>')
125
114
  .description('Discard a pending item (no undo)')
126
115
  .option('--confirm', 'required: acknowledge there is no way to un-reject')
127
- .action(async (itemId, opts, cmd) => {
128
- const client = resolveClient(cmd.optsWithGlobals().apiKey);
129
- await run(() => handleInboxReject(client, itemId, opts.confirm ?? false));
130
- });
116
+ .action(action(async ({ args: [itemId], opts, getClient }) => {
117
+ const client = getClient();
118
+ await handleInboxReject(client, itemId, opts.confirm ?? false);
119
+ }));
131
120
  inbox
132
121
  .command('merge')
133
122
  .argument('<itemId>')
134
123
  .description('File a pending item as a duplicate of the match RefHub already found (no undo)')
135
124
  .option('--confirm', 'required: acknowledge there is no way to un-merge')
136
- .action(async (itemId, opts, cmd) => {
137
- const g = cmd.optsWithGlobals();
138
- const client = resolveClient(g.apiKey);
139
- await run(() => handleInboxMerge(client, itemId, opts.confirm ?? false, g.table ?? false));
140
- });
125
+ .action(action(async ({ args: [itemId], opts, g, getClient }) => {
126
+ const client = getClient();
127
+ await handleInboxMerge(client, itemId, opts.confirm ?? false, g.table ?? false);
128
+ }));
141
129
  inbox
142
130
  .command('postpone')
143
131
  .argument('<itemId>')
144
132
  .description('Move a pending item to the back of the queue')
145
- .action(async (itemId, _opts, cmd) => {
146
- const g = cmd.optsWithGlobals();
147
- const client = resolveClient(g.apiKey);
148
- await run(() => handleInboxPostpone(client, itemId, g.table ?? false));
149
- });
133
+ .action(action(async ({ args: [itemId], g, getClient }) => {
134
+ const client = getClient();
135
+ await handleInboxPostpone(client, itemId, g.table ?? false);
136
+ }));
150
137
  inbox
151
138
  .command('delete')
152
139
  .argument('<itemId>')
153
140
  .description('Delete an inbox item regardless of status (hard delete, no undo)')
154
141
  .option('--confirm', 'required: acknowledge this is a permanent deletion')
155
- .action(async (itemId, opts, cmd) => {
156
- const client = resolveClient(cmd.optsWithGlobals().apiKey);
157
- await run(() => handleInboxDelete(client, itemId, opts.confirm ?? false));
158
- });
142
+ .action(action(async ({ args: [itemId], opts, getClient }) => {
143
+ const client = getClient();
144
+ await handleInboxDelete(client, itemId, opts.confirm ?? false);
145
+ }));
159
146
  }
@@ -1,11 +1,10 @@
1
1
  // src/commands/items.ts
2
2
  import { readFileSync } from 'fs';
3
3
  import { InvalidArgumentError } from 'commander';
4
- import { resolveClient, run } from '../client.js';
4
+ import { UsageError, action } from '../client.js';
5
5
  import { format } from '../format.js';
6
6
  function usageError(code, message) {
7
- process.stderr.write(JSON.stringify({ error: { code, message } }) + '\n');
8
- process.exit(2);
7
+ throw new UsageError(code, message);
9
8
  }
10
9
  /** Collects a repeatable option into an array. */
11
10
  export function collectOption(value, previous = []) {
@@ -121,8 +120,7 @@ export async function handleItemUpdate(client, vaultId, itemId, opts, tableMode)
121
120
  }
122
121
  export async function handleItemDelete(client, vaultId, itemId, confirmed) {
123
122
  if (!confirmed) {
124
- process.stderr.write(JSON.stringify({ error: { code: 'confirm_required', message: 'Pass --confirm to acknowledge this is a hard delete with no undo.' } }) + '\n');
125
- process.exit(2);
123
+ throw new UsageError('confirm_required', 'Pass --confirm to acknowledge this is a hard delete with no undo.');
126
124
  }
127
125
  const result = await client.deleteItem(vaultId, itemId);
128
126
  format(result, false);
@@ -130,8 +128,7 @@ export async function handleItemDelete(client, vaultId, itemId, confirmed) {
130
128
  export async function handleItemUpsert(client, vaultId, filePath, idempotencyKey, tableMode) {
131
129
  const items = JSON.parse(readFileSync(filePath, 'utf8'));
132
130
  if (!Array.isArray(items)) {
133
- process.stderr.write(JSON.stringify({ error: { code: 'invalid_file', message: 'File must contain a JSON array of items.' } }) + '\n');
134
- process.exit(2);
131
+ throw new UsageError('invalid_file', 'File must contain a JSON array of items.');
135
132
  }
136
133
  const result = await client.upsertItems(vaultId, items, idempotencyKey);
137
134
  format(result, tableMode);
@@ -139,8 +136,7 @@ export async function handleItemUpsert(client, vaultId, filePath, idempotencyKey
139
136
  export async function handleItemPreview(client, vaultId, filePath, tableMode) {
140
137
  const items = JSON.parse(readFileSync(filePath, 'utf8'));
141
138
  if (!Array.isArray(items)) {
142
- process.stderr.write(JSON.stringify({ error: { code: 'invalid_file', message: 'File must contain a JSON array of items.' } }) + '\n');
143
- process.exit(2);
139
+ throw new UsageError('invalid_file', 'File must contain a JSON array of items.');
144
140
  }
145
141
  const result = await client.previewImport(vaultId, items);
146
142
  format(result, tableMode);
@@ -173,21 +169,19 @@ export function registerItems(program) {
173
169
  .requiredOption('--vault <id>')
174
170
  .option('--page <n>', 'page number', (v) => parseInt(v, 10))
175
171
  .option('--limit <n>', 'results per page', (v) => parseInt(v, 10))
176
- .action(async (opts, cmd) => {
177
- const g = cmd.optsWithGlobals();
178
- const client = resolveClient(g.apiKey);
179
- await run(() => handleItemsList(client, opts.vault, { page: opts.page, limit: opts.limit }, g.table ?? false));
180
- });
172
+ .action(action(async ({ opts, g, getClient }) => {
173
+ const client = getClient();
174
+ await handleItemsList(client, opts.vault, { page: opts.page, limit: opts.limit }, g.table ?? false);
175
+ }));
181
176
  items
182
177
  .command('get')
183
178
  .argument('<itemId>')
184
179
  .description('Get a single item')
185
180
  .requiredOption('--vault <id>')
186
- .action(async (itemId, opts, cmd) => {
187
- const g = cmd.optsWithGlobals();
188
- const client = resolveClient(g.apiKey);
189
- await run(() => handleItemGet(client, opts.vault, itemId, g.table ?? false));
190
- });
181
+ .action(action(async ({ args: [itemId], opts, g, getClient }) => {
182
+ const client = getClient();
183
+ await handleItemGet(client, opts.vault, itemId, g.table ?? false);
184
+ }));
191
185
  items
192
186
  .command('add')
193
187
  .description('Add an item to a vault')
@@ -201,11 +195,10 @@ export function registerItems(program) {
201
195
  .option('--tags <ids>', 'comma-separated tag IDs')
202
196
  .option('--notes <text>', 'free-text notes on the item')
203
197
  .option('--pdf-url <url>', "publisher PDF link (the frontend's publisher_pdf field)")
204
- .action(async (opts, cmd) => {
205
- const g = cmd.optsWithGlobals();
206
- const client = resolveClient(g.apiKey);
207
- await run(() => handleItemAdd(client, opts.vault, { title: opts.title, author: opts.author, authors: opts.authors, year: opts.year, doi: opts.doi, url: opts.url, tags: opts.tags, notes: opts.notes, pdfUrl: opts.pdfUrl }, g.table ?? false));
208
- });
198
+ .action(action(async ({ opts, g, getClient }) => {
199
+ const client = getClient();
200
+ await handleItemAdd(client, opts.vault, { title: opts.title, author: opts.author, authors: opts.authors, year: opts.year, doi: opts.doi, url: opts.url, tags: opts.tags, notes: opts.notes, pdfUrl: opts.pdfUrl }, g.table ?? false);
201
+ }));
209
202
  items
210
203
  .command('update')
211
204
  .argument('<itemId>')
@@ -225,54 +218,48 @@ export function registerItems(program) {
225
218
  .option('--featured', 'mark this item as featured — requires vault owner access')
226
219
  .option('--unfeature', 'remove this item from featured — requires vault owner access')
227
220
  .option('--featured-note <text>', 'curator note shown alongside the featured item — requires vault owner access')
228
- .action(async (itemId, opts, cmd) => {
221
+ .action(action(async ({ args: [itemId], opts, g, getClient }) => {
229
222
  if (opts.section && opts.unsetSection) {
230
- process.stderr.write(JSON.stringify({ error: { code: 'invalid_options', message: 'Pass at most one of --section or --unset-section.' } }) + '\n');
231
- process.exit(2);
223
+ throw new UsageError('invalid_options', 'Pass at most one of --section or --unset-section.');
232
224
  }
233
225
  if (opts.featured && opts.unfeature) {
234
- process.stderr.write(JSON.stringify({ error: { code: 'invalid_options', message: 'Pass at most one of --featured or --unfeature.' } }) + '\n');
235
- process.exit(2);
226
+ throw new UsageError('invalid_options', 'Pass at most one of --featured or --unfeature.');
236
227
  }
237
- const g = cmd.optsWithGlobals();
238
- const client = resolveClient(g.apiKey);
239
- await run(() => handleItemUpdate(client, opts.vault, itemId, {
228
+ const client = getClient();
229
+ await handleItemUpdate(client, opts.vault, itemId, {
240
230
  title: opts.title, author: opts.author, authors: opts.authors, year: opts.year, doi: opts.doi, url: opts.url, tags: opts.tags, notes: opts.notes, pdfUrl: opts.pdfUrl,
241
231
  section: opts.section, unsetSection: opts.unsetSection, featured: opts.featured, unfeature: opts.unfeature, featuredNote: opts.featuredNote,
242
- }, g.table ?? false));
243
- });
232
+ }, g.table ?? false);
233
+ }));
244
234
  items
245
235
  .command('delete')
246
236
  .argument('<itemId>')
247
237
  .description('Delete an item (hard delete, no undo)')
248
238
  .requiredOption('--vault <id>')
249
239
  .option('--confirm', 'required: acknowledge permanent deletion')
250
- .action(async (itemId, opts, cmd) => {
251
- const g = cmd.optsWithGlobals();
252
- const client = resolveClient(g.apiKey);
253
- await run(() => handleItemDelete(client, opts.vault, itemId, opts.confirm ?? false));
254
- });
240
+ .action(action(async ({ args: [itemId], opts, g, getClient }) => {
241
+ const client = getClient();
242
+ await handleItemDelete(client, opts.vault, itemId, opts.confirm ?? false);
243
+ }));
255
244
  items
256
245
  .command('upsert')
257
246
  .description('Bulk upsert items from a JSON file')
258
247
  .requiredOption('--vault <id>')
259
248
  .requiredOption('--file <path>', 'path to JSON file containing array of items')
260
249
  .option('--idempotency-key <key>', 'safe retry key')
261
- .action(async (opts, cmd) => {
262
- const g = cmd.optsWithGlobals();
263
- const client = resolveClient(g.apiKey);
264
- await run(() => handleItemUpsert(client, opts.vault, opts.file, opts.idempotencyKey, g.table ?? false));
265
- });
250
+ .action(action(async ({ opts, g, getClient }) => {
251
+ const client = getClient();
252
+ await handleItemUpsert(client, opts.vault, opts.file, opts.idempotencyKey, g.table ?? false);
253
+ }));
266
254
  items
267
255
  .command('preview')
268
256
  .description('Dry-run upsert — shows what would change without writing')
269
257
  .requiredOption('--vault <id>')
270
258
  .requiredOption('--file <path>', 'path to JSON file containing array of items')
271
- .action(async (opts, cmd) => {
272
- const g = cmd.optsWithGlobals();
273
- const client = resolveClient(g.apiKey);
274
- await run(() => handleItemPreview(client, opts.vault, opts.file, g.table ?? false));
275
- });
259
+ .action(action(async ({ opts, g, getClient }) => {
260
+ const client = getClient();
261
+ await handleItemPreview(client, opts.vault, opts.file, g.table ?? false);
262
+ }));
276
263
  items
277
264
  .command('search')
278
265
  .description('Search items in a vault')
@@ -284,28 +271,25 @@ export function registerItems(program) {
284
271
  .option('--tag <tagId>')
285
272
  .option('--page <n>', '', (v) => parseInt(v, 10))
286
273
  .option('--limit <n>', '', (v) => parseInt(v, 10))
287
- .action(async (opts, cmd) => {
288
- const g = cmd.optsWithGlobals();
289
- const client = resolveClient(g.apiKey);
290
- await run(() => handleItemSearch(client, opts.vault, { q: opts.q, author: opts.author, year: opts.year, doi: opts.doi, tag: opts.tag, page: opts.page, limit: opts.limit }, g.table ?? false));
291
- });
274
+ .action(action(async ({ opts, g, getClient }) => {
275
+ const client = getClient();
276
+ await handleItemSearch(client, opts.vault, { q: opts.q, author: opts.author, year: opts.year, doi: opts.doi, tag: opts.tag, page: opts.page, limit: opts.limit }, g.table ?? false);
277
+ }));
292
278
  items
293
279
  .command('stats')
294
280
  .description('Get vault statistics')
295
281
  .requiredOption('--vault <id>')
296
- .action(async (opts, cmd) => {
297
- const g = cmd.optsWithGlobals();
298
- const client = resolveClient(g.apiKey);
299
- await run(() => handleItemStats(client, opts.vault, g.table ?? false));
300
- });
282
+ .action(action(async ({ opts, g, getClient }) => {
283
+ const client = getClient();
284
+ await handleItemStats(client, opts.vault, g.table ?? false);
285
+ }));
301
286
  items
302
287
  .command('changes')
303
288
  .description('Get items changed since a timestamp')
304
289
  .requiredOption('--vault <id>')
305
290
  .requiredOption('--since <ISO>', 'ISO 8601 timestamp')
306
- .action(async (opts, cmd) => {
307
- const g = cmd.optsWithGlobals();
308
- const client = resolveClient(g.apiKey);
309
- await run(() => handleItemChanges(client, opts.vault, opts.since, g.table ?? false));
310
- });
291
+ .action(action(async ({ opts, g, getClient }) => {
292
+ const client = getClient();
293
+ await handleItemChanges(client, opts.vault, opts.since, g.table ?? false);
294
+ }));
311
295
  }
@@ -1,6 +1,6 @@
1
1
  // src/commands/pdf.ts
2
2
  import { readFileSync } from 'fs';
3
- import { resolveClient, run } from '../client.js';
3
+ import { UsageError, action } from '../client.js';
4
4
  import { format } from '../format.js';
5
5
  export async function handlePdfUpload(client, vaultId, itemId, filePath, tableMode) {
6
6
  let pdfBuffer;
@@ -8,8 +8,7 @@ export async function handlePdfUpload(client, vaultId, itemId, filePath, tableMo
8
8
  pdfBuffer = readFileSync(filePath);
9
9
  }
10
10
  catch (e) {
11
- process.stderr.write(JSON.stringify({ error: { code: 'file_read_error', message: `Cannot read file: ${String(e)}` } }) + '\n');
12
- process.exit(2);
11
+ throw new UsageError('file_read_error', `Cannot read file: ${String(e)}`);
13
12
  }
14
13
  const result = await client.uploadItemPdf(vaultId, itemId, pdfBuffer);
15
14
  format(result, tableMode);
@@ -22,9 +21,8 @@ export function registerPdf(program) {
22
21
  .requiredOption('--vault <id>', 'vault ID')
23
22
  .requiredOption('--item <id>', 'vault item ID')
24
23
  .requiredOption('--file <path>', 'path to PDF file')
25
- .action(async (opts, cmd) => {
26
- const g = cmd.optsWithGlobals();
27
- const client = resolveClient(g.apiKey);
28
- await run(() => handlePdfUpload(client, opts.vault, opts.item, opts.file, g.table ?? false));
29
- });
24
+ .action(action(async ({ opts, g, getClient }) => {
25
+ const client = getClient();
26
+ await handlePdfUpload(client, opts.vault, opts.item, opts.file, g.table ?? false);
27
+ }));
30
28
  }
@@ -1,4 +1,4 @@
1
- import type { Command } from 'commander';
1
+ import { type Command } from 'commander';
2
2
  import { RefHubClient } from '../client.js';
3
3
  export declare function handleRelationsList(client: RefHubClient, vaultId: string, type: string | undefined, tableMode: boolean): Promise<void>;
4
4
  export declare function handleRelationCreate(client: RefHubClient, vaultId: string, opts: {
@@ -1,7 +1,11 @@
1
- import { resolveClient, run } from '../client.js';
1
+ // src/commands/relations.ts
2
+ import { Option } from 'commander';
3
+ import { action } from '../client.js';
2
4
  import { format } from '../format.js';
3
5
  import { registerRelationsScan } from './relationsScan.js';
4
- const RELATION_TYPES = 'cites|extends|builds_on|contradicts|reviews|related';
6
+ import { RELATION_TYPES } from '../types.js';
7
+ // commander refuses any other value, and lists these in its error and in --help.
8
+ const typeOption = () => new Option('--type <type>', 'relation type').choices(RELATION_TYPES);
5
9
  export async function handleRelationsList(client, vaultId, type, tableMode) {
6
10
  const result = await client.listRelations(vaultId, type);
7
11
  format(result, tableMode, ['id', 'publication_id', 'related_publication_id', 'relation_type']);
@@ -28,44 +32,40 @@ export function registerRelations(program) {
28
32
  .command('list')
29
33
  .description('List relations in a vault')
30
34
  .requiredOption('--vault <id>')
31
- .option('--type <type>', RELATION_TYPES)
32
- .action(async (opts, cmd) => {
33
- const g = cmd.optsWithGlobals();
34
- const client = resolveClient(g.apiKey);
35
- await run(() => handleRelationsList(client, opts.vault, opts.type, g.table ?? false));
36
- });
35
+ .addOption(typeOption())
36
+ .action(action(async ({ opts, g, getClient }) => {
37
+ const client = getClient();
38
+ await handleRelationsList(client, opts.vault, opts.type, g.table ?? false);
39
+ }));
37
40
  relations
38
41
  .command('create')
39
42
  .description('Create a relation between two publications')
40
43
  .requiredOption('--vault <id>')
41
44
  .requiredOption('--pub <publicationId>')
42
45
  .requiredOption('--related <publicationId>')
43
- .option('--type <type>', RELATION_TYPES)
44
- .action(async (opts, cmd) => {
45
- const g = cmd.optsWithGlobals();
46
- const client = resolveClient(g.apiKey);
47
- await run(() => handleRelationCreate(client, opts.vault, { pub: opts.pub, related: opts.related, type: opts.type }, g.table ?? false));
48
- });
46
+ .addOption(typeOption())
47
+ .action(action(async ({ opts, g, getClient }) => {
48
+ const client = getClient();
49
+ await handleRelationCreate(client, opts.vault, { pub: opts.pub, related: opts.related, type: opts.type }, g.table ?? false);
50
+ }));
49
51
  relations
50
52
  .command('update')
51
53
  .argument('<relationId>')
52
54
  .description('Update a relation type')
53
55
  .requiredOption('--vault <id>')
54
- .requiredOption('--type <type>', RELATION_TYPES)
55
- .action(async (relationId, opts, cmd) => {
56
- const g = cmd.optsWithGlobals();
57
- const client = resolveClient(g.apiKey);
58
- await run(() => handleRelationUpdate(client, opts.vault, relationId, opts.type, g.table ?? false));
59
- });
56
+ .addOption(typeOption().makeOptionMandatory())
57
+ .action(action(async ({ args: [relationId], opts, g, getClient }) => {
58
+ const client = getClient();
59
+ await handleRelationUpdate(client, opts.vault, relationId, opts.type, g.table ?? false);
60
+ }));
60
61
  relations
61
62
  .command('delete')
62
63
  .argument('<relationId>')
63
64
  .description('Delete a relation')
64
65
  .requiredOption('--vault <id>')
65
- .action(async (relationId, opts, cmd) => {
66
- const g = cmd.optsWithGlobals();
67
- const client = resolveClient(g.apiKey);
68
- await run(() => handleRelationDelete(client, opts.vault, relationId));
69
- });
66
+ .action(action(async ({ args: [relationId], opts, g, getClient }) => {
67
+ const client = getClient();
68
+ await handleRelationDelete(client, opts.vault, relationId);
69
+ }));
70
70
  registerRelationsScan(relations);
71
71
  }