para-cli 1.18.7 → 1.19.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 (3) hide show
  1. package/index.js +125 -13
  2. package/package.json +6 -6
  3. package/para-cli.js +21 -3
package/index.js CHANGED
@@ -56,7 +56,7 @@ export function setup(config) {
56
56
  rl.question(cyan.bold('Para Access Key: '), function (accessKey) {
57
57
  rl.question(cyan.bold('Para Secret Key: '), function (secretKey) {
58
58
  rl.question(cyan.bold('Para Endpoint: '), function (endpoint) {
59
- var access = (accessKey || config.get('accessKey')).trim();
59
+ var access = (accessKey || config.get('accessKey') || "app:para").trim();
60
60
  var secret = (secretKey || config.get('secretKey')).trim();
61
61
  var endpoint = (endpoint || config.get('endpoint')).trim();
62
62
  newJWT(access, secret, endpoint, config);
@@ -427,9 +427,10 @@ export function rebuildIndex(pc, config, flags) {
427
427
  }
428
428
 
429
429
  export function listApps(config, flags, parentAccessKey, failureCallback) {
430
- var accessKey = flags.accessKey || process.env.PARA_ACCESS_KEY || config.get('accessKey');
431
- var secretKey = flags.secretKey || process.env.PARA_SECRET_KEY || config.get('secretKey');
432
- var endpoint = flags.endpoint || process.env.PARA_ENDPOINT || config.get('endpoint');
430
+ var selectedEndpoint = getSelectedEndpoint(config, flags);
431
+ var accessKey = selectedEndpoint.accessKey;
432
+ var secretKey = selectedEndpoint.secretKey;
433
+ var endpoint = selectedEndpoint.endpoint;
433
434
  var pc = new ParaClient(accessKey, secretKey, {endpoint: endpoint});
434
435
  var p = new Pager();
435
436
  var results = [];
@@ -440,7 +441,7 @@ export function listApps(config, flags, parentAccessKey, failureCallback) {
440
441
  }).then(function () {
441
442
  var apps = results.map(function (app) {return app.appIdentifier.trim();});
442
443
  if (apps.length) {
443
- console.log('Found', p.count, 'apps:', yellow('[') + green(apps.join(yellow('] ['))) + yellow(']'));
444
+ console.log('Found', p.count, 'apps on ' + cyan(endpoint) + ':', yellow('[') + green(apps.join(yellow('] ['))) + yellow(']'));
444
445
  console.log('Typing', cyan('para-cli select'), green(apps[0]), 'will switch to that app. Current app:',
445
446
  green(parentAccessKey));
446
447
  process.exit(0);
@@ -452,9 +453,11 @@ export function listApps(config, flags, parentAccessKey, failureCallback) {
452
453
  });
453
454
  }
454
455
 
455
- export function selectApp(pc, input, config, flags) {
456
- var accessKey = flags.accessKey || process.env.PARA_ACCESS_KEY || config.get('accessKey');
457
- var secretKey = flags.secretKey || process.env.PARA_SECRET_KEY || config.get('secretKey');
456
+ export function selectApp(input, config, flags) {
457
+ var selectedEndpoint = getSelectedEndpoint(config, flags);
458
+ var accessKey = selectedEndpoint.accessKey;
459
+ var secretKey = selectedEndpoint.secretKey;
460
+ var endpoint = selectedEndpoint.endpoint;
458
461
  if (accessKey === 'app:para' && secretKey) {
459
462
  var selectedApp = 'app:' + (input[1] || 'para').trim();
460
463
  if (selectedApp === 'app:para') {
@@ -465,28 +468,137 @@ export function selectApp(pc, input, config, flags) {
465
468
  var now = Math.round(new Date().getTime() / 1000);
466
469
  var jwt = sign(JSON.stringify({
467
470
  iat: now,
468
- exp: now + 60,
471
+ exp: now + 10,
469
472
  appid: accessKey,
470
473
  getCredentials: selectedApp
471
474
  }), secretKey, { algorithm: 'HS256' });
472
- pc.setAccessToken(jwt);
473
- pc.me(jwt).then(function (data) {
475
+ var paraClient = new ParaClient(accessKey, secretKey, { endpoint: endpoint });
476
+ paraClient.setAccessToken(jwt);
477
+ paraClient.me(jwt).then(function (data) {
474
478
  if (data && data.credentials) {
475
479
  config.set('selectedApp', data.credentials);
476
480
  console.log(green('✔'), 'Selected', green(selectedApp), 'as the current app.');
477
481
  } else {
478
482
  fail('That did not work -' + red(input[1]) + ' try updating Para to the latest version.');
479
483
  }
480
- pc.clearAccessToken();
481
484
  }).catch(function (err) {
482
485
  fail('App ' + red(input[1]) + ' not found!');
483
- pc.clearAccessToken();
484
486
  });
485
487
  } else {
486
488
  fail('This command only works when Para CLI is configured to use the keys for the root app.');
487
489
  }
488
490
  }
489
491
 
492
+ export function listEndpoints(config, flags, failureCallback) {
493
+ var accessKey = flags.accessKey || process.env.PARA_ACCESS_KEY || config.get('accessKey');
494
+ var secretKey = flags.secretKey || process.env.PARA_SECRET_KEY || config.get('secretKey');
495
+ var endpoint = flags.endpoint || process.env.PARA_ENDPOINT || config.get('endpoint');
496
+ var endpoints = config.get('endpoints') || [];
497
+ var list = [{endpoint: endpoint, accessKey: accessKey, secretKey: secretKey}].concat(endpoints);
498
+ if (list.length === 0) {
499
+ failureCallback();
500
+ return [];
501
+ }
502
+ for (var i = 0; i < list.length; i++) {
503
+ var ep = list[i];
504
+ var selected = (config.get('selectedEndpoint') || 0) === i;
505
+ var rootAppConfigured = ep.accessKey === 'app:para' && ep.secretKey.length > 10;
506
+ console.log(yellow((selected ? ' ➤' : ' '), (i + 1) + '. ') + cyan(ep.endpoint), rootAppConfigured ?
507
+ green('✔ root app configured') : red('root app not configured'));
508
+ }
509
+ return list;
510
+ }
511
+
512
+ export function addEndpoint(config) {
513
+ var endpoints = config.get('endpoints') || [];
514
+ var rl = createInterface({
515
+ input: process.stdin,
516
+ output: process.stdout
517
+ });
518
+ rl.question(cyan.bold('Para Endpoint: '), function (endpoint) {
519
+ rl.question(cyan.bold('Para Secret Key (for root app app:para): '), function (secretKey) {
520
+ var pc = new ParaClient("app:para", secretKey, {endpoint: endpoint});
521
+ var endpoints = config.get('endpoints') || [];
522
+ var existing = false;
523
+ for (var i = 0; i < endpoints.length; i++) {
524
+ var ep = endpoints[i];
525
+ if (ep.endpoint === endpoint) {
526
+ ep.secretKey = secretKey;
527
+ existing = true;
528
+ }
529
+ }
530
+ if (!existing) {
531
+ endpoints.push({accessKey: 'app:para', secretKey: secretKey, endpoint: endpoint});
532
+ }
533
+ config.set('endpoints', endpoints);
534
+ ping(pc, config);
535
+ rl.close();
536
+ });
537
+ });
538
+ }
539
+
540
+ export function removeEndpoint(config, flags) {
541
+ var list = listEndpoints(config, flags, function () {console.log('No endpoints found.');});
542
+ var rl = createInterface({
543
+ input: process.stdin,
544
+ output: process.stdout
545
+ });
546
+
547
+ rl.question(yellow.bold('Type the number of the Para endpoint to remove: '), function (index) {
548
+ var selectedEndpoint = 0;
549
+ if (!isNaN(index) && index <= list.length && index >= 1) {
550
+ selectedEndpoint = index - 1;
551
+ }
552
+ var url = list[selectedEndpoint].endpoint;
553
+ if (selectedEndpoint === 0) {
554
+ config.set('accessKey', 'app:para');
555
+ config.set('secretKey', '');
556
+ config.set('endpoint', defaultConfig.endpoint);
557
+ } else {
558
+ if (selectedEndpoint === config.get('selectedEndpoint')) {
559
+ config.delete('selectedEndpoint');
560
+ config.delete('selectedApp');
561
+ }
562
+ list.splice(selectedEndpoint, 1);
563
+ list.shift();
564
+ config.set('endpoints', list);
565
+ }
566
+ console.log("Removed endpoint: " + cyan(url));
567
+ rl.close();
568
+ });
569
+ }
570
+
571
+ export function selectEndpoint(config, flags) {
572
+ var list = listEndpoints(config, flags, function () {console.log('No endpoints found.');});
573
+ var rl = createInterface({
574
+ input: process.stdin,
575
+ output: process.stdout
576
+ });
577
+ rl.question(yellow.bold('Type the number of the Para endpoint to select: '), function (index) {
578
+ var selectedEndpoint = 0;
579
+ if (!isNaN(index) && index <= list.length && index >= 1) {
580
+ selectedEndpoint = index - 1;
581
+ }
582
+ config.delete('selectedApp');
583
+ config.set('selectedEndpoint', selectedEndpoint);
584
+ console.log("Selected endpoint: " + cyan(list[selectedEndpoint].endpoint));
585
+ rl.close();
586
+ });
587
+ }
588
+
589
+ function getSelectedEndpoint(config, flags) {
590
+ var accessKey = flags.accessKey || process.env.PARA_ACCESS_KEY || config.get('accessKey');
591
+ var secretKey = flags.secretKey || process.env.PARA_SECRET_KEY || config.get('secretKey');
592
+ var endpoint = flags.endpoint || process.env.PARA_ENDPOINT || config.get('endpoint');
593
+ var endpoints = [{endpoint: endpoint, accessKey: accessKey, secretKey: secretKey}].concat(config.get('endpoints') || []);
594
+ try {
595
+ return endpoints[config.get('selectedEndpoint') || 0];
596
+ } catch (e) {
597
+ config.delete('selectedEndpoint');
598
+ return endpoints[0];
599
+ }
600
+ }
601
+
490
602
  function sendFileChunk(chunkId, textEncoded, json, id, flags, start, end, pc, decoder) {
491
603
  if (start > 0 && textEncoded[start] !== 32) {
492
604
  for (var i = 0; i < 100 && start - i >= 0; i++) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "para-cli",
3
- "version": "1.18.7",
3
+ "version": "1.19.0",
4
4
  "license": "Apache-2.0",
5
5
  "description": "Command-line tool for Para backend servers",
6
6
  "homepage": "https://paraio.org",
@@ -24,25 +24,25 @@
24
24
  "api"
25
25
  ],
26
26
  "devDependencies": {
27
- "eslint": "^8.19.0"
27
+ "eslint": "^8.28.0"
28
28
  },
29
29
  "repository": "Erudika/para-cli",
30
30
  "dependencies": {
31
31
  "brace-expansion": "^2.0.1",
32
- "chalk": "^5.0.1",
33
- "conf": "^10.1.2",
32
+ "chalk": "^5.1.2",
33
+ "conf": "^10.2.0",
34
34
  "exit": "^0.1.2",
35
35
  "figlet": "^1.5.2",
36
36
  "get-stdin": "^9.0.0",
37
37
  "globby": "^13.1.2",
38
38
  "htmlparser2": "^8.0.1",
39
39
  "jsonwebtoken": "^8.5.1",
40
- "meow": "^10.1.3",
40
+ "meow": "^11.0.0",
41
41
  "mime-types": "^2.1.35",
42
42
  "para-client-js": "^1.37.14",
43
43
  "resolve": "^1.22.1",
44
44
  "striptags": "^3.2.0",
45
45
  "update-notifier": "^6.0.2",
46
- "yargs-parser": "^21.0.1"
46
+ "yargs-parser": "^21.1.1"
47
47
  }
48
48
  }
package/para-cli.js CHANGED
@@ -27,7 +27,10 @@ import Conf from 'conf';
27
27
  import figlet from 'figlet';
28
28
  import chalk from 'chalk';
29
29
  import meow from 'meow';
30
- import { defaultConfig, setup, listApps, selectApp, createAll, readAll, updateAll, deleteAll, search, newKeys, newJWT, newApp, ping, me, appSettings, rebuildIndex, exportData, importData } from './index.js';
30
+ import {
31
+ defaultConfig, setup, listApps, selectEndpoint, addEndpoint, removeEndpoint, selectApp, createAll, readAll,
32
+ updateAll, deleteAll, search, newKeys, newJWT, newApp, ping, me, appSettings, rebuildIndex, exportData, importData
33
+ } from './index.js';
31
34
 
32
35
  const { red, green, blue } = chalk;
33
36
  const { textSync } = figlet;
@@ -40,6 +43,7 @@ var cli = meow(`
40
43
  setup Initial setup, prompts you to enter your Para API keys and endpoint
41
44
  apps Returns a list of all Para apps
42
45
  select <appid> Selects a Para app as a target for all subsequent read/write requests.
46
+ endpoints [add|remove] List and select Para server endpoints, add new or remove an exiting one.
43
47
  create <file|glob> [--id] [--type] Persists files as Para objects and makes them searchable
44
48
  read --id 123 [--id 345 ...] Fetches objects with the given ids
45
49
  update <file.json|glob> ... Updates Para objects with the data from a JSON file (must contain id field)
@@ -82,7 +86,9 @@ var cli = meow(`
82
86
  $ para-cli search "*" --type article --page all
83
87
  $ para-cli new-key
84
88
  $ para-cli new-app "mynewapp" --name "Full app name"
85
-
89
+ $ para-cli apps
90
+ $ para-cli select scoold
91
+ $ para-cli endpoints
86
92
  `, {
87
93
  importMeta: import.meta,
88
94
  flags: {
@@ -111,12 +117,14 @@ var selectedApp = config.get('selectedApp');
111
117
  if (!flags.accessKey && !flags.secretKey && selectedApp && selectedApp.accessKey && selectedApp.accessKey.indexOf("app:") === 0) {
112
118
  accessKey = selectedApp.accessKey;
113
119
  secretKey = selectedApp.secretKey;
120
+ endpoint = selectedApp.endpoint;
114
121
  }
115
122
 
116
123
  if (!input[0]) {
117
124
  console.log(help);
118
125
  } else if (!accessKey || !secretKey) {
119
126
  console.error(red('Command ' + input[0] + ' failed! Blank credentials, running setup first...'));
127
+ console.log("Please enter the access key and secret key for the root app 'app:para' first.");
120
128
  process.exitCode = 1;
121
129
  setup(config);
122
130
  } else {
@@ -130,8 +138,18 @@ if (!input[0]) {
130
138
  listApps(config, flags, accessKey, function () {console.log('No apps found within', green(accessKey));});
131
139
  }
132
140
 
141
+ if (input[0] === 'endpoints') {
142
+ if (input.length > 1 && input[1] === 'add') {
143
+ addEndpoint(config);
144
+ } else if (input.length > 1 && input[1] === 'remove') {
145
+ removeEndpoint(config, flags);
146
+ } else {
147
+ selectEndpoint(config, flags);
148
+ }
149
+ }
150
+
133
151
  if (input[0] === 'select') {
134
- selectApp(pc, input, config, flags);
152
+ selectApp(input, config, flags);
135
153
  }
136
154
 
137
155
  if (input[0] === 'create') {