@hive3/cli 0.1.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.
- package/README.md +153 -0
- package/bin/hive.js +5 -0
- package/package.json +26 -0
- package/src/api-client.js +273 -0
- package/src/commands.js +598 -0
- package/src/config.js +91 -0
- package/src/extended-commands.js +493 -0
- package/src/login.js +176 -0
package/src/commands.js
ADDED
|
@@ -0,0 +1,598 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
import { readFile, stat, writeFile } from "node:fs/promises";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
|
|
6
|
+
import { createApiClient, HiveApiError, isResourceKind } from "./api-client.js";
|
|
7
|
+
import { clearConfig, loadConfig } from "./config.js";
|
|
8
|
+
import { runExtendedCommand } from "./extended-commands.js";
|
|
9
|
+
import { login, loginUsage, parseLoginArgs } from "./login.js";
|
|
10
|
+
|
|
11
|
+
function usage() {
|
|
12
|
+
return [
|
|
13
|
+
"Usage: hive <command> [options]",
|
|
14
|
+
"",
|
|
15
|
+
"Commands:",
|
|
16
|
+
" login Connect this CLI through browser approval",
|
|
17
|
+
" logout Remove the stored local credential",
|
|
18
|
+
" whoami Show the authenticated Hive account",
|
|
19
|
+
" capabilities List community targets available to this account",
|
|
20
|
+
" profile <action> Manage profile text, media IDs, access, and builders",
|
|
21
|
+
" upload <file> Upload an allowed image or document and print its storage ID",
|
|
22
|
+
" account <action> Manage account preferences, exports, and deletion requests",
|
|
23
|
+
" wallets <action> Manage linked wallet visibility/default selection",
|
|
24
|
+
" nfts <action> Manage public NFT exposure",
|
|
25
|
+
" notifications <action> Manage notifications and notification mutes",
|
|
26
|
+
" comments <action> Manage post comments",
|
|
27
|
+
" communities <action> Manage community membership and role-derived admin actions",
|
|
28
|
+
" posts <action> Manage posts",
|
|
29
|
+
" events <action> Manage events",
|
|
30
|
+
" courses <action> Manage courses",
|
|
31
|
+
" content <action> Manage content",
|
|
32
|
+
" quests <action> Manage quests",
|
|
33
|
+
" jobs <action> Manage jobs",
|
|
34
|
+
"",
|
|
35
|
+
"Resource CRUD actions:",
|
|
36
|
+
" list [--limit n] [--cursor c] [--owner-type person|community] [--community-id id]",
|
|
37
|
+
" create [--title t] [--body b] [--description d] [--content-type t] [--visibility v]",
|
|
38
|
+
" update <id> [--title t] [--body b] [--description d] [--content-type t] [--visibility v]",
|
|
39
|
+
" archive <id> (workflow subcommands include participants, enrollments, evidence, applications)",
|
|
40
|
+
].join("\n");
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function resourceUsage(kind) {
|
|
44
|
+
return [
|
|
45
|
+
`Usage: hive ${kind} <list|create|update|archive> [options]`,
|
|
46
|
+
"",
|
|
47
|
+
"Create/update fields:",
|
|
48
|
+
" --title <text>",
|
|
49
|
+
" --body <text>",
|
|
50
|
+
" --description <text>",
|
|
51
|
+
" --content-type <type>",
|
|
52
|
+
" --image-url <url>",
|
|
53
|
+
" --visibility <public|protected|private>",
|
|
54
|
+
" --file <json>",
|
|
55
|
+
"",
|
|
56
|
+
"Create owner fields:",
|
|
57
|
+
" --owner-type <person|community>",
|
|
58
|
+
" --community-id <id>",
|
|
59
|
+
" --idempotency-key <key>",
|
|
60
|
+
].join("\n");
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function defaultDeps(overrides = {}) {
|
|
64
|
+
return {
|
|
65
|
+
env: process.env,
|
|
66
|
+
fetch: globalThis.fetch,
|
|
67
|
+
homeDir: undefined,
|
|
68
|
+
now: () => Date.now(),
|
|
69
|
+
openBrowser: undefined,
|
|
70
|
+
platform: process.platform,
|
|
71
|
+
sleep: undefined,
|
|
72
|
+
stderr: process.stderr,
|
|
73
|
+
stdout: process.stdout,
|
|
74
|
+
...overrides,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function loginDeps(deps) {
|
|
79
|
+
const next = {
|
|
80
|
+
env: deps.env,
|
|
81
|
+
fetch: deps.fetch,
|
|
82
|
+
platform: deps.platform,
|
|
83
|
+
stdout: deps.stdout,
|
|
84
|
+
};
|
|
85
|
+
if (deps.homeDir !== undefined) next.homeDir = deps.homeDir;
|
|
86
|
+
if (typeof deps.now === "function") next.now = deps.now;
|
|
87
|
+
if (typeof deps.openBrowser === "function") next.openBrowser = deps.openBrowser;
|
|
88
|
+
if (typeof deps.sleep === "function") next.sleep = deps.sleep;
|
|
89
|
+
return next;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function writeLine(stream, message = "") {
|
|
93
|
+
stream.write(`${message}\n`);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function writeJson(stream, value) {
|
|
97
|
+
writeLine(stream, JSON.stringify(value, null, 2));
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function requiredFlagValue(flag, value) {
|
|
101
|
+
if (!value || value.startsWith("--")) throw new Error(`${flag} requires a value`);
|
|
102
|
+
return value;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function parseListOptions(args) {
|
|
106
|
+
const query = {};
|
|
107
|
+
for (let index = 0; index < args.length; index += 1) {
|
|
108
|
+
const flag = args[index];
|
|
109
|
+
if (flag === "--limit") {
|
|
110
|
+
index += 1;
|
|
111
|
+
query.limit = Number(requiredFlagValue(flag, args[index]));
|
|
112
|
+
if (!Number.isFinite(query.limit) || query.limit <= 0) throw new Error("--limit must be a positive number");
|
|
113
|
+
} else if (flag === "--cursor") {
|
|
114
|
+
index += 1;
|
|
115
|
+
query.cursor = requiredFlagValue(flag, args[index]);
|
|
116
|
+
} else if (flag === "--owner-type") {
|
|
117
|
+
index += 1;
|
|
118
|
+
query.ownerType = requiredFlagValue(flag, args[index]);
|
|
119
|
+
if (query.ownerType !== "person" && query.ownerType !== "community") {
|
|
120
|
+
throw new Error("--owner-type must be person or community");
|
|
121
|
+
}
|
|
122
|
+
} else if (flag === "--community-id") {
|
|
123
|
+
index += 1;
|
|
124
|
+
query.communityId = requiredFlagValue(flag, args[index]);
|
|
125
|
+
} else {
|
|
126
|
+
throw new Error(`Unknown list option: ${flag}`);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return query;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function setPayloadField(payload, key, value) {
|
|
133
|
+
if (value !== undefined) payload[key] = value;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
async function parseResourcePayload(args, includeOwner) {
|
|
137
|
+
let payload = {};
|
|
138
|
+
let idempotencyKey;
|
|
139
|
+
let ownerType;
|
|
140
|
+
let communityId;
|
|
141
|
+
for (let index = 0; index < args.length; index += 1) {
|
|
142
|
+
const flag = args[index];
|
|
143
|
+
if (flag === "--title") {
|
|
144
|
+
index += 1;
|
|
145
|
+
setPayloadField(payload, "title", requiredFlagValue(flag, args[index]));
|
|
146
|
+
} else if (flag === "--body") {
|
|
147
|
+
index += 1;
|
|
148
|
+
setPayloadField(payload, "body", requiredFlagValue(flag, args[index]));
|
|
149
|
+
} else if (flag === "--description") {
|
|
150
|
+
index += 1;
|
|
151
|
+
setPayloadField(payload, "description", requiredFlagValue(flag, args[index]));
|
|
152
|
+
} else if (flag === "--content-type") {
|
|
153
|
+
index += 1;
|
|
154
|
+
setPayloadField(payload, "contentType", requiredFlagValue(flag, args[index]));
|
|
155
|
+
} else if (flag === "--image-url") {
|
|
156
|
+
index += 1;
|
|
157
|
+
setPayloadField(payload, "imageUrl", requiredFlagValue(flag, args[index]));
|
|
158
|
+
} else if (flag === "--visibility") {
|
|
159
|
+
index += 1;
|
|
160
|
+
setPayloadField(payload, "visibility", requiredFlagValue(flag, args[index]));
|
|
161
|
+
} else if (flag === "--file") {
|
|
162
|
+
index += 1;
|
|
163
|
+
const filePayload = await readJsonFile(requiredFlagValue(flag, args[index]));
|
|
164
|
+
if (!filePayload || typeof filePayload !== "object" || Array.isArray(filePayload)) {
|
|
165
|
+
throw new Error("--file must contain a JSON object");
|
|
166
|
+
}
|
|
167
|
+
payload = { ...payload, ...filePayload };
|
|
168
|
+
} else if (includeOwner && flag === "--owner-type") {
|
|
169
|
+
index += 1;
|
|
170
|
+
ownerType = requiredFlagValue(flag, args[index]);
|
|
171
|
+
if (ownerType !== "person" && ownerType !== "community") throw new Error("--owner-type must be person or community");
|
|
172
|
+
} else if (includeOwner && flag === "--community-id") {
|
|
173
|
+
index += 1;
|
|
174
|
+
communityId = requiredFlagValue(flag, args[index]);
|
|
175
|
+
} else if (includeOwner && flag === "--idempotency-key") {
|
|
176
|
+
index += 1;
|
|
177
|
+
idempotencyKey = requiredFlagValue(flag, args[index]);
|
|
178
|
+
} else {
|
|
179
|
+
throw new Error(`Unknown resource option: ${flag}`);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
if (includeOwner && ownerType === "community") {
|
|
183
|
+
if (!communityId) throw new Error("--community-id is required when --owner-type is community");
|
|
184
|
+
payload.owner = { communityId, type: "community" };
|
|
185
|
+
} else if (includeOwner && ownerType === "person") {
|
|
186
|
+
payload.owner = { type: "person" };
|
|
187
|
+
}
|
|
188
|
+
if (Object.keys(payload).length === 0) throw new Error("At least one resource field is required");
|
|
189
|
+
return { idempotencyKey, payload };
|
|
190
|
+
}
|
|
191
|
+
function parseBoolean(value, flag) {
|
|
192
|
+
if (value === "true" || value === "1" || value === "yes") return true;
|
|
193
|
+
if (value === "false" || value === "0" || value === "no") return false;
|
|
194
|
+
throw new Error(`${flag} must be true or false`);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function parseCsv(value) {
|
|
198
|
+
return value.split(",").map((item) => item.trim()).filter(Boolean);
|
|
199
|
+
}
|
|
200
|
+
async function readJsonFile(filePath) {
|
|
201
|
+
const text = await readFile(filePath, "utf8");
|
|
202
|
+
return JSON.parse(text);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function profileBuilderKind(value) {
|
|
206
|
+
if (value === "overview" || value === "widgets" || value === "welcome-page") return value;
|
|
207
|
+
return null;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function apiBuilderKind(value) {
|
|
211
|
+
return value === "welcome-page" ? "welcome-page" : value;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function parseProfileUpdatePayload(args) {
|
|
215
|
+
const payload = {};
|
|
216
|
+
for (let index = 0; index < args.length; index += 1) {
|
|
217
|
+
const flag = args[index];
|
|
218
|
+
if (flag === "--name") {
|
|
219
|
+
index += 1; payload.name = requiredFlagValue(flag, args[index]);
|
|
220
|
+
} else if (flag === "--username" || flag === "--handle") {
|
|
221
|
+
index += 1; payload.username = requiredFlagValue(flag, args[index]);
|
|
222
|
+
} else if (flag === "--title") {
|
|
223
|
+
index += 1; payload.title = requiredFlagValue(flag, args[index]);
|
|
224
|
+
} else if (flag === "--experience") {
|
|
225
|
+
index += 1; payload.experience = requiredFlagValue(flag, args[index]);
|
|
226
|
+
} else if (flag === "--short-description") {
|
|
227
|
+
index += 1; payload.shortDescription = requiredFlagValue(flag, args[index]);
|
|
228
|
+
} else if (flag === "--description") {
|
|
229
|
+
index += 1; payload.description = requiredFlagValue(flag, args[index]);
|
|
230
|
+
} else if (flag === "--location") {
|
|
231
|
+
index += 1; payload.location = requiredFlagValue(flag, args[index]);
|
|
232
|
+
} else if (flag === "--image-storage-id") {
|
|
233
|
+
index += 1; payload.imageStorageId = requiredFlagValue(flag, args[index]);
|
|
234
|
+
} else if (flag === "--hero-image-storage-id" || flag === "--banner-storage-id") {
|
|
235
|
+
index += 1; payload.heroImageStorageId = requiredFlagValue(flag, args[index]);
|
|
236
|
+
} else if (flag === "--attachment-storage-id") {
|
|
237
|
+
index += 1; payload.attachmentStorageId = requiredFlagValue(flag, args[index]);
|
|
238
|
+
} else if (flag === "--show-collaborator-count") {
|
|
239
|
+
index += 1; payload.showCollaboratorCount = parseBoolean(requiredFlagValue(flag, args[index]), flag);
|
|
240
|
+
} else if (flag === "--custom-collaborator-count") {
|
|
241
|
+
index += 1;
|
|
242
|
+
const value = Number(requiredFlagValue(flag, args[index]));
|
|
243
|
+
if (!Number.isFinite(value)) throw new Error(`${flag} must be a number`);
|
|
244
|
+
payload.customCollaboratorCount = value;
|
|
245
|
+
} else if (flag === "--badges") {
|
|
246
|
+
index += 1; payload.badges = parseCsv(requiredFlagValue(flag, args[index]));
|
|
247
|
+
} else if (flag === "--socials-json") {
|
|
248
|
+
index += 1; payload.socials = JSON.parse(requiredFlagValue(flag, args[index]));
|
|
249
|
+
} else {
|
|
250
|
+
throw new Error(`Unknown profile option: ${flag}`);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
if (Object.keys(payload).length === 0) throw new Error("At least one profile field is required");
|
|
254
|
+
return payload;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function mimeFromPath(filePath) {
|
|
258
|
+
const ext = path.extname(filePath).toLowerCase();
|
|
259
|
+
return {
|
|
260
|
+
".doc": "application/msword",
|
|
261
|
+
".docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
262
|
+
".gif": "image/gif",
|
|
263
|
+
".jpeg": "image/jpeg",
|
|
264
|
+
".jpg": "image/jpeg",
|
|
265
|
+
".md": "text/markdown",
|
|
266
|
+
".pdf": "application/pdf",
|
|
267
|
+
".png": "image/png",
|
|
268
|
+
".txt": "text/plain",
|
|
269
|
+
".webp": "image/webp",
|
|
270
|
+
}[ext] || "";
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
async function authenticatedClient(deps) {
|
|
274
|
+
const config = await loadConfig({ env: deps.env, homeDir: deps.homeDir, platform: deps.platform });
|
|
275
|
+
if (!config) throw new HiveApiError("Not logged in. Run hive login first.", { code: "auth_required", status: 401 });
|
|
276
|
+
return createApiClient({ apiBaseUrl: config.apiBaseUrl, apiKey: config.apiKey, fetch: deps.fetch });
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
function capabilityLabels(capabilities) {
|
|
280
|
+
return Object.entries(capabilities)
|
|
281
|
+
.filter(([, enabled]) => enabled)
|
|
282
|
+
.map(([key]) => key.replace(/^can/, "").toLowerCase());
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
async function runWhoami(deps) {
|
|
286
|
+
const result = await (await authenticatedClient(deps)).whoami();
|
|
287
|
+
const data = result.data;
|
|
288
|
+
const person = data.person;
|
|
289
|
+
writeLine(deps.stdout, `Person: ${person.displayName || person.handle || person.id}`);
|
|
290
|
+
if (person.handle) writeLine(deps.stdout, `Handle: @${person.handle}`);
|
|
291
|
+
writeLine(deps.stdout, `Scopes: ${data.credential.scopes.join(", ") || "none"}`);
|
|
292
|
+
writeLine(deps.stdout, `Community targets: ${data.communities.length}`);
|
|
293
|
+
for (const community of data.communities) {
|
|
294
|
+
writeLine(deps.stdout, `- ${community.name} (${community.role}): ${capabilityLabels(community.capabilities).join(", ")}`);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
async function runCapabilities(deps) {
|
|
299
|
+
const result = await (await authenticatedClient(deps)).capabilities();
|
|
300
|
+
const communities = result.data.communities;
|
|
301
|
+
if (communities.length === 0) {
|
|
302
|
+
writeLine(deps.stdout, "No admin or moderator community targets are available.");
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
305
|
+
for (const community of communities) {
|
|
306
|
+
writeLine(deps.stdout, `${community.name} (${community.role}): ${capabilityLabels(community.capabilities).join(", ")}`);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
async function runResource(kind, args, deps) {
|
|
311
|
+
const action = args[0];
|
|
312
|
+
const rest = args.slice(1);
|
|
313
|
+
if (!action || action === "--help" || action === "-h") {
|
|
314
|
+
writeLine(deps.stdout, resourceUsage(kind));
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
const client = await authenticatedClient(deps);
|
|
318
|
+
if (action === "list") {
|
|
319
|
+
writeJson(deps.stdout, await client.listResource(kind, parseListOptions(rest)));
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
if (action === "create") {
|
|
323
|
+
const { idempotencyKey, payload } = await parseResourcePayload(rest, true);
|
|
324
|
+
writeJson(deps.stdout, await client.createResource(kind, payload, idempotencyKey));
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
if (action === "update") {
|
|
328
|
+
const id = requiredFlagValue("resource id", rest[0]);
|
|
329
|
+
const { payload } = await parseResourcePayload(rest.slice(1), false);
|
|
330
|
+
writeJson(deps.stdout, await client.updateResource(kind, id, payload));
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
if (action === "archive") {
|
|
334
|
+
const id = requiredFlagValue("resource id", rest[0]);
|
|
335
|
+
if (rest.length > 1) throw new Error(`Unknown archive option: ${rest[1]}`);
|
|
336
|
+
writeJson(deps.stdout, await client.archiveResource(kind, id));
|
|
337
|
+
return;
|
|
338
|
+
}
|
|
339
|
+
throw new Error(`Unknown ${kind} action: ${action}`);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
async function runProfile(args, deps) {
|
|
343
|
+
const [action, ...rest] = args;
|
|
344
|
+
const client = await authenticatedClient(deps);
|
|
345
|
+
if (action === "get") {
|
|
346
|
+
if (rest.length > 0) throw new Error(`Unknown profile get option: ${rest[0]}`);
|
|
347
|
+
writeJson(deps.stdout, await client.getProfile());
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
if (action === "update") {
|
|
351
|
+
writeJson(deps.stdout, await client.updateProfile(parseProfileUpdatePayload(rest)));
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
if (action === "handle") {
|
|
355
|
+
const handle = requiredFlagValue("handle", rest[0]);
|
|
356
|
+
if (rest.length > 1) throw new Error(`Unknown profile handle option: ${rest[1]}`);
|
|
357
|
+
writeJson(deps.stdout, await client.checkHandle(handle));
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
if (action === "access") {
|
|
361
|
+
let accessType;
|
|
362
|
+
let subscriptionId;
|
|
363
|
+
for (let index = 0; index < rest.length; index += 1) {
|
|
364
|
+
const flag = rest[index];
|
|
365
|
+
if (flag === "--type") {
|
|
366
|
+
index += 1; accessType = requiredFlagValue(flag, rest[index]);
|
|
367
|
+
} else if (flag === "--subscription-id") {
|
|
368
|
+
index += 1; subscriptionId = requiredFlagValue(flag, rest[index]);
|
|
369
|
+
} else {
|
|
370
|
+
throw new Error(`Unknown profile access option: ${flag}`);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
if (!accessType) throw new Error("--type is required");
|
|
374
|
+
writeJson(deps.stdout, await client.updateProfileAccess({ accessType, subscriptionId }));
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
const builderKind = profileBuilderKind(action);
|
|
378
|
+
if (builderKind) {
|
|
379
|
+
const [builderAction, ...builderRest] = rest;
|
|
380
|
+
const apiKind = apiBuilderKind(builderKind);
|
|
381
|
+
if (builderAction === "get") {
|
|
382
|
+
if (builderRest.length > 0) throw new Error(`Unknown ${builderKind} get option: ${builderRest[0]}`);
|
|
383
|
+
writeJson(deps.stdout, await client.getBuilder(apiKind));
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
if (builderAction === "save" || builderAction === "import") {
|
|
387
|
+
if (builderRest[0] !== "--file") throw new Error("--file is required");
|
|
388
|
+
const file = requiredFlagValue("--file", builderRest[1]);
|
|
389
|
+
if (builderRest.length > 2) throw new Error(`Unknown ${builderKind} ${builderAction} option: ${builderRest[2]}`);
|
|
390
|
+
writeJson(deps.stdout, await client.saveBuilder(apiKind, await readJsonFile(file)));
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
if (builderAction === "export") {
|
|
394
|
+
if (builderRest[0] !== "--file") throw new Error("--file is required");
|
|
395
|
+
const file = requiredFlagValue("--file", builderRest[1]);
|
|
396
|
+
if (builderRest.length > 2) throw new Error(`Unknown ${builderKind} export option: ${builderRest[2]}`);
|
|
397
|
+
const data = await client.getBuilder(apiKind);
|
|
398
|
+
await writeFile(file, `${JSON.stringify(data.data, null, 2)}\n`, "utf8");
|
|
399
|
+
writeLine(deps.stdout, `Wrote ${builderKind} config to ${file}.`);
|
|
400
|
+
return;
|
|
401
|
+
}
|
|
402
|
+
throw new Error(`Unknown profile ${builderKind} action: ${builderAction || ""}`.trim());
|
|
403
|
+
}
|
|
404
|
+
throw new Error(`Unknown profile action: ${action || ""}`.trim());
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
async function runUpload(args, deps) {
|
|
408
|
+
const filePath = requiredFlagValue("file", args[0]);
|
|
409
|
+
let purpose = "image";
|
|
410
|
+
let contentType = mimeFromPath(filePath);
|
|
411
|
+
for (let index = 1; index < args.length; index += 1) {
|
|
412
|
+
const flag = args[index];
|
|
413
|
+
if (flag === "--purpose") {
|
|
414
|
+
index += 1; purpose = requiredFlagValue(flag, args[index]);
|
|
415
|
+
} else if (flag === "--content-type") {
|
|
416
|
+
index += 1; contentType = requiredFlagValue(flag, args[index]);
|
|
417
|
+
} else {
|
|
418
|
+
throw new Error(`Unknown upload option: ${flag}`);
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
if (purpose !== "image" && purpose !== "document") throw new Error("--purpose must be image or document");
|
|
422
|
+
if (!contentType) throw new Error("Could not infer content type; pass --content-type");
|
|
423
|
+
const fileStat = await stat(filePath);
|
|
424
|
+
const fileName = path.basename(filePath);
|
|
425
|
+
const client = await authenticatedClient(deps);
|
|
426
|
+
const issued = await client.createUploadUrl({ byteSize: fileStat.size, contentType, fileName, purpose });
|
|
427
|
+
const bytes = await readFile(filePath);
|
|
428
|
+
const response = await deps.fetch(issued.data.uploadUrl, {
|
|
429
|
+
body: new Blob([bytes], { type: contentType }),
|
|
430
|
+
headers: { "Content-Type": contentType },
|
|
431
|
+
method: "POST",
|
|
432
|
+
});
|
|
433
|
+
const text = await response.text();
|
|
434
|
+
const parsed = text ? JSON.parse(text) : {};
|
|
435
|
+
if (!response.ok) {
|
|
436
|
+
throw new HiveApiError(`Upload failed with status ${response.status}`, { code: "upload_failed", status: response.status });
|
|
437
|
+
}
|
|
438
|
+
writeJson(deps.stdout, { data: { ...issued.data, ...parsed } });
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
async function runAccount(args, deps) {
|
|
442
|
+
const [action, ...rest] = args;
|
|
443
|
+
const client = await authenticatedClient(deps);
|
|
444
|
+
if (action === "get") {
|
|
445
|
+
if (rest.length > 0) throw new Error(`Unknown account get option: ${rest[0]}`);
|
|
446
|
+
writeJson(deps.stdout, await client.getAccount());
|
|
447
|
+
return;
|
|
448
|
+
}
|
|
449
|
+
if (action === "preferences") {
|
|
450
|
+
const payload = {};
|
|
451
|
+
for (let index = 0; index < rest.length; index += 1) {
|
|
452
|
+
const flag = rest[index];
|
|
453
|
+
if (flag === "--theme") {
|
|
454
|
+
index += 1; payload.theme = requiredFlagValue(flag, rest[index]);
|
|
455
|
+
} else if (flag === "--muted-notifications") {
|
|
456
|
+
index += 1; payload.mutedNotifications = parseCsv(requiredFlagValue(flag, rest[index]));
|
|
457
|
+
} else {
|
|
458
|
+
throw new Error(`Unknown account preferences option: ${flag}`);
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
writeJson(deps.stdout, await client.updateAccountPreferences(payload));
|
|
462
|
+
return;
|
|
463
|
+
}
|
|
464
|
+
if (action === "export") {
|
|
465
|
+
if (rest.length > 0) throw new Error(`Unknown account export option: ${rest[0]}`);
|
|
466
|
+
writeJson(deps.stdout, await client.requestAccountExport());
|
|
467
|
+
return;
|
|
468
|
+
}
|
|
469
|
+
if (action === "deletion") {
|
|
470
|
+
const deletionAction = rest[0];
|
|
471
|
+
if (deletionAction === "cancel") {
|
|
472
|
+
if (rest.length > 1) throw new Error(`Unknown account deletion cancel option: ${rest[1]}`);
|
|
473
|
+
writeJson(deps.stdout, await client.cancelAccountDeletion());
|
|
474
|
+
return;
|
|
475
|
+
}
|
|
476
|
+
if (deletionAction === "request") {
|
|
477
|
+
if (rest[1] !== "--confirm") throw new Error("--confirm DELETE is required");
|
|
478
|
+
const confirmation = requiredFlagValue("--confirm", rest[2]);
|
|
479
|
+
if (rest.length > 3) throw new Error(`Unknown account deletion request option: ${rest[3]}`);
|
|
480
|
+
writeJson(deps.stdout, await client.requestAccountDeletion(confirmation));
|
|
481
|
+
return;
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
throw new Error(`Unknown account action: ${action || ""}`.trim());
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
async function runWallets(args, deps) {
|
|
488
|
+
const [action, ...rest] = args;
|
|
489
|
+
const client = await authenticatedClient(deps);
|
|
490
|
+
if (action === "list") {
|
|
491
|
+
if (rest.length > 0) throw new Error(`Unknown wallets list option: ${rest[0]}`);
|
|
492
|
+
writeJson(deps.stdout, await client.listWallets());
|
|
493
|
+
return;
|
|
494
|
+
}
|
|
495
|
+
if (action === "default") {
|
|
496
|
+
const walletAddress = requiredFlagValue("wallet address", rest[0]);
|
|
497
|
+
if (rest.length > 1) throw new Error(`Unknown wallets default option: ${rest[1]}`);
|
|
498
|
+
writeJson(deps.stdout, await client.setDefaultWallet(walletAddress));
|
|
499
|
+
return;
|
|
500
|
+
}
|
|
501
|
+
if (action === "visibility") {
|
|
502
|
+
const walletAddress = requiredFlagValue("wallet address", rest[0]);
|
|
503
|
+
if (rest[1] !== "--public") throw new Error("--public is required");
|
|
504
|
+
const isPublic = parseBoolean(requiredFlagValue("--public", rest[2]), "--public");
|
|
505
|
+
if (rest.length > 3) throw new Error(`Unknown wallets visibility option: ${rest[3]}`);
|
|
506
|
+
writeJson(deps.stdout, await client.setWalletVisibility(walletAddress, isPublic));
|
|
507
|
+
return;
|
|
508
|
+
}
|
|
509
|
+
if (action === "remove") {
|
|
510
|
+
const walletAddress = requiredFlagValue("wallet address", rest[0]);
|
|
511
|
+
if (rest[1] !== "--confirm" || rest[2] !== walletAddress) {
|
|
512
|
+
throw new Error("--confirm must exactly match the wallet address");
|
|
513
|
+
}
|
|
514
|
+
if (rest.length > 3) throw new Error(`Unknown wallets remove option: ${rest[3]}`);
|
|
515
|
+
writeJson(deps.stdout, await client.removeWallet(walletAddress));
|
|
516
|
+
return;
|
|
517
|
+
}
|
|
518
|
+
throw new Error(`Unknown wallets action: ${action || ""}`.trim());
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
async function runNfts(args, deps) {
|
|
522
|
+
const [action, ...rest] = args;
|
|
523
|
+
if (action !== "visibility") throw new Error(`Unknown nfts action: ${action || ""}`.trim());
|
|
524
|
+
if (rest[0] !== "--public") throw new Error("--public is required");
|
|
525
|
+
const enabled = parseBoolean(requiredFlagValue("--public", rest[1]), "--public");
|
|
526
|
+
if (rest.length > 2) throw new Error(`Unknown nfts visibility option: ${rest[2]}`);
|
|
527
|
+
writeJson(deps.stdout, await (await authenticatedClient(deps)).setNftExposure(enabled));
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
function renderError(error) {
|
|
531
|
+
if (error instanceof HiveApiError) {
|
|
532
|
+
const scopeText = error.requiredScopes.length > 0 ? ` Required scopes: ${error.requiredScopes.join(", ")}.` : "";
|
|
533
|
+
if (error.code === "auth_required") return error.message;
|
|
534
|
+
return `Hive API error ${error.status} (${error.code}): ${error.message}.${scopeText}`;
|
|
535
|
+
}
|
|
536
|
+
return error instanceof Error ? error.message : "Hive CLI failed.";
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
export async function main(argv, overrides = {}) {
|
|
540
|
+
const deps = defaultDeps(overrides);
|
|
541
|
+
const [command, ...rest] = argv;
|
|
542
|
+
try {
|
|
543
|
+
if (!command || command === "help" || command === "--help" || command === "-h") {
|
|
544
|
+
writeLine(deps.stdout, usage());
|
|
545
|
+
return 0;
|
|
546
|
+
}
|
|
547
|
+
if (command === "login") {
|
|
548
|
+
const parsed = parseLoginArgs(rest, deps.env);
|
|
549
|
+
if (parsed.help) writeLine(deps.stdout, loginUsage());
|
|
550
|
+
else await login(parsed.options, loginDeps(deps));
|
|
551
|
+
return 0;
|
|
552
|
+
}
|
|
553
|
+
if (command === "logout") {
|
|
554
|
+
const file = await clearConfig({ env: deps.env, homeDir: deps.homeDir, platform: deps.platform });
|
|
555
|
+
writeLine(deps.stdout, `Removed Hive CLI credential at ${file}.`);
|
|
556
|
+
return 0;
|
|
557
|
+
}
|
|
558
|
+
if (command === "whoami") {
|
|
559
|
+
if (rest.length > 0) throw new Error(`Unknown whoami option: ${rest[0]}`);
|
|
560
|
+
await runWhoami(deps);
|
|
561
|
+
return 0;
|
|
562
|
+
}
|
|
563
|
+
if (command === "capabilities") {
|
|
564
|
+
if (rest.length > 0) throw new Error(`Unknown capabilities option: ${rest[0]}`);
|
|
565
|
+
await runCapabilities(deps);
|
|
566
|
+
return 0;
|
|
567
|
+
}
|
|
568
|
+
if (command === "profile") {
|
|
569
|
+
await runProfile(rest, deps);
|
|
570
|
+
return 0;
|
|
571
|
+
}
|
|
572
|
+
if (command === "upload") {
|
|
573
|
+
await runUpload(rest, deps);
|
|
574
|
+
return 0;
|
|
575
|
+
}
|
|
576
|
+
if (command === "account") {
|
|
577
|
+
await runAccount(rest, deps);
|
|
578
|
+
return 0;
|
|
579
|
+
}
|
|
580
|
+
if (command === "wallets") {
|
|
581
|
+
await runWallets(rest, deps);
|
|
582
|
+
return 0;
|
|
583
|
+
}
|
|
584
|
+
if (command === "nfts") {
|
|
585
|
+
await runNfts(rest, deps);
|
|
586
|
+
return 0;
|
|
587
|
+
}
|
|
588
|
+
if (await runExtendedCommand(command, rest, deps)) return 0;
|
|
589
|
+
if (isResourceKind(command)) {
|
|
590
|
+
await runResource(command, rest, deps);
|
|
591
|
+
return 0;
|
|
592
|
+
}
|
|
593
|
+
throw new Error(`Unknown command: ${command}`);
|
|
594
|
+
} catch (error) {
|
|
595
|
+
writeLine(deps.stderr, renderError(error));
|
|
596
|
+
return error instanceof HiveApiError && error.code === "auth_required" ? 1 : 2;
|
|
597
|
+
}
|
|
598
|
+
}
|
package/src/config.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
import { promises as fs } from "node:fs";
|
|
4
|
+
import os from "node:os";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @typedef {object} StoredHiveConfig
|
|
9
|
+
* @property {string} apiBaseUrl
|
|
10
|
+
* @property {string} apiKey
|
|
11
|
+
* @property {string} createdAt
|
|
12
|
+
* @property {string} keyPrefix
|
|
13
|
+
* @property {string[]} scopes
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
export function configDirectory(env = process.env, platform = process.platform, homeDir = os.homedir()) {
|
|
17
|
+
if (env.HIVE_CONFIG_HOME) return env.HIVE_CONFIG_HOME;
|
|
18
|
+
if (platform === "darwin") return path.join(homeDir, "Library", "Application Support", "Hive");
|
|
19
|
+
if (platform === "win32") return path.join(env.APPDATA || path.join(homeDir, "AppData", "Roaming"), "Hive");
|
|
20
|
+
return path.join(env.XDG_CONFIG_HOME || path.join(homeDir, ".config"), "hive");
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function configPath(env = process.env, platform = process.platform, homeDir = os.homedir()) {
|
|
24
|
+
return path.join(configDirectory(env, platform, homeDir), "config.json");
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function configDeps(overrides = {}) {
|
|
28
|
+
const deps = {
|
|
29
|
+
env: process.env,
|
|
30
|
+
homeDir: os.homedir(),
|
|
31
|
+
platform: process.platform,
|
|
32
|
+
};
|
|
33
|
+
for (const [key, value] of Object.entries(overrides)) {
|
|
34
|
+
if (value !== undefined) deps[key] = value;
|
|
35
|
+
}
|
|
36
|
+
return deps;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function assertStoredConfig(value) {
|
|
40
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
41
|
+
throw new Error("Hive CLI config is invalid. Run hive login again.");
|
|
42
|
+
}
|
|
43
|
+
const record = /** @type {Record<string, unknown>} */ (value);
|
|
44
|
+
if (typeof record.apiBaseUrl !== "string" || typeof record.apiKey !== "string") {
|
|
45
|
+
throw new Error("Hive CLI config is missing credentials. Run hive login again.");
|
|
46
|
+
}
|
|
47
|
+
return /** @type {StoredHiveConfig} */ ({
|
|
48
|
+
apiBaseUrl: record.apiBaseUrl,
|
|
49
|
+
apiKey: record.apiKey,
|
|
50
|
+
createdAt: typeof record.createdAt === "string" ? record.createdAt : "",
|
|
51
|
+
keyPrefix: typeof record.keyPrefix === "string" ? record.keyPrefix : record.apiKey.slice(0, 16),
|
|
52
|
+
scopes: Array.isArray(record.scopes) ? record.scopes.filter(scope => typeof scope === "string") : [],
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export async function loadConfig(overrides = {}) {
|
|
57
|
+
const deps = configDeps(overrides);
|
|
58
|
+
const file = configPath(deps.env, deps.platform, deps.homeDir);
|
|
59
|
+
try {
|
|
60
|
+
const text = await fs.readFile(file, "utf8");
|
|
61
|
+
return assertStoredConfig(JSON.parse(text));
|
|
62
|
+
} catch (error) {
|
|
63
|
+
if (error && typeof error === "object" && "code" in error && error.code === "ENOENT") return null;
|
|
64
|
+
if (error instanceof SyntaxError) throw new Error("Hive CLI config is malformed. Run hive login again.");
|
|
65
|
+
throw error;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export async function saveCredential(credential, options, overrides = {}) {
|
|
70
|
+
const deps = configDeps(overrides);
|
|
71
|
+
const directory = configDirectory(deps.env, deps.platform, deps.homeDir);
|
|
72
|
+
await fs.mkdir(directory, { mode: 0o700, recursive: true });
|
|
73
|
+
const file = path.join(directory, "config.json");
|
|
74
|
+
const body = {
|
|
75
|
+
apiBaseUrl: options.apiBaseUrl,
|
|
76
|
+
apiKey: credential.key,
|
|
77
|
+
createdAt: new Date().toISOString(),
|
|
78
|
+
keyPrefix: credential.keyPrefix,
|
|
79
|
+
scopes: credential.scopes,
|
|
80
|
+
};
|
|
81
|
+
await fs.writeFile(file, `${JSON.stringify(body, null, 2)}\n`, { mode: 0o600 });
|
|
82
|
+
await fs.chmod(file, 0o600);
|
|
83
|
+
return file;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export async function clearConfig(overrides = {}) {
|
|
87
|
+
const deps = configDeps(overrides);
|
|
88
|
+
const file = configPath(deps.env, deps.platform, deps.homeDir);
|
|
89
|
+
await fs.rm(file, { force: true });
|
|
90
|
+
return file;
|
|
91
|
+
}
|