@nuelink/nuelink-cli 1.4.0 → 1.4.2
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/CHANGELOG.md +13 -0
- package/LICENSE +21 -0
- package/README.md +51 -29
- package/SECURITY.md +70 -0
- package/package.json +22 -4
- package/src/cli.js +37 -13
- package/src/commands.js +109 -67
- package/src/config.js +107 -38
- package/src/errors.js +47 -0
- package/src/http.js +95 -54
- package/src/postPayload.js +187 -175
- package/src/validation.js +53 -14
- package/test/cli.test.mjs +1235 -0
- package/test/mock-fetch.cjs +261 -0
- package/test/mock-fetch.mjs +261 -0
package/src/commands.js
CHANGED
|
@@ -4,8 +4,9 @@ import path from "node:path";
|
|
|
4
4
|
import chalk from "chalk";
|
|
5
5
|
|
|
6
6
|
import { clearApiKey, maskToken, readConfig, resolveApiKey, CONFIG_FILE, decryptApiKey, saveApiKey } from "./config.js";
|
|
7
|
+
import { CliError, printCliError } from "./errors.js";
|
|
7
8
|
import { brandPath, collectionPath, nuelinkRequest } from "./http.js";
|
|
8
|
-
import { buildPostPayload, parseCsv, validateRawPostPayload, VALID_PUBLISH_MODES } from "./postPayload.js";
|
|
9
|
+
import { buildPostPayload, parseCsv, validateRawPostPayload, VALID_PUBLISH_MODES } from "./postPayload.js";
|
|
9
10
|
import {
|
|
10
11
|
VALID_AUTOMATION_SUB_TYPES,
|
|
11
12
|
VALID_AUTOMATION_TYPES,
|
|
@@ -19,8 +20,9 @@ import {
|
|
|
19
20
|
parsePositiveInteger,
|
|
20
21
|
splitCsv,
|
|
21
22
|
validateHttpUrl,
|
|
22
|
-
validateMediaFile,
|
|
23
|
+
validateMediaFile,
|
|
23
24
|
validateQueueSlots,
|
|
25
|
+
validateRequiredString,
|
|
24
26
|
} from "./validation.js";
|
|
25
27
|
|
|
26
28
|
function extractApiKeyArg(args) {
|
|
@@ -138,7 +140,7 @@ async function promptHidden(promptText) {
|
|
|
138
140
|
});
|
|
139
141
|
}
|
|
140
142
|
|
|
141
|
-
async function resolveLoginApiKey(options, fallbackApiKey) {
|
|
143
|
+
async function resolveLoginApiKey(options, fallbackApiKey) {
|
|
142
144
|
if (options.apiKey || fallbackApiKey) {
|
|
143
145
|
return String(options.apiKey || fallbackApiKey).trim();
|
|
144
146
|
}
|
|
@@ -148,8 +150,8 @@ async function resolveLoginApiKey(options, fallbackApiKey) {
|
|
|
148
150
|
}
|
|
149
151
|
|
|
150
152
|
return promptHidden("API key: ");
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
+
}
|
|
154
|
+
|
|
153
155
|
function buildRequestOptions(program) {
|
|
154
156
|
const options = program.opts();
|
|
155
157
|
|
|
@@ -178,12 +180,15 @@ async function executeRequest(program, runtimeApiKey, request) {
|
|
|
178
180
|
});
|
|
179
181
|
}
|
|
180
182
|
|
|
181
|
-
function withCommandErrorHandling(handler) {
|
|
183
|
+
function withCommandErrorHandling(program, handler) {
|
|
182
184
|
return async (...args) => {
|
|
183
185
|
try {
|
|
184
186
|
await handler(...args);
|
|
185
187
|
} catch (error) {
|
|
186
|
-
|
|
188
|
+
printCliError(error, {
|
|
189
|
+
json: Boolean(program.opts().json),
|
|
190
|
+
fallbackCode: "VALIDATION_ERROR",
|
|
191
|
+
});
|
|
187
192
|
process.exitCode = 1;
|
|
188
193
|
}
|
|
189
194
|
};
|
|
@@ -196,8 +201,8 @@ function registerCommands(program, runtimeApiKey, cliVersion) {
|
|
|
196
201
|
.description("Nuelink CLI for the Public API")
|
|
197
202
|
.option("-a, --api-key <apiKey>", "Use API key for this invocation only")
|
|
198
203
|
.option("--auth <apiKey>", "Deprecated alias for --api-key")
|
|
199
|
-
.option("--dry-run", "Print the validated request without sending it")
|
|
200
|
-
.option("--json", "Output
|
|
204
|
+
.option("--dry-run", "Print the validated request without sending it")
|
|
205
|
+
.option("--json", "Output machine-readable JSON for successes and failures")
|
|
201
206
|
.option("--quiet", "Suppress non-essential success banners");
|
|
202
207
|
|
|
203
208
|
program
|
|
@@ -205,7 +210,7 @@ function registerCommands(program, runtimeApiKey, cliVersion) {
|
|
|
205
210
|
.description("Persist an API key to local config")
|
|
206
211
|
.option("--api-key <apiKey>", "API key to save (prefer stdin or interactive prompt)")
|
|
207
212
|
.option("--stdin", "Read API key from stdin")
|
|
208
|
-
.action(withCommandErrorHandling(async (options) => {
|
|
213
|
+
.action(withCommandErrorHandling(program, async (options) => {
|
|
209
214
|
const apiKey = await resolveLoginApiKey(options, runtimeApiKey);
|
|
210
215
|
|
|
211
216
|
if (!apiKey) {
|
|
@@ -214,55 +219,86 @@ function registerCommands(program, runtimeApiKey, cliVersion) {
|
|
|
214
219
|
|
|
215
220
|
process.env.NUELINK_API_KEY = apiKey;
|
|
216
221
|
await saveApiKey(apiKey);
|
|
217
|
-
|
|
222
|
+
if (program.opts().json) {
|
|
223
|
+
console.log(JSON.stringify({ success: true, source: "saved_config" }));
|
|
224
|
+
} else {
|
|
225
|
+
console.log(chalk.green("API key saved to local config."));
|
|
226
|
+
}
|
|
218
227
|
}));
|
|
219
228
|
|
|
220
229
|
program
|
|
221
230
|
.command("auth:clear")
|
|
222
231
|
.description("Remove the saved API key")
|
|
223
|
-
.action(withCommandErrorHandling(async () => {
|
|
232
|
+
.action(withCommandErrorHandling(program, async () => {
|
|
224
233
|
await clearApiKey();
|
|
225
|
-
|
|
234
|
+
if (program.opts().json) {
|
|
235
|
+
console.log(JSON.stringify({ success: true, cleared: true }));
|
|
236
|
+
} else {
|
|
237
|
+
console.log(chalk.yellow("Saved API key removed."));
|
|
238
|
+
}
|
|
226
239
|
}));
|
|
227
240
|
|
|
228
241
|
program
|
|
229
242
|
.command("auth:status")
|
|
230
243
|
.description("Show where auth is currently resolved from")
|
|
231
|
-
.action(withCommandErrorHandling(async () => {
|
|
244
|
+
.action(withCommandErrorHandling(program, async () => {
|
|
232
245
|
if (runtimeApiKey) {
|
|
233
|
-
|
|
246
|
+
if (program.opts().json) {
|
|
247
|
+
console.log(JSON.stringify({ authenticated: true, source: "runtime_flag", token: maskToken(runtimeApiKey) }));
|
|
248
|
+
} else {
|
|
249
|
+
console.log(chalk.green(`Auth source: runtime CLI flag (${maskToken(runtimeApiKey)})`));
|
|
250
|
+
}
|
|
234
251
|
return;
|
|
235
252
|
}
|
|
236
253
|
|
|
237
254
|
if (process.env.NUELINK_API_KEY) {
|
|
238
|
-
|
|
255
|
+
if (program.opts().json) {
|
|
256
|
+
console.log(JSON.stringify({ authenticated: true, source: "environment", token: maskToken(process.env.NUELINK_API_KEY) }));
|
|
257
|
+
} else {
|
|
258
|
+
console.log(chalk.green(`Auth source: NUELINK_API_KEY (${maskToken(process.env.NUELINK_API_KEY)})`));
|
|
259
|
+
}
|
|
239
260
|
return;
|
|
240
261
|
}
|
|
241
262
|
|
|
242
263
|
const config = await readConfig();
|
|
243
264
|
const storedApiKey = decryptApiKey(config);
|
|
244
265
|
if (storedApiKey) {
|
|
245
|
-
|
|
266
|
+
let lastUpdated;
|
|
246
267
|
|
|
247
268
|
try {
|
|
248
269
|
const fileStat = await stat(CONFIG_FILE);
|
|
249
|
-
|
|
250
|
-
console.log(chalk.blue(`Last updated: ${fileStat.mtime.toISOString()}`));
|
|
270
|
+
lastUpdated = fileStat.mtime.toISOString();
|
|
251
271
|
} catch {
|
|
252
272
|
// Ignore non-critical stat failure.
|
|
253
273
|
}
|
|
254
274
|
|
|
275
|
+
if (program.opts().json) {
|
|
276
|
+
console.log(JSON.stringify({
|
|
277
|
+
authenticated: true,
|
|
278
|
+
source: "saved_config",
|
|
279
|
+
token: maskToken(storedApiKey),
|
|
280
|
+
configPath: CONFIG_FILE,
|
|
281
|
+
lastUpdated: lastUpdated || null,
|
|
282
|
+
}));
|
|
283
|
+
} else {
|
|
284
|
+
console.log(chalk.green(`Auth source: saved config (${maskToken(storedApiKey)})`));
|
|
285
|
+
console.log(chalk.blue(`Config path: ${CONFIG_FILE}`));
|
|
286
|
+
if (lastUpdated) console.log(chalk.blue(`Last updated: ${lastUpdated}`));
|
|
287
|
+
}
|
|
288
|
+
|
|
255
289
|
return;
|
|
256
290
|
}
|
|
257
291
|
|
|
258
|
-
|
|
259
|
-
|
|
292
|
+
throw new CliError(
|
|
293
|
+
"AUTH_MISSING",
|
|
294
|
+
"No API key configured. Use auth:login to save one, or pass --api-key for a single command.",
|
|
295
|
+
);
|
|
260
296
|
}));
|
|
261
297
|
|
|
262
298
|
program
|
|
263
299
|
.command("me")
|
|
264
300
|
.description("Get current profile")
|
|
265
|
-
.action(withCommandErrorHandling(async () => {
|
|
301
|
+
.action(withCommandErrorHandling(program, async () => {
|
|
266
302
|
await executeRequest(program, runtimeApiKey, { endpoint: "/me" });
|
|
267
303
|
}));
|
|
268
304
|
|
|
@@ -271,7 +307,7 @@ function registerCommands(program, runtimeApiKey, cliVersion) {
|
|
|
271
307
|
.description("List brands")
|
|
272
308
|
.option("--per-page <number>", "Items per page", "25")
|
|
273
309
|
.option("--page <number>", "Page number", "1")
|
|
274
|
-
.action(withCommandErrorHandling(async (options) => {
|
|
310
|
+
.action(withCommandErrorHandling(program, async (options) => {
|
|
275
311
|
await executeRequest(program, runtimeApiKey, {
|
|
276
312
|
endpoint: "/brands",
|
|
277
313
|
query: {
|
|
@@ -287,7 +323,7 @@ function registerCommands(program, runtimeApiKey, cliVersion) {
|
|
|
287
323
|
.requiredOption("--brand-id <id>", "Brand ID")
|
|
288
324
|
.option("--per-page <number>", "Items per page", "25")
|
|
289
325
|
.option("--page <number>", "Page number", "1")
|
|
290
|
-
.action(withCommandErrorHandling(async (options) => {
|
|
326
|
+
.action(withCommandErrorHandling(program, async (options) => {
|
|
291
327
|
await executeRequest(program, runtimeApiKey, {
|
|
292
328
|
endpoint: `${brandPath(parsePositiveInteger(options.brandId, "brand-id"))}/collections`,
|
|
293
329
|
query: {
|
|
@@ -306,7 +342,7 @@ function registerCommands(program, runtimeApiKey, cliVersion) {
|
|
|
306
342
|
.option("--max-republish <number>", "Max republish")
|
|
307
343
|
.option("--channels <ids>", "Comma-separated channel IDs")
|
|
308
344
|
.option("--queues <slots>", "Comma-separated queue slots (e.g. Mon 10:10,Mon 12:12)")
|
|
309
|
-
.action(withCommandErrorHandling(async (options) => {
|
|
345
|
+
.action(withCommandErrorHandling(program, async (options) => {
|
|
310
346
|
const channels = splitCsv(options.channels).map((id) => parsePositiveInteger(id, "channel-id"));
|
|
311
347
|
const queues = validateQueueSlots(splitCsv(options.queues));
|
|
312
348
|
|
|
@@ -314,7 +350,7 @@ function registerCommands(program, runtimeApiKey, cliVersion) {
|
|
|
314
350
|
method: "POST",
|
|
315
351
|
endpoint: `${brandPath(parsePositiveInteger(options.brandId, "brand-id"))}/collections`,
|
|
316
352
|
body: {
|
|
317
|
-
title:
|
|
353
|
+
title: validateRequiredString(options.title, 255, "title"),
|
|
318
354
|
description: validateMaxLength(options.description, 1000, "description"),
|
|
319
355
|
maxRepublish: parseOptionalNonNegativeInteger(options.maxRepublish, "max-republish"),
|
|
320
356
|
channels,
|
|
@@ -328,9 +364,9 @@ function registerCommands(program, runtimeApiKey, cliVersion) {
|
|
|
328
364
|
.description("List automations for a brand")
|
|
329
365
|
.requiredOption("--brand-id <id>", "Brand ID")
|
|
330
366
|
.option("--per-page <number>", "Items per page", "25")
|
|
331
|
-
.option("--page <number>", "Page number", "1")
|
|
332
|
-
.action(withCommandErrorHandling(async (options) => {
|
|
333
|
-
await executeRequest(program, runtimeApiKey, {
|
|
367
|
+
.option("--page <number>", "Page number", "1")
|
|
368
|
+
.action(withCommandErrorHandling(program, async (options) => {
|
|
369
|
+
await executeRequest(program, runtimeApiKey, {
|
|
334
370
|
endpoint: `${brandPath(parsePositiveInteger(options.brandId, "brand-id"))}/automations`,
|
|
335
371
|
query: {
|
|
336
372
|
per_page: parseIntegerInRange(options.perPage, "per-page", 1, 100),
|
|
@@ -354,28 +390,28 @@ function registerCommands(program, runtimeApiKey, cliVersion) {
|
|
|
354
390
|
.option("--description <text>", "Automation description")
|
|
355
391
|
.option("--load-old-posts <boolean>", "Import existing feed items: true/false")
|
|
356
392
|
.option("--add-posts-as-draft <boolean>", "Create imported posts as drafts: true/false")
|
|
357
|
-
.option("--refresh-rate <hours>", "Refresh interval (24, 12, 6, or 1 hours)")
|
|
358
|
-
.action(withCommandErrorHandling(async (options) => {
|
|
359
|
-
await executeRequest(program, runtimeApiKey, {
|
|
360
|
-
method: "POST",
|
|
361
|
-
endpoint: `${brandPath(parsePositiveInteger(options.brandId, "brand-id"))}/automations`,
|
|
362
|
-
body: {
|
|
363
|
-
collectionId: parsePositiveInteger(options.collectionId, "collection-id"),
|
|
364
|
-
feedUrl: validateHttpUrl(options.feedUrl, "feed-url"),
|
|
365
|
-
importAsType: normalizeEnum(options.importAsType, "import-as-type", VALID_IMPORT_AS_TYPES),
|
|
366
|
-
subType: normalizeEnum(options.subType, "sub-type", VALID_AUTOMATION_SUB_TYPES),
|
|
367
|
-
title:
|
|
368
|
-
type: normalizeEnum(options.type, "type", VALID_AUTOMATION_TYPES),
|
|
369
|
-
dynamicTitle: validateMaxLength(options.dynamicTitle, 255, "dynamic-title"),
|
|
370
|
-
dynamicBody: validateMaxLength(options.dynamicBody, 255, "dynamic-body"),
|
|
371
|
-
description: options.description,
|
|
372
|
-
loadOldPosts: parseBooleanStrict(options.loadOldPosts, "load-old-posts"),
|
|
373
|
-
addPostsAsDraft: parseBooleanStrict(options.addPostsAsDraft, "add-posts-as-draft"),
|
|
374
|
-
refreshRate: options.refreshRate === undefined
|
|
375
|
-
? undefined
|
|
376
|
-
: Number(normalizeEnum(options.refreshRate, "refresh-rate", ["24", "12", "6", "1"])),
|
|
377
|
-
},
|
|
378
|
-
});
|
|
393
|
+
.option("--refresh-rate <hours>", "Refresh interval (24, 12, 6, or 1 hours)")
|
|
394
|
+
.action(withCommandErrorHandling(program, async (options) => {
|
|
395
|
+
await executeRequest(program, runtimeApiKey, {
|
|
396
|
+
method: "POST",
|
|
397
|
+
endpoint: `${brandPath(parsePositiveInteger(options.brandId, "brand-id"))}/automations`,
|
|
398
|
+
body: {
|
|
399
|
+
collectionId: parsePositiveInteger(options.collectionId, "collection-id"),
|
|
400
|
+
feedUrl: validateHttpUrl(options.feedUrl, "feed-url"),
|
|
401
|
+
importAsType: normalizeEnum(options.importAsType, "import-as-type", VALID_IMPORT_AS_TYPES),
|
|
402
|
+
subType: normalizeEnum(options.subType, "sub-type", VALID_AUTOMATION_SUB_TYPES),
|
|
403
|
+
title: validateRequiredString(options.title, 255, "title"),
|
|
404
|
+
type: normalizeEnum(options.type, "type", VALID_AUTOMATION_TYPES),
|
|
405
|
+
dynamicTitle: validateMaxLength(options.dynamicTitle, 255, "dynamic-title"),
|
|
406
|
+
dynamicBody: validateMaxLength(options.dynamicBody, 255, "dynamic-body"),
|
|
407
|
+
description: options.description,
|
|
408
|
+
loadOldPosts: parseBooleanStrict(options.loadOldPosts, "load-old-posts"),
|
|
409
|
+
addPostsAsDraft: parseBooleanStrict(options.addPostsAsDraft, "add-posts-as-draft"),
|
|
410
|
+
refreshRate: options.refreshRate === undefined
|
|
411
|
+
? undefined
|
|
412
|
+
: Number(normalizeEnum(options.refreshRate, "refresh-rate", ["24", "12", "6", "1"])),
|
|
413
|
+
},
|
|
414
|
+
});
|
|
379
415
|
}));
|
|
380
416
|
|
|
381
417
|
program
|
|
@@ -384,7 +420,7 @@ function registerCommands(program, runtimeApiKey, cliVersion) {
|
|
|
384
420
|
.requiredOption("--brand-id <id>", "Brand ID")
|
|
385
421
|
.option("--per-page <number>", "Items per page", "25")
|
|
386
422
|
.option("--page <number>", "Page number", "1")
|
|
387
|
-
.action(withCommandErrorHandling(async (options) => {
|
|
423
|
+
.action(withCommandErrorHandling(program, async (options) => {
|
|
388
424
|
await executeRequest(program, runtimeApiKey, {
|
|
389
425
|
endpoint: `${brandPath(parsePositiveInteger(options.brandId, "brand-id"))}/channels`,
|
|
390
426
|
query: {
|
|
@@ -401,7 +437,7 @@ function registerCommands(program, runtimeApiKey, cliVersion) {
|
|
|
401
437
|
.option("--type <type>", `Media type filter (${VALID_MEDIA_FILTER_TYPES.join(", ")})`)
|
|
402
438
|
.option("--per-page <number>", "Items per page", "25")
|
|
403
439
|
.option("--page <number>", "Page number", "1")
|
|
404
|
-
.action(withCommandErrorHandling(async (options) => {
|
|
440
|
+
.action(withCommandErrorHandling(program, async (options) => {
|
|
405
441
|
await executeRequest(program, runtimeApiKey, {
|
|
406
442
|
endpoint: `${brandPath(parsePositiveInteger(options.brandId, "brand-id"))}/media`,
|
|
407
443
|
query: {
|
|
@@ -419,19 +455,24 @@ function registerCommands(program, runtimeApiKey, cliVersion) {
|
|
|
419
455
|
.description("Upload a media file")
|
|
420
456
|
.requiredOption("--brand-id <id>", "Brand ID")
|
|
421
457
|
.requiredOption("--file <path>", "Path to local file")
|
|
422
|
-
.action(withCommandErrorHandling(async (options) => {
|
|
423
|
-
const brandId = parsePositiveInteger(options.brandId, "brand-id");
|
|
424
|
-
const fileStat = await stat(options.file);
|
|
425
|
-
const { mediaType, size } = validateMediaFile(options.file, fileStat);
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
458
|
+
.action(withCommandErrorHandling(program, async (options) => {
|
|
459
|
+
const brandId = parsePositiveInteger(options.brandId, "brand-id");
|
|
460
|
+
const fileStat = await stat(options.file);
|
|
461
|
+
const { mediaType, size } = validateMediaFile(options.file, fileStat);
|
|
462
|
+
let multipart = true;
|
|
463
|
+
|
|
464
|
+
if (!program.opts().dryRun) {
|
|
465
|
+
const formData = new FormData();
|
|
466
|
+
const fileBuffer = await readFile(options.file);
|
|
467
|
+
const fileBlob = new Blob([fileBuffer], { type: mediaType });
|
|
468
|
+
formData.append("media", fileBlob, path.basename(options.file));
|
|
469
|
+
multipart = formData;
|
|
470
|
+
}
|
|
430
471
|
|
|
431
472
|
await executeRequest(program, runtimeApiKey, {
|
|
432
473
|
method: "POST",
|
|
433
474
|
endpoint: `${brandPath(brandId)}/media`,
|
|
434
|
-
multipart
|
|
475
|
+
multipart,
|
|
435
476
|
dryRunBody: {
|
|
436
477
|
file: path.basename(options.file),
|
|
437
478
|
size,
|
|
@@ -447,7 +488,7 @@ function registerCommands(program, runtimeApiKey, cliVersion) {
|
|
|
447
488
|
.requiredOption("--collection-id <id>", "Collection ID")
|
|
448
489
|
.option("--per-page <number>", "Items per page", "25")
|
|
449
490
|
.option("--page <number>", "Page number", "1")
|
|
450
|
-
.action(withCommandErrorHandling(async (options) => {
|
|
491
|
+
.action(withCommandErrorHandling(program, async (options) => {
|
|
451
492
|
await executeRequest(program, runtimeApiKey, {
|
|
452
493
|
endpoint: `${collectionPath(
|
|
453
494
|
parsePositiveInteger(options.brandId, "brand-id"),
|
|
@@ -466,11 +507,11 @@ function registerCommands(program, runtimeApiKey, cliVersion) {
|
|
|
466
507
|
.requiredOption("--brand-id <id>", "Brand ID")
|
|
467
508
|
.requiredOption("--collection-id <id>", "Collection ID")
|
|
468
509
|
.requiredOption("--payload <file>", "Path to JSON payload file")
|
|
469
|
-
.action(withCommandErrorHandling(async (options) => {
|
|
510
|
+
.action(withCommandErrorHandling(program, async (options) => {
|
|
470
511
|
const payloadText = options.payload === "-"
|
|
471
512
|
? await readStdinText()
|
|
472
513
|
: await readFile(options.payload, "utf8");
|
|
473
|
-
const payload = validateRawPostPayload(parseJsonString(payloadText, "payload"));
|
|
514
|
+
const payload = validateRawPostPayload(parseJsonString(payloadText, "payload"));
|
|
474
515
|
|
|
475
516
|
await executeRequest(program, runtimeApiKey, {
|
|
476
517
|
method: "POST",
|
|
@@ -491,6 +532,7 @@ function registerCommands(program, runtimeApiKey, cliVersion) {
|
|
|
491
532
|
.option("--caption <text>", "Post caption/body")
|
|
492
533
|
.option("--alt <text>", "Alt text")
|
|
493
534
|
.option("--link <url>", "Post link")
|
|
535
|
+
.option("--auto-thread-text <boolean>", "Automatically thread text: true/false")
|
|
494
536
|
.option("--publish-mode <mode>", `Publish mode (${VALID_PUBLISH_MODES.join(", ")})`, "DRAFT")
|
|
495
537
|
.option("--scheduled-at <value>", "Publish datetime (YYYY-MM-DD HH:mm:ss) or null")
|
|
496
538
|
.option("--post-as-story <boolean>", "Post as story: true/false")
|
|
@@ -515,8 +557,8 @@ function registerCommands(program, runtimeApiKey, cliVersion) {
|
|
|
515
557
|
.option("--youtube-tags <tags>", "Comma-separated YouTube tags")
|
|
516
558
|
.option("--youtube-playlists <json>", "YouTube playlists JSON string")
|
|
517
559
|
.option("--googlemybusiness-upload-to-photos-section <boolean>", "Upload to Google Business Profile photos: true/false")
|
|
518
|
-
.action(withCommandErrorHandling(async (options) => {
|
|
519
|
-
const payload = buildPostPayload(options);
|
|
560
|
+
.action(withCommandErrorHandling(program, async (options) => {
|
|
561
|
+
const payload = buildPostPayload(options);
|
|
520
562
|
|
|
521
563
|
await executeRequest(program, runtimeApiKey, {
|
|
522
564
|
method: "POST",
|
package/src/config.js
CHANGED
|
@@ -1,19 +1,60 @@
|
|
|
1
|
-
import crypto from "node:crypto";
|
|
2
|
-
import os from "node:os";
|
|
3
|
-
import path from "node:path";
|
|
4
|
-
import {
|
|
1
|
+
import crypto from "node:crypto";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { chmod, lstat, mkdir, readFile, rename, rm, writeFile } from "node:fs/promises";
|
|
5
|
+
import { CliError } from "./errors.js";
|
|
5
6
|
|
|
6
7
|
const CONFIG_DIR = path.join(os.homedir(), ".nuelink-cli");
|
|
7
8
|
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
|
|
12
|
-
try {
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
9
|
+
const CONFIG_ENCRYPTION_SALT = "nuelink-cli-config-salt-v1";
|
|
10
|
+
const CONFIG_ENCRYPTION_PASSPHRASE = "nuelink-cli::config::v1::auth";
|
|
11
|
+
|
|
12
|
+
async function ensureSafeConfigDirectory({ create = false } = {}) {
|
|
13
|
+
try {
|
|
14
|
+
const directoryStat = await lstat(CONFIG_DIR);
|
|
15
|
+
if (directoryStat.isSymbolicLink() || !directoryStat.isDirectory()) {
|
|
16
|
+
throw new CliError("CONFIG_UNSAFE", `Refusing unsafe config directory: ${CONFIG_DIR}`);
|
|
17
|
+
}
|
|
18
|
+
} catch (error) {
|
|
19
|
+
if (error?.code !== "ENOENT") throw error;
|
|
20
|
+
if (!create) return false;
|
|
21
|
+
await mkdir(CONFIG_DIR, { recursive: true, mode: 0o700 });
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
try {
|
|
25
|
+
await chmod(CONFIG_DIR, 0o700);
|
|
26
|
+
} catch {
|
|
27
|
+
// Ignore on platforms where chmod permissions are not meaningful.
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async function readConfig() {
|
|
34
|
+
try {
|
|
35
|
+
if (!await ensureSafeConfigDirectory()) return {};
|
|
36
|
+
|
|
37
|
+
const fileStat = await lstat(CONFIG_FILE);
|
|
38
|
+
if (fileStat.isSymbolicLink() || !fileStat.isFile()) {
|
|
39
|
+
throw new CliError("CONFIG_UNSAFE", `Refusing unsafe config path: ${CONFIG_FILE}`);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
try {
|
|
43
|
+
await chmod(CONFIG_FILE, 0o600);
|
|
44
|
+
} catch {
|
|
45
|
+
// Ignore on platforms where chmod permissions are not meaningful.
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const content = await readFile(CONFIG_FILE, "utf8");
|
|
49
|
+
try {
|
|
50
|
+
return JSON.parse(content);
|
|
51
|
+
} catch {
|
|
52
|
+
throw new CliError("CONFIG_INVALID", `Config file is not valid JSON: ${CONFIG_FILE}`);
|
|
53
|
+
}
|
|
54
|
+
} catch (error) {
|
|
55
|
+
if (error?.code === "ENOENT") return {};
|
|
56
|
+
if (error instanceof CliError) throw error;
|
|
57
|
+
throw new CliError("CONFIG_READ_FAILED", `Unable to read config file: ${error.message}`);
|
|
17
58
|
}
|
|
18
59
|
}
|
|
19
60
|
|
|
@@ -35,21 +76,25 @@ function encryptApiKey(apiKey) {
|
|
|
35
76
|
};
|
|
36
77
|
}
|
|
37
78
|
|
|
38
|
-
function decryptApiKey(config) {
|
|
39
|
-
if (config?.encryptedApiKey && config?.iv && config?.authTag) {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
79
|
+
function decryptApiKey(config) {
|
|
80
|
+
if (config?.encryptedApiKey && config?.iv && config?.authTag) {
|
|
81
|
+
try {
|
|
82
|
+
const decipher = crypto.createDecipheriv(
|
|
83
|
+
"aes-256-gcm",
|
|
84
|
+
getConfigCipherKey(),
|
|
85
|
+
Buffer.from(config.iv, "base64"),
|
|
86
|
+
);
|
|
87
|
+
decipher.setAuthTag(Buffer.from(config.authTag, "base64"));
|
|
88
|
+
|
|
89
|
+
const decrypted = Buffer.concat([
|
|
90
|
+
decipher.update(Buffer.from(config.encryptedApiKey, "base64")),
|
|
91
|
+
decipher.final(),
|
|
92
|
+
]);
|
|
93
|
+
|
|
94
|
+
return decrypted.toString("utf8");
|
|
95
|
+
} catch {
|
|
96
|
+
throw new CliError("CONFIG_INVALID", `Saved credentials are invalid: ${CONFIG_FILE}`);
|
|
97
|
+
}
|
|
53
98
|
}
|
|
54
99
|
|
|
55
100
|
if (config?.apiKey) {
|
|
@@ -59,16 +104,40 @@ function decryptApiKey(config) {
|
|
|
59
104
|
return undefined;
|
|
60
105
|
}
|
|
61
106
|
|
|
62
|
-
async function writeConfig(config) {
|
|
63
|
-
await
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
}
|
|
71
|
-
|
|
107
|
+
async function writeConfig(config) {
|
|
108
|
+
await ensureSafeConfigDirectory({ create: true });
|
|
109
|
+
|
|
110
|
+
try {
|
|
111
|
+
const existing = await lstat(CONFIG_FILE);
|
|
112
|
+
if (existing.isSymbolicLink() || !existing.isFile()) {
|
|
113
|
+
throw new CliError("CONFIG_UNSAFE", `Refusing unsafe config path: ${CONFIG_FILE}`);
|
|
114
|
+
}
|
|
115
|
+
} catch (error) {
|
|
116
|
+
if (error?.code !== "ENOENT") throw error;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const temporaryFile = path.join(
|
|
120
|
+
CONFIG_DIR,
|
|
121
|
+
`.config-${process.pid}-${crypto.randomBytes(8).toString("hex")}.tmp`,
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
try {
|
|
125
|
+
await writeFile(temporaryFile, JSON.stringify(config, null, 2), {
|
|
126
|
+
encoding: "utf8",
|
|
127
|
+
flag: "wx",
|
|
128
|
+
mode: 0o600,
|
|
129
|
+
});
|
|
130
|
+
await rename(temporaryFile, CONFIG_FILE);
|
|
131
|
+
|
|
132
|
+
try {
|
|
133
|
+
await chmod(CONFIG_FILE, 0o600);
|
|
134
|
+
} catch {
|
|
135
|
+
// Ignore on platforms where chmod permissions are not meaningful.
|
|
136
|
+
}
|
|
137
|
+
} finally {
|
|
138
|
+
await rm(temporaryFile, { force: true });
|
|
139
|
+
}
|
|
140
|
+
}
|
|
72
141
|
|
|
73
142
|
async function saveApiKey(apiKey) {
|
|
74
143
|
await writeConfig(encryptApiKey(apiKey));
|
package/src/errors.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import chalk from "chalk";
|
|
2
|
+
|
|
3
|
+
class CliError extends Error {
|
|
4
|
+
constructor(code, message, { httpStatus = null, details = {} } = {}) {
|
|
5
|
+
super(message);
|
|
6
|
+
this.name = "CliError";
|
|
7
|
+
this.code = code;
|
|
8
|
+
this.httpStatus = httpStatus;
|
|
9
|
+
this.details = details;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function normalizeCliError(error, fallbackCode = "CLI_ERROR") {
|
|
14
|
+
if (error instanceof CliError) {
|
|
15
|
+
return error;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return new CliError(fallbackCode, error?.message || "Unexpected CLI error.");
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function printCliError(error, { json = false, fallbackCode } = {}) {
|
|
22
|
+
const normalized = normalizeCliError(error, fallbackCode);
|
|
23
|
+
|
|
24
|
+
if (json) {
|
|
25
|
+
process.stdout.write(`${JSON.stringify({
|
|
26
|
+
success: false,
|
|
27
|
+
error: {
|
|
28
|
+
code: normalized.code,
|
|
29
|
+
message: normalized.message,
|
|
30
|
+
httpStatus: normalized.httpStatus,
|
|
31
|
+
details: normalized.details,
|
|
32
|
+
},
|
|
33
|
+
})}\n`);
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
console.error(chalk.red(normalized.message));
|
|
38
|
+
if (normalized.details && Object.keys(normalized.details).length > 0) {
|
|
39
|
+
console.error(chalk.red(JSON.stringify(normalized.details, null, 2)));
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export {
|
|
44
|
+
CliError,
|
|
45
|
+
normalizeCliError,
|
|
46
|
+
printCliError,
|
|
47
|
+
};
|