@nuelink/nuelink-cli 1.1.0 → 1.3.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 +61 -12
- package/package.json +4 -4
- package/src/cli.js +29 -0
- package/src/commands.js +497 -0
- package/src/config.js +121 -0
- package/src/http.js +176 -0
- package/src/output.js +183 -0
- package/src/postPayload.js +174 -0
- package/src/validation.js +186 -0
- package/src/main.js +0 -734
package/src/commands.js
ADDED
|
@@ -0,0 +1,497 @@
|
|
|
1
|
+
import { openAsBlob } from "node:fs";
|
|
2
|
+
import { readFile, stat } from "node:fs/promises";
|
|
3
|
+
import readline from "node:readline";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import chalk from "chalk";
|
|
6
|
+
|
|
7
|
+
import { clearApiKey, maskToken, readConfig, resolveApiKey, CONFIG_FILE, decryptApiKey, saveApiKey } from "./config.js";
|
|
8
|
+
import { brandPath, collectionPath, nuelinkRequest } from "./http.js";
|
|
9
|
+
import { buildPostPayload, parseCsv, VALID_PUBLISH_MODES } from "./postPayload.js";
|
|
10
|
+
import {
|
|
11
|
+
VALID_AUTOMATION_SUB_TYPES,
|
|
12
|
+
VALID_AUTOMATION_TYPES,
|
|
13
|
+
VALID_IMPORT_AS_TYPES,
|
|
14
|
+
normalizeEnum,
|
|
15
|
+
parseJsonString,
|
|
16
|
+
parseOptionalPositiveInteger,
|
|
17
|
+
parsePositiveInteger,
|
|
18
|
+
splitCsv,
|
|
19
|
+
validateHttpUrl,
|
|
20
|
+
validateMediaFile,
|
|
21
|
+
validateQueueSlots,
|
|
22
|
+
} from "./validation.js";
|
|
23
|
+
|
|
24
|
+
function extractApiKeyArg(args) {
|
|
25
|
+
const sanitizedArgs = [];
|
|
26
|
+
let apiKey;
|
|
27
|
+
|
|
28
|
+
for (let index = 0; index < args.length; index += 1) {
|
|
29
|
+
const arg = args[index];
|
|
30
|
+
|
|
31
|
+
if (arg === "--api-key" || arg === "--auth" || arg === "-a") {
|
|
32
|
+
const next = args[index + 1];
|
|
33
|
+
if (next && !next.startsWith("-")) {
|
|
34
|
+
apiKey = next;
|
|
35
|
+
index += 1;
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (arg.startsWith("--api-key=")) {
|
|
41
|
+
apiKey = arg.slice("--api-key=".length);
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (arg.startsWith("--auth=")) {
|
|
46
|
+
apiKey = arg.slice("--auth=".length);
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
sanitizedArgs.push(arg);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return { apiKey, sanitizedArgs };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async function readStdinText() {
|
|
57
|
+
return new Promise((resolve, reject) => {
|
|
58
|
+
let payload = "";
|
|
59
|
+
|
|
60
|
+
process.stdin.setEncoding("utf8");
|
|
61
|
+
process.stdin.on("data", (chunk) => {
|
|
62
|
+
payload += chunk;
|
|
63
|
+
});
|
|
64
|
+
process.stdin.on("end", () => {
|
|
65
|
+
resolve(payload.trim());
|
|
66
|
+
});
|
|
67
|
+
process.stdin.on("error", reject);
|
|
68
|
+
process.stdin.resume();
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async function promptHidden(promptText) {
|
|
73
|
+
return new Promise((resolve, reject) => {
|
|
74
|
+
const stdin = process.stdin;
|
|
75
|
+
const stdout = process.stdout;
|
|
76
|
+
const wasRaw = stdin.isTTY ? stdin.isRaw : false;
|
|
77
|
+
let value = "";
|
|
78
|
+
|
|
79
|
+
const cleanup = () => {
|
|
80
|
+
stdin.removeListener("keypress", onKeypress);
|
|
81
|
+
|
|
82
|
+
if (stdin.isTTY) {
|
|
83
|
+
stdin.setRawMode(Boolean(wasRaw));
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
stdout.write("\n");
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
const onKeypress = (character, key) => {
|
|
90
|
+
if (key?.ctrl && key.name === "c") {
|
|
91
|
+
cleanup();
|
|
92
|
+
reject(new Error("Authentication cancelled."));
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (key?.name === "return" || key?.name === "enter") {
|
|
97
|
+
cleanup();
|
|
98
|
+
resolve(value.trim());
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (key?.name === "backspace") {
|
|
103
|
+
value = value.slice(0, -1);
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (character) {
|
|
108
|
+
value += character;
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
stdout.write(promptText);
|
|
113
|
+
readline.emitKeypressEvents(stdin);
|
|
114
|
+
|
|
115
|
+
if (stdin.isTTY) {
|
|
116
|
+
stdin.setRawMode(true);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
stdin.resume();
|
|
120
|
+
stdin.on("keypress", onKeypress);
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
async function resolveLoginApiKey(options) {
|
|
125
|
+
if (options.apiKey) {
|
|
126
|
+
return options.apiKey.trim();
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (options.stdin || !process.stdin.isTTY) {
|
|
130
|
+
return readStdinText();
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return promptHidden("API key: ");
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function buildRequestOptions(program) {
|
|
137
|
+
const options = program.opts();
|
|
138
|
+
|
|
139
|
+
return {
|
|
140
|
+
dryRun: Boolean(options.dryRun),
|
|
141
|
+
outputMode: options.json ? "json" : "table",
|
|
142
|
+
quiet: Boolean(options.quiet),
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
async function executeRequest(program, runtimeApiKey, request) {
|
|
147
|
+
const requestOptions = buildRequestOptions(program);
|
|
148
|
+
const apiKey = requestOptions.dryRun ? undefined : await resolveApiKey(runtimeApiKey);
|
|
149
|
+
|
|
150
|
+
return nuelinkRequest({
|
|
151
|
+
apiKey,
|
|
152
|
+
...request,
|
|
153
|
+
...requestOptions,
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function withCommandErrorHandling(handler) {
|
|
158
|
+
return async (...args) => {
|
|
159
|
+
try {
|
|
160
|
+
await handler(...args);
|
|
161
|
+
} catch (error) {
|
|
162
|
+
console.error(chalk.red(error.message));
|
|
163
|
+
process.exitCode = 1;
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function registerCommands(program, runtimeApiKey, cliVersion) {
|
|
169
|
+
program
|
|
170
|
+
.name("nuelink-cli")
|
|
171
|
+
.version(cliVersion)
|
|
172
|
+
.description("Nuelink CLI for the Public API")
|
|
173
|
+
.option("-a, --api-key <apiKey>", "Use API key for this invocation only")
|
|
174
|
+
.option("--auth <apiKey>", "Deprecated alias for --api-key")
|
|
175
|
+
.option("--dry-run", "Print the validated request without sending it")
|
|
176
|
+
.option("--json", "Output raw JSON instead of tables for list commands")
|
|
177
|
+
.option("--quiet", "Suppress non-essential success banners");
|
|
178
|
+
|
|
179
|
+
program
|
|
180
|
+
.command("auth:login")
|
|
181
|
+
.description("Persist an API key to local config")
|
|
182
|
+
.option("--api-key <apiKey>", "API key to save (prefer stdin or interactive prompt)")
|
|
183
|
+
.option("--stdin", "Read API key from stdin")
|
|
184
|
+
.action(withCommandErrorHandling(async (options) => {
|
|
185
|
+
const apiKey = await resolveLoginApiKey(options);
|
|
186
|
+
|
|
187
|
+
if (!apiKey) {
|
|
188
|
+
throw new Error("Missing API key. Provide --stdin, pipe a key to auth:login, or enter it interactively.");
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
process.env.NUELINK_API_KEY = apiKey;
|
|
192
|
+
await saveApiKey(apiKey);
|
|
193
|
+
console.log(chalk.green("API key saved to local config."));
|
|
194
|
+
}));
|
|
195
|
+
|
|
196
|
+
program
|
|
197
|
+
.command("auth:clear")
|
|
198
|
+
.description("Remove the saved API key")
|
|
199
|
+
.action(withCommandErrorHandling(async () => {
|
|
200
|
+
await clearApiKey();
|
|
201
|
+
console.log(chalk.yellow("Saved API key removed."));
|
|
202
|
+
}));
|
|
203
|
+
|
|
204
|
+
program
|
|
205
|
+
.command("auth:status")
|
|
206
|
+
.description("Show where auth is currently resolved from")
|
|
207
|
+
.action(withCommandErrorHandling(async () => {
|
|
208
|
+
if (process.env.NUELINK_API_KEY) {
|
|
209
|
+
console.log(chalk.green(`Auth source: NUELINK_API_KEY (${maskToken(process.env.NUELINK_API_KEY)})`));
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
if (runtimeApiKey) {
|
|
214
|
+
console.log(chalk.green(`Auth source: runtime CLI flag (${maskToken(runtimeApiKey)})`));
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const config = await readConfig();
|
|
219
|
+
const storedApiKey = decryptApiKey(config);
|
|
220
|
+
if (storedApiKey) {
|
|
221
|
+
console.log(chalk.green(`Auth source: saved config (${maskToken(storedApiKey)})`));
|
|
222
|
+
|
|
223
|
+
try {
|
|
224
|
+
const fileStat = await stat(CONFIG_FILE);
|
|
225
|
+
console.log(chalk.blue(`Config path: ${CONFIG_FILE}`));
|
|
226
|
+
console.log(chalk.blue(`Last updated: ${fileStat.mtime.toISOString()}`));
|
|
227
|
+
} catch {
|
|
228
|
+
// Ignore non-critical stat failure.
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
console.log(chalk.yellow("No API key configured. Use auth:login to save one, or pass --api-key for a single command."));
|
|
235
|
+
}));
|
|
236
|
+
|
|
237
|
+
program
|
|
238
|
+
.command("me")
|
|
239
|
+
.description("Get current profile")
|
|
240
|
+
.action(withCommandErrorHandling(async () => {
|
|
241
|
+
await executeRequest(program, runtimeApiKey, { endpoint: "/me" });
|
|
242
|
+
}));
|
|
243
|
+
|
|
244
|
+
program
|
|
245
|
+
.command("brands")
|
|
246
|
+
.description("List brands")
|
|
247
|
+
.option("--per-page <number>", "Items per page", "25")
|
|
248
|
+
.option("--page <number>", "Page number", "1")
|
|
249
|
+
.action(withCommandErrorHandling(async (options) => {
|
|
250
|
+
await executeRequest(program, runtimeApiKey, {
|
|
251
|
+
endpoint: "/brands",
|
|
252
|
+
query: {
|
|
253
|
+
per_page: parsePositiveInteger(options.perPage, "per-page"),
|
|
254
|
+
page: parsePositiveInteger(options.page, "page"),
|
|
255
|
+
},
|
|
256
|
+
});
|
|
257
|
+
}));
|
|
258
|
+
|
|
259
|
+
program
|
|
260
|
+
.command("collections")
|
|
261
|
+
.description("List collections for a brand")
|
|
262
|
+
.requiredOption("--brand-id <id>", "Brand ID")
|
|
263
|
+
.option("--per-page <number>", "Items per page", "25")
|
|
264
|
+
.option("--page <number>", "Page number", "1")
|
|
265
|
+
.action(withCommandErrorHandling(async (options) => {
|
|
266
|
+
await executeRequest(program, runtimeApiKey, {
|
|
267
|
+
endpoint: `${brandPath(parsePositiveInteger(options.brandId, "brand-id"))}/collections`,
|
|
268
|
+
query: {
|
|
269
|
+
per_page: parsePositiveInteger(options.perPage, "per-page"),
|
|
270
|
+
page: parsePositiveInteger(options.page, "page"),
|
|
271
|
+
},
|
|
272
|
+
});
|
|
273
|
+
}));
|
|
274
|
+
|
|
275
|
+
program
|
|
276
|
+
.command("collections:create")
|
|
277
|
+
.description("Create a collection")
|
|
278
|
+
.requiredOption("--brand-id <id>", "Brand ID")
|
|
279
|
+
.requiredOption("--title <title>", "Collection title")
|
|
280
|
+
.option("--description <text>", "Collection description", "")
|
|
281
|
+
.option("--max-republish <number>", "Max republish")
|
|
282
|
+
.option("--channels <ids>", "Comma-separated channel IDs")
|
|
283
|
+
.option("--queues <slots>", "Comma-separated queue slots (e.g. Mon 10:10,Mon 12:12)")
|
|
284
|
+
.action(withCommandErrorHandling(async (options) => {
|
|
285
|
+
const channels = splitCsv(options.channels).map((id) => parsePositiveInteger(id, "channel-id"));
|
|
286
|
+
const queues = validateQueueSlots(splitCsv(options.queues));
|
|
287
|
+
|
|
288
|
+
await executeRequest(program, runtimeApiKey, {
|
|
289
|
+
method: "POST",
|
|
290
|
+
endpoint: `${brandPath(parsePositiveInteger(options.brandId, "brand-id"))}/collections`,
|
|
291
|
+
body: {
|
|
292
|
+
title: options.title,
|
|
293
|
+
description: options.description,
|
|
294
|
+
maxRepublish: parseOptionalPositiveInteger(options.maxRepublish, "max-republish"),
|
|
295
|
+
channels,
|
|
296
|
+
queues,
|
|
297
|
+
},
|
|
298
|
+
});
|
|
299
|
+
}));
|
|
300
|
+
|
|
301
|
+
program
|
|
302
|
+
.command("automations")
|
|
303
|
+
.description("List automations for a brand")
|
|
304
|
+
.requiredOption("--brand-id <id>", "Brand ID")
|
|
305
|
+
.option("--per-page <number>", "Items per page", "25")
|
|
306
|
+
.option("--page <number>", "Page number", "1")
|
|
307
|
+
.action(withCommandErrorHandling(async (options) => {
|
|
308
|
+
await executeRequest(program, runtimeApiKey, {
|
|
309
|
+
endpoint: `${brandPath(parsePositiveInteger(options.brandId, "brand-id"))}/automations`,
|
|
310
|
+
query: {
|
|
311
|
+
per_page: parsePositiveInteger(options.perPage, "per-page"),
|
|
312
|
+
page: parsePositiveInteger(options.page, "page"),
|
|
313
|
+
},
|
|
314
|
+
});
|
|
315
|
+
}));
|
|
316
|
+
|
|
317
|
+
program
|
|
318
|
+
.command("automations:create")
|
|
319
|
+
.description("Create an automation")
|
|
320
|
+
.requiredOption("--brand-id <id>", "Brand ID")
|
|
321
|
+
.requiredOption("--collection-id <id>", "Collection ID")
|
|
322
|
+
.requiredOption("--feed-url <url>", "Feed URL")
|
|
323
|
+
.requiredOption("--import-as-type <type>", "Import type, e.g. IMAGE")
|
|
324
|
+
.requiredOption("--sub-type <type>", "Sub type, e.g. RSS")
|
|
325
|
+
.requiredOption("--title <title>", "Automation title")
|
|
326
|
+
.option("--type <type>", "Automation type", "FEED")
|
|
327
|
+
.option("--dynamic-title <text>", "Dynamic title", "{{title}}")
|
|
328
|
+
.option("--dynamic-body <text>", "Dynamic body", "{title} {link}")
|
|
329
|
+
.option("--description <text>", "Automation description")
|
|
330
|
+
.action(withCommandErrorHandling(async (options) => {
|
|
331
|
+
await executeRequest(program, runtimeApiKey, {
|
|
332
|
+
method: "POST",
|
|
333
|
+
endpoint: `${brandPath(parsePositiveInteger(options.brandId, "brand-id"))}/automations`,
|
|
334
|
+
body: {
|
|
335
|
+
collectionId: parsePositiveInteger(options.collectionId, "collection-id"),
|
|
336
|
+
feedUrl: validateHttpUrl(options.feedUrl, "feed-url"),
|
|
337
|
+
importAsType: normalizeEnum(options.importAsType, "import-as-type", VALID_IMPORT_AS_TYPES),
|
|
338
|
+
subType: normalizeEnum(options.subType, "sub-type", VALID_AUTOMATION_SUB_TYPES),
|
|
339
|
+
title: options.title,
|
|
340
|
+
type: normalizeEnum(options.type, "type", VALID_AUTOMATION_TYPES),
|
|
341
|
+
dynamicTitle: options.dynamicTitle,
|
|
342
|
+
dynamicBody: options.dynamicBody,
|
|
343
|
+
description: options.description,
|
|
344
|
+
},
|
|
345
|
+
});
|
|
346
|
+
}));
|
|
347
|
+
|
|
348
|
+
program
|
|
349
|
+
.command("channels")
|
|
350
|
+
.description("List channels for a brand")
|
|
351
|
+
.requiredOption("--brand-id <id>", "Brand ID")
|
|
352
|
+
.option("--per-page <number>", "Items per page", "25")
|
|
353
|
+
.option("--page <number>", "Page number", "1")
|
|
354
|
+
.action(withCommandErrorHandling(async (options) => {
|
|
355
|
+
await executeRequest(program, runtimeApiKey, {
|
|
356
|
+
endpoint: `${brandPath(parsePositiveInteger(options.brandId, "brand-id"))}/channels`,
|
|
357
|
+
query: {
|
|
358
|
+
per_page: parsePositiveInteger(options.perPage, "per-page"),
|
|
359
|
+
page: parsePositiveInteger(options.page, "page"),
|
|
360
|
+
},
|
|
361
|
+
});
|
|
362
|
+
}));
|
|
363
|
+
|
|
364
|
+
program
|
|
365
|
+
.command("media")
|
|
366
|
+
.description("List media for a brand")
|
|
367
|
+
.requiredOption("--brand-id <id>", "Brand ID")
|
|
368
|
+
.option("--per-page <number>", "Items per page", "25")
|
|
369
|
+
.option("--page <number>", "Page number", "1")
|
|
370
|
+
.action(withCommandErrorHandling(async (options) => {
|
|
371
|
+
await executeRequest(program, runtimeApiKey, {
|
|
372
|
+
endpoint: `${brandPath(parsePositiveInteger(options.brandId, "brand-id"))}/media`,
|
|
373
|
+
query: {
|
|
374
|
+
per_page: parsePositiveInteger(options.perPage, "per-page"),
|
|
375
|
+
page: parsePositiveInteger(options.page, "page"),
|
|
376
|
+
},
|
|
377
|
+
});
|
|
378
|
+
}));
|
|
379
|
+
|
|
380
|
+
program
|
|
381
|
+
.command("media:upload")
|
|
382
|
+
.description("Upload a media file")
|
|
383
|
+
.requiredOption("--brand-id <id>", "Brand ID")
|
|
384
|
+
.requiredOption("--file <path>", "Path to local file")
|
|
385
|
+
.action(withCommandErrorHandling(async (options) => {
|
|
386
|
+
const brandId = parsePositiveInteger(options.brandId, "brand-id");
|
|
387
|
+
const fileStat = await stat(options.file);
|
|
388
|
+
const { mediaType, size } = validateMediaFile(options.file, fileStat);
|
|
389
|
+
const formData = new FormData();
|
|
390
|
+
const fileBlob = await openAsBlob(options.file, { type: mediaType });
|
|
391
|
+
formData.append("media", fileBlob, path.basename(options.file));
|
|
392
|
+
|
|
393
|
+
await executeRequest(program, runtimeApiKey, {
|
|
394
|
+
method: "POST",
|
|
395
|
+
endpoint: `${brandPath(brandId)}/media`,
|
|
396
|
+
multipart: formData,
|
|
397
|
+
dryRunBody: {
|
|
398
|
+
file: path.basename(options.file),
|
|
399
|
+
size,
|
|
400
|
+
type: mediaType,
|
|
401
|
+
},
|
|
402
|
+
});
|
|
403
|
+
}));
|
|
404
|
+
|
|
405
|
+
program
|
|
406
|
+
.command("posts")
|
|
407
|
+
.description("List posts in a collection")
|
|
408
|
+
.requiredOption("--brand-id <id>", "Brand ID")
|
|
409
|
+
.requiredOption("--collection-id <id>", "Collection ID")
|
|
410
|
+
.option("--per-page <number>", "Items per page", "25")
|
|
411
|
+
.option("--page <number>", "Page number", "1")
|
|
412
|
+
.action(withCommandErrorHandling(async (options) => {
|
|
413
|
+
await executeRequest(program, runtimeApiKey, {
|
|
414
|
+
endpoint: `${collectionPath(
|
|
415
|
+
parsePositiveInteger(options.brandId, "brand-id"),
|
|
416
|
+
parsePositiveInteger(options.collectionId, "collection-id"),
|
|
417
|
+
)}/posts`,
|
|
418
|
+
query: {
|
|
419
|
+
per_page: parsePositiveInteger(options.perPage, "per-page"),
|
|
420
|
+
page: parsePositiveInteger(options.page, "page"),
|
|
421
|
+
},
|
|
422
|
+
});
|
|
423
|
+
}));
|
|
424
|
+
|
|
425
|
+
program
|
|
426
|
+
.command("posts:add-json")
|
|
427
|
+
.description("Create a post using a JSON payload file")
|
|
428
|
+
.requiredOption("--brand-id <id>", "Brand ID")
|
|
429
|
+
.requiredOption("--collection-id <id>", "Collection ID")
|
|
430
|
+
.requiredOption("--payload <file>", "Path to JSON payload file")
|
|
431
|
+
.action(withCommandErrorHandling(async (options) => {
|
|
432
|
+
const payloadText = options.payload === "-"
|
|
433
|
+
? await readStdinText()
|
|
434
|
+
: await readFile(options.payload, "utf8");
|
|
435
|
+
const payload = parseJsonString(payloadText, "payload");
|
|
436
|
+
|
|
437
|
+
await executeRequest(program, runtimeApiKey, {
|
|
438
|
+
method: "POST",
|
|
439
|
+
endpoint: `${collectionPath(
|
|
440
|
+
parsePositiveInteger(options.brandId, "brand-id"),
|
|
441
|
+
parsePositiveInteger(options.collectionId, "collection-id"),
|
|
442
|
+
)}/posts`,
|
|
443
|
+
body: payload,
|
|
444
|
+
});
|
|
445
|
+
}));
|
|
446
|
+
|
|
447
|
+
program
|
|
448
|
+
.command("posts:create")
|
|
449
|
+
.description("Create a post using CLI options")
|
|
450
|
+
.requiredOption("--brand-id <id>", "Brand ID")
|
|
451
|
+
.requiredOption("--collection-id <id>", "Collection ID")
|
|
452
|
+
.option("--title <text>", "Post title")
|
|
453
|
+
.option("--caption <text>", "Post caption/body")
|
|
454
|
+
.option("--alt <text>", "Alt text")
|
|
455
|
+
.option("--link <url>", "Post link")
|
|
456
|
+
.option("--publish-mode <mode>", `Publish mode (${VALID_PUBLISH_MODES.join(", ")})`, "DRAFT")
|
|
457
|
+
.option("--publish-at <value>", "Publish datetime (YYYY-MM-DD HH:mm:ss or ISO) or null")
|
|
458
|
+
.option("--post-as-story <boolean>", "Post as story: true/false")
|
|
459
|
+
.option("--post-type <type>", "Post type override")
|
|
460
|
+
.option("--comment <text>", "First comment")
|
|
461
|
+
.option("--comment-delay <seconds>", "First comment delay seconds")
|
|
462
|
+
.option("--media-ids <ids>", "Comma-separated media IDs")
|
|
463
|
+
.option("--media-urls <urls>", "Comma-separated media URLs")
|
|
464
|
+
.option("--poll-question <text>", "Poll question")
|
|
465
|
+
.option("--poll-options <items>", "Comma-separated poll options")
|
|
466
|
+
.option("--poll-period <value>", "Poll period (e.g. 3d)")
|
|
467
|
+
.option("--poll-correct-option-index <number>", "Poll correct option index")
|
|
468
|
+
.option("--poll-explanation <text>", "Poll explanation")
|
|
469
|
+
.option("--instagram-collab <username>", "Instagram collab username")
|
|
470
|
+
.option("--instagram-location-id <id>", "Instagram location ID")
|
|
471
|
+
.option("--instagram-location-name <name>", "Instagram location name")
|
|
472
|
+
.option("--instagram-trial-reel <mode>", "Instagram trial reel mode")
|
|
473
|
+
.option("--instagram-share-to-feed <boolean>", "Instagram share to feed true/false")
|
|
474
|
+
.option("--facebook-collab <username>", "Facebook collab username")
|
|
475
|
+
.option("--facebook-location-id <id>", "Facebook location ID")
|
|
476
|
+
.option("--facebook-location-name <name>", "Facebook location name")
|
|
477
|
+
.option("--tiktok-send-to-inbox <boolean>", "TikTok send to inbox true/false")
|
|
478
|
+
.option("--youtube-tags <tags>", "Comma-separated YouTube tags")
|
|
479
|
+
.option("--youtube-playlists <json>", "YouTube playlists JSON string")
|
|
480
|
+
.action(withCommandErrorHandling(async (options) => {
|
|
481
|
+
const payload = buildPostPayload(options);
|
|
482
|
+
|
|
483
|
+
await executeRequest(program, runtimeApiKey, {
|
|
484
|
+
method: "POST",
|
|
485
|
+
endpoint: `${collectionPath(
|
|
486
|
+
parsePositiveInteger(options.brandId, "brand-id"),
|
|
487
|
+
parsePositiveInteger(options.collectionId, "collection-id"),
|
|
488
|
+
)}/posts`,
|
|
489
|
+
body: payload,
|
|
490
|
+
});
|
|
491
|
+
}));
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
export {
|
|
495
|
+
extractApiKeyArg,
|
|
496
|
+
registerCommands,
|
|
497
|
+
};
|
package/src/config.js
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import crypto from "node:crypto";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { readFile, writeFile, mkdir, chmod } from "node:fs/promises";
|
|
5
|
+
|
|
6
|
+
const CONFIG_DIR = path.join(os.homedir(), ".nuelink-cli");
|
|
7
|
+
const CONFIG_FILE = path.join(CONFIG_DIR, "config.json");
|
|
8
|
+
const CONFIG_ENCRYPTION_SALT = "nuelink-cli-config-salt-v1";
|
|
9
|
+
const CONFIG_ENCRYPTION_PASSPHRASE = "nuelink-cli::config::v1::auth";
|
|
10
|
+
|
|
11
|
+
async function readConfig() {
|
|
12
|
+
try {
|
|
13
|
+
const content = await readFile(CONFIG_FILE, "utf8");
|
|
14
|
+
return JSON.parse(content);
|
|
15
|
+
} catch {
|
|
16
|
+
return {};
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function getConfigCipherKey() {
|
|
21
|
+
return crypto.scryptSync(CONFIG_ENCRYPTION_PASSPHRASE, CONFIG_ENCRYPTION_SALT, 32);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function encryptApiKey(apiKey) {
|
|
25
|
+
const iv = crypto.randomBytes(12);
|
|
26
|
+
const cipher = crypto.createCipheriv("aes-256-gcm", getConfigCipherKey(), iv);
|
|
27
|
+
const encrypted = Buffer.concat([cipher.update(apiKey, "utf8"), cipher.final()]);
|
|
28
|
+
const authTag = cipher.getAuthTag();
|
|
29
|
+
|
|
30
|
+
return {
|
|
31
|
+
version: 1,
|
|
32
|
+
encryptedApiKey: encrypted.toString("base64"),
|
|
33
|
+
iv: iv.toString("base64"),
|
|
34
|
+
authTag: authTag.toString("base64"),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function decryptApiKey(config) {
|
|
39
|
+
if (config?.encryptedApiKey && config?.iv && config?.authTag) {
|
|
40
|
+
const decipher = crypto.createDecipheriv(
|
|
41
|
+
"aes-256-gcm",
|
|
42
|
+
getConfigCipherKey(),
|
|
43
|
+
Buffer.from(config.iv, "base64"),
|
|
44
|
+
);
|
|
45
|
+
decipher.setAuthTag(Buffer.from(config.authTag, "base64"));
|
|
46
|
+
|
|
47
|
+
const decrypted = Buffer.concat([
|
|
48
|
+
decipher.update(Buffer.from(config.encryptedApiKey, "base64")),
|
|
49
|
+
decipher.final(),
|
|
50
|
+
]);
|
|
51
|
+
|
|
52
|
+
return decrypted.toString("utf8");
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (config?.apiKey) {
|
|
56
|
+
return config.apiKey;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return undefined;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async function writeConfig(config) {
|
|
63
|
+
await mkdir(CONFIG_DIR, { recursive: true });
|
|
64
|
+
await writeFile(CONFIG_FILE, JSON.stringify(config, null, 2), "utf8");
|
|
65
|
+
|
|
66
|
+
try {
|
|
67
|
+
await chmod(CONFIG_FILE, 0o600);
|
|
68
|
+
} catch {
|
|
69
|
+
// Ignore on platforms where chmod permissions are not meaningful.
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async function saveApiKey(apiKey) {
|
|
74
|
+
await writeConfig(encryptApiKey(apiKey));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async function clearApiKey() {
|
|
78
|
+
await writeConfig({});
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async function resolveApiKey(runtimeApiKey) {
|
|
82
|
+
if (process.env.NUELINK_API_KEY) {
|
|
83
|
+
return process.env.NUELINK_API_KEY;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (runtimeApiKey) {
|
|
87
|
+
process.env.NUELINK_API_KEY = runtimeApiKey;
|
|
88
|
+
return runtimeApiKey;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const config = await readConfig();
|
|
92
|
+
const storedApiKey = decryptApiKey(config);
|
|
93
|
+
|
|
94
|
+
if (storedApiKey && config.apiKey) {
|
|
95
|
+
await saveApiKey(storedApiKey);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (storedApiKey) {
|
|
99
|
+
process.env.NUELINK_API_KEY = storedApiKey;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return storedApiKey;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function maskToken(token) {
|
|
106
|
+
if (!token || token.length < 8) {
|
|
107
|
+
return "(set)";
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return `${token.slice(0, 4)}...${token.slice(-4)}`;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export {
|
|
114
|
+
CONFIG_FILE,
|
|
115
|
+
clearApiKey,
|
|
116
|
+
decryptApiKey,
|
|
117
|
+
maskToken,
|
|
118
|
+
readConfig,
|
|
119
|
+
resolveApiKey,
|
|
120
|
+
saveApiKey,
|
|
121
|
+
};
|