@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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. 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.0.0",
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
- const config = await readConfig();
37
- config.apiKey = apiKey;
38
- await writeConfig(config);
81
+ await writeConfig(encryptApiKey(apiKey));
39
82
  }
40
83
 
41
84
  async function clearApiKey() {
42
- const config = await readConfig();
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
- return config.apiKey;
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
- if (config.apiKey) {
251
- console.log(chalk.green(`Auth source: saved config (${maskToken(config.apiKey)})`));
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
- await nuelinkRequest({
301
- apiKey,
302
- endpoint: `/brands/${options.brandId}/collections`,
303
- query: {
304
- per_page: options.perPage,
305
- page: options.page,
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
- await nuelinkRequest({
322
- apiKey,
323
- method: "POST",
324
- endpoint: `/brands/${options.brandId}/collections`,
325
- body: {
326
- title: options.title,
327
- description: options.description,
328
- maxRepublish: options.maxRepublish ? Number(options.maxRepublish) : undefined,
329
- channels: parseCsv(options.channels).map((id) => Number(id)),
330
- queues: parseCsv(options.queues),
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
- await nuelinkRequest({
342
- apiKey,
343
- endpoint: `/brands/${options.brandId}/automations`,
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
- await nuelinkRequest({
363
- apiKey,
364
- method: "POST",
365
- endpoint: `/brands/${options.brandId}/automations`,
366
- body: {
367
- collectionId: Number(options.collectionId),
368
- feedUrl: options.feedUrl,
369
- importAsType: options.importAsType,
370
- subType: options.subType,
371
- title: options.title,
372
- type: options.type,
373
- dynamicTitle: options.dynamicTitle,
374
- dynamicBody: options.dynamicBody,
375
- description: options.description,
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
- await nuelinkRequest({
389
- apiKey,
390
- endpoint: `/brands/${options.brandId}/channels`,
391
- query: {
392
- per_page: options.perPage,
393
- page: options.page,
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
- await nuelinkRequest({
405
- apiKey,
406
- endpoint: `/brands/${options.brandId}/media`,
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
- await nuelinkRequest({
423
- apiKey,
424
- method: "POST",
425
- endpoint: `/brands/${options.brandId}/media`,
426
- multipart: formData,
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
- await nuelinkRequest({
438
- apiKey,
439
- endpoint: `/brands/${options.brandId}/collections/${options.collectionId}/posts`,
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
- await nuelinkRequest({
455
- apiKey,
456
- method: "POST",
457
- endpoint: `/brands/${options.brandId}/collections/${options.collectionId}/posts`,
458
- body: payload,
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
- await nuelinkRequest({
597
- apiKey,
598
- method: "POST",
599
- endpoint: `/brands/${options.brandId}/collections/${options.collectionId}/posts`,
600
- body: payload,
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("-"));