base.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.unsetConfigValue = exports.setConfigValue = exports.getConfigValue = exports.getConfig = exports.BaseConfigCommand = void 0;
  4. const tslib_1 = require("tslib");
  5. const cli_framework_1 = require("@ionic/cli-framework");
  6. const lodash = tslib_1.__importStar(require("lodash"));
  7. const util = tslib_1.__importStar(require("util"));
  8. const color_1 = require("../../lib/color");
  9. const command_1 = require("../../lib/command");
  10. const errors_1 = require("../../lib/errors");
  11. class BaseConfigCommand extends command_1.Command {
  12. generateContext(inputs, options) {
  13. const [property, v] = inputs;
  14. const global = options['global'] ? true : false;
  15. const json = options['json'] ? true : false;
  16. const force = options['force'] ? true : false;
  17. const root = options['root'] ? true : false;
  18. const value = this.interpretValue(v, json);
  19. const base = { json, property, value, force, root };
  20. if (global) {
  21. if (root) {
  22. this.env.log.warn(`${(0, color_1.input)('--root')} has no effect with ${(0, color_1.input)('--global')}: this command always operates at root for CLI config.`);
  23. }
  24. return { global, config: this.env.config, ...base };
  25. }
  26. else {
  27. if (!this.project) {
  28. throw new errors_1.FatalException(`Sorry--this won't work outside an Ionic project directory.\n` +
  29. `Did you mean to operate on global config using ${(0, color_1.input)('--global')}?`);
  30. }
  31. return { global, config: this.project.config, ...base };
  32. }
  33. }
  34. jsonStringify(v) {
  35. try {
  36. const serialized = JSON.stringify(v);
  37. if (typeof serialized === 'undefined') {
  38. throw new errors_1.FatalException(`Cannot serialize value: ${(0, color_1.strong)(v)}`);
  39. }
  40. return serialized;
  41. }
  42. catch (e) {
  43. throw new errors_1.FatalException(`Cannot serialize value: ${(0, color_1.strong)(v)}`);
  44. }
  45. }
  46. interpretValue(v, expectJson = false) {
  47. if (typeof v === 'undefined') {
  48. return;
  49. }
  50. try {
  51. // '12345e6' (a possible App ID) is interpreted as a number in scientific
  52. // notation during JSON.parse, so don't try
  53. if (!v.match(/^\d+e\d+$/)) {
  54. v = JSON.parse(v);
  55. }
  56. }
  57. catch (e) {
  58. if (e.name !== 'SyntaxError') {
  59. throw e;
  60. }
  61. if (expectJson) {
  62. throw new errors_1.FatalException(`${(0, color_1.input)('--json')}: ${(0, color_1.input)(String(v))} is invalid JSON: ${(0, color_1.failure)(e.toString())}`);
  63. }
  64. }
  65. return v;
  66. }
  67. }
  68. exports.BaseConfigCommand = BaseConfigCommand;
  69. class FlexibleConfig extends cli_framework_1.BaseConfig {
  70. provideDefaults() {
  71. return {};
  72. }
  73. }
  74. function getConfig(ctx) {
  75. return ctx.root ? new FlexibleConfig(ctx.config.p) : ctx.config;
  76. }
  77. exports.getConfig = getConfig;
  78. function getConfigValue(ctx) {
  79. const { c } = getConfig(ctx);
  80. if (ctx.global) { // Global config is flattened
  81. return ctx.property ? c[ctx.property] : c;
  82. }
  83. else {
  84. return ctx.property ? lodash.get(c, ctx.property) : c;
  85. }
  86. }
  87. exports.getConfigValue = getConfigValue;
  88. function setConfigValue(ctx) {
  89. const conf = getConfig(ctx);
  90. if (ctx.originalValue && typeof ctx.originalValue === 'object' && !ctx.force) {
  91. throw new errors_1.FatalException(`Sorry--will not override objects or arrays without ${(0, color_1.input)('--force')}.\n` +
  92. `Value of ${(0, color_1.input)(ctx.property)} is: ${(0, color_1.strong)(util.inspect(ctx.originalValue, { colors: false }))}`);
  93. }
  94. if (ctx.global) { // Global config is flattened
  95. conf.set(ctx.property, ctx.value);
  96. }
  97. else {
  98. const { c } = conf;
  99. lodash.set(c, ctx.property, ctx.value);
  100. conf.c = c;
  101. }
  102. }
  103. exports.setConfigValue = setConfigValue;
  104. function unsetConfigValue(ctx) {
  105. const conf = getConfig(ctx);
  106. if (ctx.global) { // Global config is flattened
  107. conf.unset(ctx.property);
  108. }
  109. else {
  110. const { c } = conf;
  111. lodash.unset(c, ctx.property);
  112. conf.c = c;
  113. }
  114. }
  115. exports.unsetConfigValue = unsetConfigValue;