@witnet/sdk 1.1.0 → 1.1.1

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.
@@ -27,12 +27,21 @@ module.exports = {
27
27
  hint: "List available Witnet Radon assets.",
28
28
  params: "[RADON_ASSETS ...]",
29
29
  options: {
30
+ filter: {
31
+ hint: "Restrict output to name-matching assets.",
32
+ },
30
33
  legacy: {
31
34
  hint: "List only those declared in witnet/assets folder.",
32
35
  },
33
- filter: {
34
- hint: "Restrict output to name-matching assets.",
36
+ modals: {
37
+ hint: "Restrict output to Radon modals.",
35
38
  },
39
+ requests: {
40
+ hint: "Restrict output to Radon requests.",
41
+ },
42
+ templates: {
43
+ hint: "Restrict output to Radon templates.",
44
+ },
36
45
  },
37
46
  },
38
47
  check: {
@@ -169,21 +178,25 @@ async function assets (options = {}, [...patterns]) {
169
178
  `${options?.module ? options.module.toUpperCase() : path.basename(process.cwd()).toUpperCase()} RADON ASSETS`,
170
179
  helpers.colors.white
171
180
  )
172
- const assets = clearEmptyBranches(loadAssets(options), patterns, options?.filter)
181
+ const assets = clearEmptyBranches(
182
+ options?.module ? require(options.module) : loadAssets(options),
183
+ patterns,
184
+ options
185
+ );
173
186
  if (assets && Object.keys(assets).length > 0) {
174
- traceWitnetArtifacts(assets, patterns, " ", options?.filter)
187
+ traceWitnetArtifacts(assets, patterns, " ", options)
175
188
  } else {
176
189
  console.info("> No custom Radon assets declared just yet.")
177
190
  }
178
191
  }
179
192
  /// -------------------------------------------------------------------------------------------------------------------
180
193
 
181
- async function check (options = {}) {
194
+ async function check () {
182
195
  if (!isModuleInitialized) {
183
196
  throw new Error(`No Witnet Radon workspace has been initialized. Please, run ${white("npx witsdk radon init")} if willing to declare your own Radon assets.`)
184
197
  }
185
198
  try {
186
- const assets = loadAssets({ ...options, legacy: true })
199
+ const assets = loadAssets({ legacy: true })
187
200
  const [
188
201
  modals,
189
202
  requests,
@@ -234,7 +247,7 @@ async function decode (options = {}, args = []) {
234
247
  } else {
235
248
  args = args.slice(1)
236
249
 
237
- const assets = loadAssets(options)
250
+ const assets = options?.module ? require(options.module) : loadAssets(options)
238
251
  const headline = options?.headline
239
252
  const crafts = flattenRadonArtifacts(assets).filter(craft => craft.key.toLowerCase().indexOf(asset.toLowerCase()) >= 0)
240
253
  if (crafts.length === 0) {
@@ -300,7 +313,7 @@ async function dryrun (options = {}, args = []) {
300
313
  }
301
314
  } else {
302
315
  args = args.slice(1)
303
- const assets = loadAssets(options)
316
+ const assets = options?.module ? require(options.module) : loadAssets(options)
304
317
  const headline = options?.headline
305
318
  const crafts = flattenRadonArtifacts(assets).filter(craft => craft.key.toLowerCase().indexOf(asset.toLowerCase()) >= 0)
306
319
  if (crafts.length === 0) {
@@ -384,8 +397,12 @@ const stringifyReducer = (x, c) => {
384
397
  }
385
398
 
386
399
  function loadAssets (options) {
387
- const { assets } = options?.module ? require(options.module) : (options?.legacy ? {} : require("@witnet/sdk"))
388
- return isModuleInitialized && fs.existsSync(`${WITNET_ASSETS_PATH}`) ? merge(assets, require(`${WITNET_ASSETS_PATH}`)) : assets
400
+ const { assets } = options?.legacy ? {} : require("@witnet/sdk")
401
+ return (
402
+ isModuleInitialized && fs.existsSync(`${WITNET_ASSETS_PATH}`)
403
+ ? merge(assets, require(`${WITNET_ASSETS_PATH}`))
404
+ : assets
405
+ );
389
406
  }
390
407
 
391
408
  function flattenRadonArtifacts (tree, headers) {
@@ -412,44 +429,64 @@ function flattenRadonArtifacts (tree, headers) {
412
429
  return matches
413
430
  };
414
431
 
415
- function countWitnetArtifacts (assets, args, filter = false) {
432
+ function countWitnetArtifacts (assets, args, options) {
416
433
  let counter = 0
417
434
  Object.entries(assets).forEach(([key, value]) => {
418
- if ((
419
- value instanceof Witnet.Radon.RadonModal ||
420
- value instanceof Witnet.Radon.RadonRequest ||
421
- value instanceof Witnet.Radon.RadonTemplate ||
422
- value instanceof Witnet.Radon.RadonRetrieval
423
- ) && (
424
- !filter ||
425
- !args ||
426
- args.length === 0 ||
427
- args.find(arg => key.toLowerCase().indexOf(arg.toLowerCase()) >= 0)
435
+ let include = (
436
+ (options?.modals && value instanceof Witnet.Radon.RadonModal)
437
+ || (options?.templates && value instanceof Witnet.Radon.RadonTemplate)
438
+ || (options?.retrievals && value instanceof Witnet.Radon.RadonRetrieval)
439
+ || (options?.requests && value instanceof Witnet.Radon.RadonRequest)
440
+ );
441
+ if (!(options?.modals || options?.templates || options?.retrievals || options?.requests)) {
442
+ include = (
443
+ value instanceof Witnet.Radon.RadonModal ||
444
+ value instanceof Witnet.Radon.RadonRequest ||
445
+ value instanceof Witnet.Radon.RadonTemplate ||
446
+ value instanceof Witnet.Radon.RadonRetrieval
447
+ );
448
+ }
449
+ if (include && (
450
+ !options?.filter
451
+ || !args
452
+ || args.length === 0
453
+ || args.find(arg => key.toLowerCase().indexOf(arg.toLowerCase()) >= 0)
428
454
  )) {
429
455
  counter++
430
456
  } else if (typeof value === "object") {
431
- counter += countWitnetArtifacts(value, args)
457
+ counter += countWitnetArtifacts(value, args, options)
432
458
  }
433
459
  })
434
460
  return counter
435
461
  }
436
462
 
437
- function clearEmptyBranches (node, args, filter) {
463
+ function clearEmptyBranches (node, args, options) {
438
464
  if (node) {
439
465
  const assets = Object.fromEntries(
440
466
  Object.entries(node).map(([key, value]) => {
467
+ let include = (
468
+ (options?.modals && value instanceof Witnet.Radon.RadonModal)
469
+ || (options?.templates && value instanceof Witnet.Radon.RadonTemplate)
470
+ || (options?.retrievals && value instanceof Witnet.Radon.RadonRetrieval)
471
+ || (options?.requests && value instanceof Witnet.Radon.RadonRequest)
472
+ );
473
+ if (!(options?.modals || options?.templates || options?.retrievals || options?.requests)) {
474
+ include = (
475
+ value instanceof Witnet.Radon.RadonModal ||
476
+ value instanceof Witnet.Radon.RadonRequest ||
477
+ value instanceof Witnet.Radon.RadonTemplate ||
478
+ value instanceof Witnet.Radon.RadonRetrieval
479
+ );
480
+ }
441
481
  if (
442
- (!filter || args.find(arg => key.toLowerCase().indexOf(arg.toLowerCase()) >= 0)) && (
443
- value instanceof Witnet.Radon.RadonRetrieval ||
444
- value instanceof Witnet.Radon.RadonModal ||
445
- value instanceof Witnet.Radon.RadonRequest ||
446
- value instanceof Witnet.Radon.RadonTemplate
482
+ include && (
483
+ !options?.filter || args.find(arg => key.toLowerCase().indexOf(arg.toLowerCase()) >= 0)
447
484
  )
448
485
  ) {
449
486
  return [key, value]
450
487
  } else if (typeof value === "object") {
451
- if (countWitnetArtifacts(value, args, filter) > 0) {
452
- return [key, clearEmptyBranches(value, args, filter)]
488
+ if (countWitnetArtifacts(value, args, options) > 0) {
489
+ return [key, clearEmptyBranches(value, args, options)]
453
490
  } else {
454
491
  return [key, undefined]
455
492
  }
@@ -467,25 +504,25 @@ function clearEmptyBranches (node, args, filter) {
467
504
  }
468
505
  }
469
506
 
470
- function traceWitnetArtifacts (assets, args, indent = "", filter = false) {
507
+ function traceWitnetArtifacts (assets, args, indent = "", options) {
471
508
  const prefix = `${indent}`
472
509
  Object.keys(assets).forEach((key, index) => {
473
510
  const isLast = index === Object.keys(assets).length - 1
474
511
  const found = args.find(arg => key.toLowerCase().indexOf(arg.toLowerCase()) >= 0)
475
512
  const color = found ? helpers.colors.mcyan : helpers.colors.cyan
476
513
  if (assets[key] instanceof Witnet.Radon.RadonRequest) {
477
- if (!filter || found) {
514
+ if (!options?.filter || found) {
478
515
  console.info(`${prefix}${color(key)} ${assets[key].sources.length > 1 ? helpers.colors.yellow(`(${assets[key].sources.length} sources)`) : ""}`)
479
516
  if (isLast) {
480
517
  console.info(`${prefix}`)
481
518
  }
482
519
  }
483
- } else if (
520
+ } else if ((
484
521
  assets[key] instanceof Witnet.Radon.RadonTemplate ||
485
522
  assets[key] instanceof Witnet.Radon.RadonModal
486
- ) {
523
+ )) {
487
524
  const argsCount = assets[key].argsCount
488
- if (!filter || found) {
525
+ if (!options?.filter || found) {
489
526
  console.info(`${prefix}${color(key)} ${argsCount > 0 ? helpers.colors.green(`(${argsCount} args)`) : ""}`)
490
527
  if (isLast) {
491
528
  console.info(`${prefix}`)
@@ -493,15 +530,15 @@ function traceWitnetArtifacts (assets, args, indent = "", filter = false) {
493
530
  }
494
531
  } else if (assets[key] instanceof Witnet.Radon.RadonRetrieval) {
495
532
  const argsCount = assets[key].argsCount
496
- if (!filter || found) {
533
+ if (!options?.filter || found) {
497
534
  console.info(`${prefix}${color(key)} ${argsCount > 0 ? helpers.colors.green(`(${argsCount} args)`) : ""}`)
498
535
  if (isLast) {
499
536
  console.info(`${prefix}`)
500
537
  }
501
538
  }
502
- } else if (typeof assets[key] === "object" && countWitnetArtifacts(assets[key], args, filter) > 0) {
539
+ } else if (typeof assets[key] === "object" && countWitnetArtifacts(assets[key], args, options) > 0) {
503
540
  console.info(`${indent}${isLast ? "└─ " : "├─ "}${key}`)
504
- traceWitnetArtifacts(assets[key], args, !isLast ? `${indent}│ ` : `${indent} `, filter)
541
+ traceWitnetArtifacts(assets[key], args, !isLast ? `${indent}│ ` : `${indent} `, options)
505
542
  }
506
543
  })
507
544
  }
@@ -946,13 +946,12 @@ async function _loadRadonRequest (options = {}) {
946
946
  }
947
947
 
948
948
  // load Radon assets from environment
949
- let assets = utils.searchRadonAssets(
950
- {
951
- assets: loadAssets(options),
949
+ const assets = utils.searchRadonAssets({
950
+ assets: options?.module ? require(options.module) : loadAssets(options),
952
951
  pattern: options?.pattern,
953
- },
952
+ },
954
953
  (key, pattern) => key.toLowerCase().indexOf(pattern.toLowerCase()) >= 0
955
- )
954
+ );
956
955
 
957
956
  if (args.length > 0) {
958
957
  // ignore RadonRequests if args were passed from the CLI
@@ -98,7 +98,7 @@ async function execRadonBytecode (bytecode, ...flags) {
98
98
  throw EvalError("invalid hex string")
99
99
  } else {
100
100
  const npx = os.type() === "Windows_NT" ? "npx.cmd" : "npx"
101
- return cmd(npx, "witnet", "radon", "dry-run", bytecode, ...flags)
101
+ return cmd(npx, "witsdk", "radon", "dry-run", bytecode, ...flags)
102
102
  // .catch((err) => {
103
103
  // let errorMessage = err.message.split('\n').slice(1).join('\n').trim()
104
104
  // const errorRegex = /.*^error: (?<message>.*)$.*/gm
@@ -1,4 +1,4 @@
1
- #!/usr/bin/env node
1
+ #!/usr/bin/env node
2
2
 
3
3
  /// IMPORTS ===========================================================================================================
4
4