UNPKG

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