set.js 4.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.ConfigSetCommand = void 0;
  4. const cli_framework_1 = require("@ionic/cli-framework");
  5. const utils_terminal_1 = require("@ionic/utils-terminal");
  6. const constants_1 = require("../../constants");
  7. const color_1 = require("../../lib/color");
  8. const errors_1 = require("../../lib/errors");
  9. const base_1 = require("./base");
  10. class ConfigSetCommand extends base_1.BaseConfigCommand {
  11. async getMetadata() {
  12. const projectFile = this.project ? (0, utils_terminal_1.prettyPath)(this.project.filePath) : constants_1.PROJECT_FILE;
  13. return {
  14. name: 'set',
  15. type: 'global',
  16. summary: 'Set config values',
  17. description: `
  18. This command writes configuration values to the project's ${(0, color_1.strong)((0, utils_terminal_1.prettyPath)(projectFile))} file. It can also operate on the global CLI configuration (${(0, color_1.strong)('~/.ionic/config.json')}) using the ${(0, color_1.input)('--global')} option.
  19. For nested properties, separate nest levels with dots. For example, the property name ${(0, color_1.input)('integrations.cordova')} will look in the ${(0, color_1.strong)('integrations')} object for the ${(0, color_1.strong)('cordova')} property.
  20. For multi-app projects, this command is scoped to the current project by default. To operate at the root of the project configuration file instead, use the ${(0, color_1.input)('--root')} option.
  21. This command will attempt to coerce ${(0, color_1.input)('value')} into a suitable JSON type. If it is JSON-parsable, such as ${(0, color_1.input)('123')}, ${(0, color_1.input)('true')}, ${(0, color_1.input)('[]')}, etc., then it takes the parsed result. Otherwise, the value is interpreted as a string. For stricter input, use ${(0, color_1.input)('--json')}, which will error with non-JSON values.
  22. By default, if ${(0, color_1.input)('property')} exists and is an object or an array, the value is not overwritten. To disable this check and always overwrite the property, use ${(0, color_1.input)('--force')}.
  23. `,
  24. inputs: [
  25. {
  26. name: 'property',
  27. summary: 'The property name you wish to set',
  28. validators: [cli_framework_1.validators.required],
  29. },
  30. {
  31. name: 'value',
  32. summary: 'The new value of the given property',
  33. validators: [cli_framework_1.validators.required],
  34. },
  35. ],
  36. options: [
  37. {
  38. name: 'global',
  39. summary: 'Use global CLI config',
  40. type: Boolean,
  41. aliases: ['g'],
  42. },
  43. {
  44. name: 'json',
  45. summary: `Always interpret ${(0, color_1.input)('value')} as JSON`,
  46. type: Boolean,
  47. groups: ["advanced" /* MetadataGroup.ADVANCED */],
  48. },
  49. {
  50. name: 'force',
  51. summary: 'Always overwrite existing values',
  52. type: Boolean,
  53. groups: ["advanced" /* MetadataGroup.ADVANCED */],
  54. },
  55. {
  56. name: 'root',
  57. summary: `Operate on root of ${(0, color_1.strong)((0, utils_terminal_1.prettyPath)(projectFile))}`,
  58. type: Boolean,
  59. hint: (0, color_1.weak)('[multi-app]'),
  60. groups: ["advanced" /* MetadataGroup.ADVANCED */],
  61. },
  62. ],
  63. exampleCommands: ['name newAppName', 'name "\\"newAppName\\"" --json', '-g interactive false'],
  64. };
  65. }
  66. async run(inputs, options) {
  67. const ctx = this.generateContext(inputs, options);
  68. const { property } = ctx;
  69. if (typeof property === 'undefined') {
  70. throw new errors_1.FatalException(`Cannot set config to ${(0, color_1.input)(ctx.value)} without a property.`);
  71. }
  72. const originalValue = (0, base_1.getConfigValue)(ctx);
  73. (0, base_1.setConfigValue)({ ...ctx, property, originalValue });
  74. if (ctx.value !== originalValue) {
  75. this.env.log.ok(`${(0, color_1.input)(property)} set to ${(0, color_1.input)(JSON.stringify(ctx.value))}!`);
  76. }
  77. else {
  78. this.env.log.info(`${(0, color_1.input)(property)} is already set to ${(0, color_1.input)(JSON.stringify(ctx.value))}.`);
  79. }
  80. }
  81. }
  82. exports.ConfigSetCommand = ConfigSetCommand;