UI for Zipcoin Blue

command.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const dargs = require("dargs");
  4. const typeDefaults = new Map()
  5. .set(String, null)
  6. .set(Boolean, false);
  7. function minimistOptionsToArray(options, fnOptions = {}) {
  8. if (typeof fnOptions.ignoreFalse === 'undefined') {
  9. fnOptions.ignoreFalse = true;
  10. }
  11. if (fnOptions.useDoubleQuotes) {
  12. fnOptions.useEquals = true;
  13. }
  14. let results = dargs(options, fnOptions);
  15. results.splice(results.length - options._.length); // take out arguments
  16. if (fnOptions.useDoubleQuotes) {
  17. results = results.map(r => r.replace(/^(\-\-[A-Za-z0-9-]+)=(.+\s+.+)$/, '$1="$2"'));
  18. }
  19. return results;
  20. }
  21. exports.minimistOptionsToArray = minimistOptionsToArray;
  22. /**
  23. * Takes a Minimist command option and normalizes its values.
  24. */
  25. function normalizeOption(option) {
  26. if (!option.type) {
  27. option.type = String;
  28. }
  29. if (!option.default) {
  30. option.default = typeDefaults.get(option.type);
  31. }
  32. if (!option.aliases) {
  33. option.aliases = [];
  34. }
  35. return option;
  36. }
  37. function metadataToMinimistOptions(metadata) {
  38. const options = {
  39. string: ['_'],
  40. boolean: [],
  41. alias: {},
  42. default: {}
  43. };
  44. if (!metadata.options) {
  45. return { boolean: true, string: '_' };
  46. }
  47. const schema = metadataToEnvCmdOptsSchema(metadata);
  48. for (let opt of schema) {
  49. const envvar = process.env[opt.envvar];
  50. if (typeof envvar !== 'undefined') {
  51. if (opt.option.type === Boolean) {
  52. opt.option.default = envvar && envvar !== '0' ? true : false;
  53. }
  54. else {
  55. opt.option.default = envvar;
  56. }
  57. }
  58. }
  59. for (let option of metadata.options) {
  60. const normalizedOption = normalizeOption(option);
  61. if (normalizedOption.type === String) {
  62. options.string.push(normalizedOption.name);
  63. }
  64. else if (normalizedOption.type === Boolean) {
  65. options.boolean.push(normalizedOption.name);
  66. }
  67. options.default[normalizedOption.name] = normalizedOption.default;
  68. options.alias[normalizedOption.name] = normalizedOption.aliases;
  69. }
  70. return options;
  71. }
  72. exports.metadataToMinimistOptions = metadataToMinimistOptions;
  73. function metadataToEnvCmdOptsSchema(metadata) {
  74. if (!metadata.options) {
  75. return [];
  76. }
  77. const schema = [];
  78. const fullName = metadata.fullName ? metadata.fullName : metadata.name;
  79. const prefix = `IONIC_CMDOPTS_${fullName.toUpperCase().split(' ').join('_')}`;
  80. for (let option of metadata.options) {
  81. schema.push({ envvar: `${prefix}_${option.name.toUpperCase().split('-').join('_')}`, option });
  82. }
  83. return schema;
  84. }
  85. exports.metadataToEnvCmdOptsSchema = metadataToEnvCmdOptsSchema;
  86. /**
  87. * Filter command line options that match a given "intent", which are specified
  88. * in the command's metadata.
  89. *
  90. * To filter options that have no intent specified in the command's metadata,
  91. * exclude the intentName parameter.
  92. *
  93. * @param metadata
  94. * @param options The options to filter.
  95. * @param indentName
  96. *
  97. * @return The filtered options.
  98. */
  99. function filterOptionsByIntent(metadata, options, intentName) {
  100. const r = Object.keys(options).reduce((allOptions, optionName) => {
  101. const metadataOptionFound = (metadata.options || []).find((mdOption) => (mdOption.name === optionName || (mdOption.aliases || []).includes(optionName)));
  102. if (metadataOptionFound) {
  103. if (intentName && metadataOptionFound.intents && metadataOptionFound.intents.includes(intentName)) {
  104. allOptions[optionName] = options[optionName];
  105. }
  106. else if (!intentName && !metadataOptionFound.intents) {
  107. allOptions[optionName] = options[optionName];
  108. }
  109. }
  110. return allOptions;
  111. }, {});
  112. r._ = options._;
  113. if (options['--']) {
  114. r['--'] = options['--'];
  115. }
  116. return r;
  117. }
  118. exports.filterOptionsByIntent = filterOptionsByIntent;