command.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. var desc = Object.getOwnPropertyDescriptor(m, k);
  5. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  6. desc = { enumerable: true, get: function() { return m[k]; } };
  7. }
  8. Object.defineProperty(o, k2, desc);
  9. }) : (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. o[k2] = m[k];
  12. }));
  13. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  14. Object.defineProperty(o, "default", { enumerable: true, value: v });
  15. }) : function(o, v) {
  16. o["default"] = v;
  17. });
  18. var __importStar = (this && this.__importStar) || function (mod) {
  19. if (mod && mod.__esModule) return mod;
  20. var result = {};
  21. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  22. __setModuleDefault(result, mod);
  23. return result;
  24. };
  25. Object.defineProperty(exports, "__esModule", { value: true });
  26. exports.Command = void 0;
  27. const cli_framework_1 = require("@ionic/cli-framework");
  28. const cli_framework_output_1 = require("@ionic/cli-framework-output");
  29. const utils_terminal_1 = require("@ionic/utils-terminal");
  30. const guards_1 = require("../guards");
  31. const color_1 = require("./color");
  32. const logger_1 = require("./utils/logger");
  33. class Command extends cli_framework_1.BaseCommand {
  34. constructor(namespace) {
  35. super(namespace);
  36. this.namespace = namespace;
  37. this.taskChains = [];
  38. }
  39. get env() {
  40. return this.namespace.root.env;
  41. }
  42. get project() {
  43. return this.namespace.root.project;
  44. }
  45. createTaskChain() {
  46. let output;
  47. const formatter = (0, logger_1.createFormatter)();
  48. if (this.env.flags.interactive) {
  49. output = new cli_framework_output_1.TTYOutputStrategy({ stream: process.stdout, colors: cli_framework_1.DEFAULT_COLORS });
  50. this.env.log.handlers = new Set([new cli_framework_output_1.StreamHandler({ stream: output.stream, formatter })]);
  51. }
  52. else {
  53. this.env.log.handlers = (0, logger_1.createDefaultLoggerHandlers)();
  54. output = new cli_framework_output_1.StreamOutputStrategy({ stream: this.env.log.createWriteStream(cli_framework_output_1.LOGGER_LEVELS.INFO, false), colors: cli_framework_1.DEFAULT_COLORS });
  55. }
  56. const chain = output.createTaskChain();
  57. this.taskChains.push(chain);
  58. chain.on('end', () => {
  59. this.env.log.handlers = (0, logger_1.createDefaultLoggerHandlers)();
  60. });
  61. return chain;
  62. }
  63. async execute(inputs, options, runinfo) {
  64. if ((0, guards_1.isCommandPreRun)(this)) {
  65. await this.preRun(inputs, options, runinfo);
  66. }
  67. try {
  68. await this.validate(inputs);
  69. }
  70. catch (e) {
  71. if (!this.env.flags.interactive) {
  72. this.env.log.warn(`Command ran non-interactively due to ${(0, color_1.input)('--no-interactive')} flag, CI being detected, non-TTY, or a config setting.`);
  73. }
  74. throw e;
  75. }
  76. const runPromise = this.run(inputs, options, runinfo);
  77. const telemetryPromise = (async () => {
  78. if (this.env.config.get('telemetry') !== false && !utils_terminal_1.TERMINAL_INFO.ci && utils_terminal_1.TERMINAL_INFO.tty) {
  79. const { Telemetry } = await Promise.resolve().then(() => __importStar(require('./telemetry')));
  80. let cmdInputs = [];
  81. const metadata = await this.getMetadata();
  82. if (metadata.name === 'login' || metadata.name === 'logout') {
  83. // This is a hack to wait until the selected commands complete before
  84. // sending telemetry data. These commands update `this.env` in some
  85. // way, which is used in the `Telemetry` instance.
  86. await runPromise;
  87. }
  88. else if (metadata.name === 'completion') {
  89. // Ignore telemetry for these commands.
  90. return;
  91. }
  92. else if (metadata.name === 'help') {
  93. cmdInputs = inputs;
  94. }
  95. else {
  96. cmdInputs = await this.getCleanInputsForTelemetry(inputs, options);
  97. }
  98. const cmd = this;
  99. const path = await (0, cli_framework_1.generateCommandPath)(cmd);
  100. const telemetry = new Telemetry({ client: this.env.client, config: this.env.config, getInfo: this.env.getInfo, ctx: this.env.ctx, project: this.project, session: this.env.session });
  101. await telemetry.sendCommand(path.map(([p]) => p).join(' '), cmdInputs);
  102. }
  103. })();
  104. await Promise.all([runPromise, telemetryPromise]);
  105. }
  106. async getCleanInputsForTelemetry(inputs, options) {
  107. const initialOptions = { _: [] };
  108. const metadata = await this.getMetadata();
  109. const filteredInputs = inputs.map((input, i) => metadata.inputs && (metadata.inputs[i] && metadata.inputs[i].private) ? '*****' : input);
  110. const filteredOptions = Object.keys(options)
  111. .filter(optionName => {
  112. if (optionName === '_') {
  113. return false;
  114. }
  115. const metadataOption = metadata.options && metadata.options.find(o => {
  116. return o.name === optionName || (typeof o.aliases !== 'undefined' && o.aliases.includes(optionName));
  117. });
  118. if (metadataOption && metadataOption.aliases && metadataOption.aliases.includes(optionName)) {
  119. return false; // exclude aliases
  120. }
  121. if (!metadataOption) {
  122. return true; // include unknown options
  123. }
  124. if (metadataOption.private) {
  125. return false; // exclude private options
  126. }
  127. if (typeof metadataOption.default !== 'undefined' && metadataOption.default === options[optionName]) {
  128. return false; // exclude options that match their default value (means it wasn't supplied by user)
  129. }
  130. return true;
  131. })
  132. .reduce((allOptions, optionName) => {
  133. allOptions[optionName] = options[optionName];
  134. return allOptions;
  135. }, initialOptions);
  136. const optionInputs = (0, cli_framework_1.unparseArgs)(filteredOptions, { useDoubleQuotes: true });
  137. return filteredInputs.concat(optionInputs);
  138. }
  139. }
  140. exports.Command = Command;