index.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.receive = exports.run = exports.loadExecutor = exports.generateContext = void 0;
  4. const tslib_1 = require("tslib");
  5. const cli_framework_1 = require("@ionic/cli-framework");
  6. const node_1 = require("@ionic/cli-framework/utils/node");
  7. const utils_process_1 = require("@ionic/utils-process");
  8. const debug_1 = require("debug");
  9. const path = tslib_1.__importStar(require("path"));
  10. const util = tslib_1.__importStar(require("util"));
  11. const commands_1 = require("./commands");
  12. const guards_1 = require("./guards");
  13. const lib_1 = require("./lib");
  14. const color_1 = require("./lib/color");
  15. const executor_1 = require("./lib/executor");
  16. tslib_1.__exportStar(require("./constants"), exports);
  17. tslib_1.__exportStar(require("./guards"), exports);
  18. tslib_1.__exportStar(require("./definitions"), exports);
  19. const debug = (0, debug_1.debug)('ionic');
  20. const PACKAGE_ROOT_PATH = __dirname;
  21. const PACKAGE_JSON_PATH = path.resolve(PACKAGE_ROOT_PATH, 'package.json');
  22. let _pkg;
  23. let _executor;
  24. async function loadPackageJson() {
  25. if (!_pkg) {
  26. _pkg = await (0, node_1.readPackageJsonFile)(PACKAGE_JSON_PATH);
  27. }
  28. return _pkg;
  29. }
  30. async function generateContext() {
  31. const pkg = await loadPackageJson();
  32. if (!pkg.bin || !pkg.bin.ionic) {
  33. throw new Error(`Missing "${(0, color_1.strong)('bin.ionic')}" in Ionic CLI package.json`);
  34. }
  35. if (!pkg.main) {
  36. throw new Error(`Missing "${(0, color_1.strong)('main')}" in Ionic CLI package.json`);
  37. }
  38. return {
  39. binPath: path.resolve(PACKAGE_ROOT_PATH, pkg.bin.ionic),
  40. libPath: PACKAGE_ROOT_PATH,
  41. execPath: process.cwd(),
  42. version: pkg.version,
  43. };
  44. }
  45. exports.generateContext = generateContext;
  46. async function loadExecutor(ctx, pargv) {
  47. if (!_executor) {
  48. const deps = await (0, lib_1.generateIonicEnvironment)(ctx, pargv);
  49. const namespace = new commands_1.IonicNamespace(deps);
  50. _executor = new executor_1.Executor({ namespace });
  51. }
  52. return _executor;
  53. }
  54. exports.loadExecutor = loadExecutor;
  55. async function authenticateFromEnvironment(ienv) {
  56. const token = process.env['IONIC_TOKEN'];
  57. const email = process.env['IONIC_EMAIL'];
  58. const password = process.env['IONIC_PASSWORD'];
  59. if (token) {
  60. const wasLoggedIn = ienv.session.isLoggedIn();
  61. debug(`${(0, color_1.strong)('IONIC_TOKEN')} environment variable detected`);
  62. if (ienv.config.get('tokens.user') !== token) {
  63. debug(`${(0, color_1.strong)('IONIC_TOKEN')} mismatch with current session--attempting login`);
  64. await ienv.session.tokenLogin(token);
  65. if (wasLoggedIn) {
  66. ienv.log.info(`You have been logged out--using ${(0, color_1.strong)('IONIC_TOKEN')} environment variable`);
  67. }
  68. }
  69. }
  70. else if (email && password) {
  71. debug(`${(0, color_1.strong)('IONIC_EMAIL')} / ${(0, color_1.strong)('IONIC_PASSWORD')} environment variables detected`);
  72. if (ienv.config.get('user.email') !== email) {
  73. debug(`${(0, color_1.strong)('IONIC_EMAIL')} mismatch with current session--attempting login`);
  74. ienv.log.warn('Authenticating using email and password is deprecated. Please generate a Personal Access Token and set the ' +
  75. (0, color_1.strong)('IONIC_TOKEN') +
  76. ' environment variable.');
  77. ienv.log.nl();
  78. try {
  79. await ienv.session.login(email, password);
  80. }
  81. catch (e) {
  82. ienv.log.error(`Error occurred during automatic login via ${(0, color_1.strong)('IONIC_EMAIL')} / ${(0, color_1.strong)('IONIC_PASSWORD')} environment variables.`);
  83. throw e;
  84. }
  85. }
  86. }
  87. }
  88. async function run(pargv) {
  89. let err;
  90. let executor;
  91. try {
  92. executor = await loadExecutor(await generateContext(), pargv);
  93. }
  94. catch (e) {
  95. process.stderr.write(`${e.message ? e.message : (e.stack ? e.stack : e)}\n`);
  96. process.exitCode = 1;
  97. return;
  98. }
  99. const ienv = executor.namespace.env;
  100. if (pargv[0] === '_') {
  101. return;
  102. }
  103. try {
  104. debug('Context: %o', ienv.ctx);
  105. ienv.config.set('version', ienv.ctx.version);
  106. const location = await executor.locate(pargv);
  107. const [, [cmd = ''] = []] = location.path;
  108. if (!['config', 'completion', 'help', 'login', 'logout', 'version'].includes(cmd)) {
  109. await authenticateFromEnvironment(ienv);
  110. }
  111. await executor.execute(location, process.env);
  112. }
  113. catch (e) {
  114. err = e;
  115. }
  116. finally {
  117. if (ienv.flags.interactive) {
  118. const { runUpdateNotify } = await Promise.resolve().then(() => tslib_1.__importStar(require('./lib/updates')));
  119. await runUpdateNotify(ienv, await loadPackageJson());
  120. }
  121. }
  122. if (err) {
  123. process.exitCode = 1;
  124. if (err instanceof cli_framework_1.InputValidationError) {
  125. for (const e of err.errors) {
  126. ienv.log.error(e.message);
  127. }
  128. ienv.log.msg(`Use the ${(0, color_1.input)('--help')} flag for more details.`);
  129. }
  130. else if ((0, guards_1.isSuperAgentError)(err)) {
  131. const { formatSuperAgentError } = await Promise.resolve().then(() => tslib_1.__importStar(require('./lib/http')));
  132. ienv.log.rawmsg(formatSuperAgentError(err));
  133. }
  134. else if (err.code && err.code === 'ENOTFOUND' || err.code === 'ECONNREFUSED') {
  135. ienv.log.error(`Network connectivity error occurred, are you offline?\n` +
  136. `If you are behind a firewall and need to configure proxy settings, see: ${(0, color_1.strong)('https://ion.link/cli-proxy-docs')}\n\n` +
  137. (0, color_1.failure)(String(err.stack ? err.stack : err)));
  138. }
  139. else if ((0, guards_1.isExitCodeException)(err)) {
  140. if (err.message) {
  141. if (err.exitCode > 0) {
  142. ienv.log.error(err.message);
  143. }
  144. else {
  145. ienv.log.msg(err.message);
  146. }
  147. }
  148. await (0, utils_process_1.processExit)(err.exitCode);
  149. }
  150. else if (err instanceof cli_framework_1.BaseError) {
  151. ienv.log.error(err.message);
  152. }
  153. else {
  154. ienv.log.rawmsg((0, color_1.failure)(util.inspect(err)));
  155. }
  156. }
  157. }
  158. exports.run = run;
  159. async function receive(msg) {
  160. if (!_executor) {
  161. throw new Error('Executor not initialized.');
  162. }
  163. const { env, project } = _executor.namespace;
  164. if (msg.type === 'telemetry') {
  165. const { sendCommand } = await Promise.resolve().then(() => tslib_1.__importStar(require('./lib/telemetry')));
  166. await sendCommand({
  167. getInfo: env.getInfo,
  168. client: env.client,
  169. config: env.config,
  170. ctx: env.ctx,
  171. project,
  172. session: env.session,
  173. }, msg.data.command, msg.data.args);
  174. }
  175. else if (msg.type === 'update-check') {
  176. const { runUpdateCheck } = await Promise.resolve().then(() => tslib_1.__importStar(require('./lib/updates')));
  177. await runUpdateCheck(env);
  178. }
  179. }
  180. exports.receive = receive;