@nuelink/nuelink-cli 1.0.0 → 1.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/package.json +1 -1
- package/src/main.js +200 -83
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nuelink/nuelink-cli",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "The official Nuelink CLI for automating social media workflows from your terminal. Schedule posts, upload media, manage brands, and integrate Nuelink into scripts, CI/CD pipelines, and AI agents.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Nuelink",
|
package/src/main.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
import { program } from "commander";
|
|
4
4
|
import chalk from "chalk";
|
|
5
|
+
import crypto from "node:crypto";
|
|
5
6
|
import os from "node:os";
|
|
6
7
|
import path from "node:path";
|
|
7
8
|
import { readFile, writeFile, mkdir, chmod, stat } from "node:fs/promises";
|
|
@@ -10,6 +11,8 @@ const API_BASE_URL = "https://nuelink.com/api/public/v1";
|
|
|
10
11
|
const CONFIG_DIR = path.join(os.homedir(), ".nuelink-cli");
|
|
11
12
|
const CONFIG_FILE = path.join(CONFIG_DIR, "config.json");
|
|
12
13
|
const REQUEST_TIMEOUT_MS = Number(process.env.NUELINK_TIMEOUT_MS || 30000);
|
|
14
|
+
const CONFIG_ENCRYPTION_SALT = "nuelink-cli-config-salt-v1";
|
|
15
|
+
const CONFIG_ENCRYPTION_PASSPHRASE = "nuelink-cli::config::v1::auth";
|
|
13
16
|
|
|
14
17
|
async function readConfig() {
|
|
15
18
|
try {
|
|
@@ -20,6 +23,48 @@ async function readConfig() {
|
|
|
20
23
|
}
|
|
21
24
|
}
|
|
22
25
|
|
|
26
|
+
function getConfigCipherKey() {
|
|
27
|
+
return crypto.scryptSync(CONFIG_ENCRYPTION_PASSPHRASE, CONFIG_ENCRYPTION_SALT, 32);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function encryptApiKey(apiKey) {
|
|
31
|
+
const iv = crypto.randomBytes(12);
|
|
32
|
+
const cipher = crypto.createCipheriv("aes-256-gcm", getConfigCipherKey(), iv);
|
|
33
|
+
const encrypted = Buffer.concat([cipher.update(apiKey, "utf8"), cipher.final()]);
|
|
34
|
+
const authTag = cipher.getAuthTag();
|
|
35
|
+
|
|
36
|
+
return {
|
|
37
|
+
version: 1,
|
|
38
|
+
encryptedApiKey: encrypted.toString("base64"),
|
|
39
|
+
iv: iv.toString("base64"),
|
|
40
|
+
authTag: authTag.toString("base64"),
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function decryptApiKey(config) {
|
|
45
|
+
if (config?.encryptedApiKey && config?.iv && config?.authTag) {
|
|
46
|
+
const decipher = crypto.createDecipheriv(
|
|
47
|
+
"aes-256-gcm",
|
|
48
|
+
getConfigCipherKey(),
|
|
49
|
+
Buffer.from(config.iv, "base64"),
|
|
50
|
+
);
|
|
51
|
+
decipher.setAuthTag(Buffer.from(config.authTag, "base64"));
|
|
52
|
+
|
|
53
|
+
const decrypted = Buffer.concat([
|
|
54
|
+
decipher.update(Buffer.from(config.encryptedApiKey, "base64")),
|
|
55
|
+
decipher.final(),
|
|
56
|
+
]);
|
|
57
|
+
|
|
58
|
+
return decrypted.toString("utf8");
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (config?.apiKey) {
|
|
62
|
+
return config.apiKey;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
67
|
+
|
|
23
68
|
async function writeConfig(config) {
|
|
24
69
|
await mkdir(CONFIG_DIR, { recursive: true });
|
|
25
70
|
await writeFile(CONFIG_FILE, JSON.stringify(config, null, 2), "utf8");
|
|
@@ -33,15 +78,11 @@ async function writeConfig(config) {
|
|
|
33
78
|
}
|
|
34
79
|
|
|
35
80
|
async function saveApiKey(apiKey) {
|
|
36
|
-
|
|
37
|
-
config.apiKey = apiKey;
|
|
38
|
-
await writeConfig(config);
|
|
81
|
+
await writeConfig(encryptApiKey(apiKey));
|
|
39
82
|
}
|
|
40
83
|
|
|
41
84
|
async function clearApiKey() {
|
|
42
|
-
|
|
43
|
-
delete config.apiKey;
|
|
44
|
-
await writeConfig(config);
|
|
85
|
+
await writeConfig({});
|
|
45
86
|
}
|
|
46
87
|
|
|
47
88
|
function maskToken(token) {
|
|
@@ -62,7 +103,31 @@ async function resolveApiKey(runtimeApiKey) {
|
|
|
62
103
|
}
|
|
63
104
|
|
|
64
105
|
const config = await readConfig();
|
|
65
|
-
|
|
106
|
+
const storedApiKey = decryptApiKey(config);
|
|
107
|
+
|
|
108
|
+
if (storedApiKey && config.apiKey) {
|
|
109
|
+
await saveApiKey(storedApiKey);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return storedApiKey;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function pathParam(value, label) {
|
|
116
|
+
const stringValue = String(value ?? "").trim();
|
|
117
|
+
|
|
118
|
+
if (!stringValue) {
|
|
119
|
+
throw new Error(`Missing required ${label}.`);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return encodeURIComponent(stringValue);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function brandPath(brandId) {
|
|
126
|
+
return `/brands/${pathParam(brandId, "brand-id")}`;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function collectionPath(brandId, collectionId) {
|
|
130
|
+
return `${brandPath(brandId)}/collections/${pathParam(collectionId, "collection-id")}`;
|
|
66
131
|
}
|
|
67
132
|
|
|
68
133
|
function extractAuthArg(args) {
|
|
@@ -247,8 +312,9 @@ program
|
|
|
247
312
|
}
|
|
248
313
|
|
|
249
314
|
const config = await readConfig();
|
|
250
|
-
|
|
251
|
-
|
|
315
|
+
const storedApiKey = decryptApiKey(config);
|
|
316
|
+
if (storedApiKey) {
|
|
317
|
+
console.log(chalk.green(`Auth source: saved config (${maskToken(storedApiKey)})`));
|
|
252
318
|
|
|
253
319
|
try {
|
|
254
320
|
const fileStat = await stat(CONFIG_FILE);
|
|
@@ -297,14 +363,20 @@ program
|
|
|
297
363
|
.option("--page <number>", "Page number", "1")
|
|
298
364
|
.action(async (options) => {
|
|
299
365
|
const apiKey = await resolveApiKey(runtimeApiKey);
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
366
|
+
|
|
367
|
+
try {
|
|
368
|
+
await nuelinkRequest({
|
|
369
|
+
apiKey,
|
|
370
|
+
endpoint: `${brandPath(options.brandId)}/collections`,
|
|
371
|
+
query: {
|
|
372
|
+
per_page: options.perPage,
|
|
373
|
+
page: options.page,
|
|
374
|
+
},
|
|
375
|
+
});
|
|
376
|
+
} catch (error) {
|
|
377
|
+
console.error(chalk.red(error.message));
|
|
378
|
+
process.exitCode = 1;
|
|
379
|
+
}
|
|
308
380
|
});
|
|
309
381
|
|
|
310
382
|
program
|
|
@@ -318,18 +390,23 @@ program
|
|
|
318
390
|
.option("--queues <slots>", "Comma-separated queue slots (e.g. Mon 10:10,Mon 12:12)")
|
|
319
391
|
.action(async (options) => {
|
|
320
392
|
const apiKey = await resolveApiKey(runtimeApiKey);
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
393
|
+
try {
|
|
394
|
+
await nuelinkRequest({
|
|
395
|
+
apiKey,
|
|
396
|
+
method: "POST",
|
|
397
|
+
endpoint: `${brandPath(options.brandId)}/collections`,
|
|
398
|
+
body: {
|
|
399
|
+
title: options.title,
|
|
400
|
+
description: options.description,
|
|
401
|
+
maxRepublish: options.maxRepublish ? Number(options.maxRepublish) : undefined,
|
|
402
|
+
channels: parseCsv(options.channels).map((id) => Number(id)),
|
|
403
|
+
queues: parseCsv(options.queues),
|
|
404
|
+
},
|
|
405
|
+
});
|
|
406
|
+
} catch (error) {
|
|
407
|
+
console.error(chalk.red(error.message));
|
|
408
|
+
process.exitCode = 1;
|
|
409
|
+
}
|
|
333
410
|
});
|
|
334
411
|
|
|
335
412
|
program
|
|
@@ -338,10 +415,15 @@ program
|
|
|
338
415
|
.requiredOption("--brand-id <id>", "Brand ID")
|
|
339
416
|
.action(async (options) => {
|
|
340
417
|
const apiKey = await resolveApiKey(runtimeApiKey);
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
418
|
+
try {
|
|
419
|
+
await nuelinkRequest({
|
|
420
|
+
apiKey,
|
|
421
|
+
endpoint: `${brandPath(options.brandId)}/automations`,
|
|
422
|
+
});
|
|
423
|
+
} catch (error) {
|
|
424
|
+
console.error(chalk.red(error.message));
|
|
425
|
+
process.exitCode = 1;
|
|
426
|
+
}
|
|
345
427
|
});
|
|
346
428
|
|
|
347
429
|
program
|
|
@@ -359,22 +441,27 @@ program
|
|
|
359
441
|
.option("--description <text>", "Automation description")
|
|
360
442
|
.action(async (options) => {
|
|
361
443
|
const apiKey = await resolveApiKey(runtimeApiKey);
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
444
|
+
try {
|
|
445
|
+
await nuelinkRequest({
|
|
446
|
+
apiKey,
|
|
447
|
+
method: "POST",
|
|
448
|
+
endpoint: `${brandPath(options.brandId)}/automations`,
|
|
449
|
+
body: {
|
|
450
|
+
collectionId: Number(options.collectionId),
|
|
451
|
+
feedUrl: options.feedUrl,
|
|
452
|
+
importAsType: options.importAsType,
|
|
453
|
+
subType: options.subType,
|
|
454
|
+
title: options.title,
|
|
455
|
+
type: options.type,
|
|
456
|
+
dynamicTitle: options.dynamicTitle,
|
|
457
|
+
dynamicBody: options.dynamicBody,
|
|
458
|
+
description: options.description,
|
|
459
|
+
},
|
|
460
|
+
});
|
|
461
|
+
} catch (error) {
|
|
462
|
+
console.error(chalk.red(error.message));
|
|
463
|
+
process.exitCode = 1;
|
|
464
|
+
}
|
|
378
465
|
});
|
|
379
466
|
|
|
380
467
|
program
|
|
@@ -385,14 +472,19 @@ program
|
|
|
385
472
|
.option("--page <number>", "Page number", "1")
|
|
386
473
|
.action(async (options) => {
|
|
387
474
|
const apiKey = await resolveApiKey(runtimeApiKey);
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
475
|
+
try {
|
|
476
|
+
await nuelinkRequest({
|
|
477
|
+
apiKey,
|
|
478
|
+
endpoint: `${brandPath(options.brandId)}/channels`,
|
|
479
|
+
query: {
|
|
480
|
+
per_page: options.perPage,
|
|
481
|
+
page: options.page,
|
|
482
|
+
},
|
|
483
|
+
});
|
|
484
|
+
} catch (error) {
|
|
485
|
+
console.error(chalk.red(error.message));
|
|
486
|
+
process.exitCode = 1;
|
|
487
|
+
}
|
|
396
488
|
});
|
|
397
489
|
|
|
398
490
|
program
|
|
@@ -401,10 +493,15 @@ program
|
|
|
401
493
|
.requiredOption("--brand-id <id>", "Brand ID")
|
|
402
494
|
.action(async (options) => {
|
|
403
495
|
const apiKey = await resolveApiKey(runtimeApiKey);
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
496
|
+
try {
|
|
497
|
+
await nuelinkRequest({
|
|
498
|
+
apiKey,
|
|
499
|
+
endpoint: `${brandPath(options.brandId)}/media`,
|
|
500
|
+
});
|
|
501
|
+
} catch (error) {
|
|
502
|
+
console.error(chalk.red(error.message));
|
|
503
|
+
process.exitCode = 1;
|
|
504
|
+
}
|
|
408
505
|
});
|
|
409
506
|
|
|
410
507
|
program
|
|
@@ -419,12 +516,17 @@ program
|
|
|
419
516
|
const formData = new FormData();
|
|
420
517
|
formData.append("media", new Blob([fileBuffer]), path.basename(options.file));
|
|
421
518
|
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
519
|
+
try {
|
|
520
|
+
await nuelinkRequest({
|
|
521
|
+
apiKey,
|
|
522
|
+
method: "POST",
|
|
523
|
+
endpoint: `${brandPath(options.brandId)}/media`,
|
|
524
|
+
multipart: formData,
|
|
525
|
+
});
|
|
526
|
+
} catch (error) {
|
|
527
|
+
console.error(chalk.red(error.message));
|
|
528
|
+
process.exitCode = 1;
|
|
529
|
+
}
|
|
428
530
|
});
|
|
429
531
|
|
|
430
532
|
program
|
|
@@ -434,10 +536,15 @@ program
|
|
|
434
536
|
.requiredOption("--collection-id <id>", "Collection ID")
|
|
435
537
|
.action(async (options) => {
|
|
436
538
|
const apiKey = await resolveApiKey(runtimeApiKey);
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
539
|
+
try {
|
|
540
|
+
await nuelinkRequest({
|
|
541
|
+
apiKey,
|
|
542
|
+
endpoint: `${collectionPath(options.brandId, options.collectionId)}/posts`,
|
|
543
|
+
});
|
|
544
|
+
} catch (error) {
|
|
545
|
+
console.error(chalk.red(error.message));
|
|
546
|
+
process.exitCode = 1;
|
|
547
|
+
}
|
|
441
548
|
});
|
|
442
549
|
|
|
443
550
|
program
|
|
@@ -451,12 +558,17 @@ program
|
|
|
451
558
|
const payloadText = await readFile(options.payload, "utf8");
|
|
452
559
|
const payload = JSON.parse(payloadText);
|
|
453
560
|
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
561
|
+
try {
|
|
562
|
+
await nuelinkRequest({
|
|
563
|
+
apiKey,
|
|
564
|
+
method: "POST",
|
|
565
|
+
endpoint: `${collectionPath(options.brandId, options.collectionId)}/posts`,
|
|
566
|
+
body: payload,
|
|
567
|
+
});
|
|
568
|
+
} catch (error) {
|
|
569
|
+
console.error(chalk.red(error.message));
|
|
570
|
+
process.exitCode = 1;
|
|
571
|
+
}
|
|
460
572
|
});
|
|
461
573
|
|
|
462
574
|
program
|
|
@@ -593,12 +705,17 @@ program
|
|
|
593
705
|
: {}),
|
|
594
706
|
};
|
|
595
707
|
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
708
|
+
try {
|
|
709
|
+
await nuelinkRequest({
|
|
710
|
+
apiKey,
|
|
711
|
+
method: "POST",
|
|
712
|
+
endpoint: `${collectionPath(options.brandId, options.collectionId)}/posts`,
|
|
713
|
+
body: payload,
|
|
714
|
+
});
|
|
715
|
+
} catch (error) {
|
|
716
|
+
console.error(chalk.red(error.message));
|
|
717
|
+
process.exitCode = 1;
|
|
718
|
+
}
|
|
602
719
|
});
|
|
603
720
|
|
|
604
721
|
const argvHasCommand = parsedAuth.sanitizedArgs.some((arg) => !arg.startsWith("-"));
|