1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const cli_framework_1 = require("@ionic/cli-framework");
|
4 | const utils_process_1 = require("@ionic/utils-process");
|
5 | const utils_subprocess_1 = require("@ionic/utils-subprocess");
|
6 | const utils_terminal_1 = require("@ionic/utils-terminal");
|
7 | const chalk = require("chalk");
|
8 | const Debug = require("debug");
|
9 | const path = require("path");
|
10 | const split2 = require("split2");
|
11 | const combineStreams = require("stream-combiner2");
|
12 | const guards_1 = require("../guards");
|
13 | const color_1 = require("./color");
|
14 | const errors_1 = require("./errors");
|
15 | const debug = Debug('ionic:lib:shell');
|
16 | class Shell {
|
17 | constructor(e, options) {
|
18 | this.e = e;
|
19 | this.alterPath = options && options.alterPath ? options.alterPath : (p) => p;
|
20 | }
|
21 | async run(command, args, { stream, killOnExit = true, showCommand = true, showError = true, fatalOnNotFound = true, fatalOnError = true, truncateErrorOutput, ...crossSpawnOptions }) {
|
22 | this.prepareSpawnOptions(crossSpawnOptions);
|
23 | const proc = await this.createSubprocess(command, args, crossSpawnOptions);
|
24 | const fullCmd = proc.bashify();
|
25 | const truncatedCmd = fullCmd.length > 80 ? fullCmd.substring(0, 80) + '...' : fullCmd;
|
26 | if (showCommand && this.e.log.level >= cli_framework_1.LOGGER_LEVELS.INFO) {
|
27 | this.e.log.rawmsg(`> ${color_1.input(fullCmd)}`);
|
28 | }
|
29 | const ws = stream ? stream : this.e.log.createWriteStream(cli_framework_1.LOGGER_LEVELS.INFO, false);
|
30 | try {
|
31 | const promise = proc.run();
|
32 | if (promise.p.stdout) {
|
33 | const s = combineStreams(split2(), ws);
|
34 |
|
35 | s.on('error', (err) => {
|
36 | debug('Error in subprocess stdout pipe: %o', err);
|
37 | });
|
38 | promise.p.stdout.pipe(s);
|
39 | }
|
40 | if (promise.p.stderr) {
|
41 | const s = combineStreams(split2(), ws);
|
42 |
|
43 | s.on('error', (err) => {
|
44 | debug('Error in subprocess stderr pipe: %o', err);
|
45 | });
|
46 | promise.p.stderr.pipe(s);
|
47 | }
|
48 | if (killOnExit) {
|
49 | utils_process_1.onBeforeExit(async () => {
|
50 | if (promise.p.pid) {
|
51 | await utils_process_1.killProcessTree(promise.p.pid);
|
52 | }
|
53 | });
|
54 | }
|
55 | await promise;
|
56 | }
|
57 | catch (e) {
|
58 | if (e instanceof utils_subprocess_1.SubprocessError && e.code === utils_subprocess_1.ERROR_COMMAND_NOT_FOUND) {
|
59 | if (fatalOnNotFound) {
|
60 | throw new errors_1.FatalException(`Command not found: ${color_1.input(command)}`, 127);
|
61 | }
|
62 | else {
|
63 | throw e;
|
64 | }
|
65 | }
|
66 | if (!guards_1.isExitCodeException(e)) {
|
67 | throw e;
|
68 | }
|
69 | let err = e.message || '';
|
70 | if (truncateErrorOutput && err.length > truncateErrorOutput) {
|
71 | err = `${color_1.strong('(truncated)')} ... ` + err.substring(err.length - truncateErrorOutput);
|
72 | }
|
73 | const publicErrorMsg = (`An error occurred while running subprocess ${color_1.input(command)}.\n` +
|
74 | `${color_1.input(truncatedCmd)} exited with exit code ${e.exitCode}.\n\n` +
|
75 | `Re-running this command with the ${color_1.input('--verbose')} flag may provide more information.`);
|
76 | const privateErrorMsg = `Subprocess (${color_1.input(command)}) encountered an error (exit code ${e.exitCode}).`;
|
77 | if (fatalOnError) {
|
78 | if (showError) {
|
79 | throw new errors_1.FatalException(publicErrorMsg, e.exitCode);
|
80 | }
|
81 | else {
|
82 | throw new errors_1.FatalException(privateErrorMsg, e.exitCode);
|
83 | }
|
84 | }
|
85 | else {
|
86 | if (showError) {
|
87 | this.e.log.error(publicErrorMsg);
|
88 | }
|
89 | }
|
90 | throw e;
|
91 | }
|
92 | }
|
93 | async output(command, args, { fatalOnNotFound = true, fatalOnError = true, showError = true, showCommand = false, ...crossSpawnOptions }) {
|
94 | const proc = await this.createSubprocess(command, args, crossSpawnOptions);
|
95 | const fullCmd = proc.bashify();
|
96 | const truncatedCmd = fullCmd.length > 80 ? fullCmd.substring(0, 80) + '...' : fullCmd;
|
97 | if (showCommand && this.e.log.level >= cli_framework_1.LOGGER_LEVELS.INFO) {
|
98 | this.e.log.rawmsg(`> ${color_1.input(fullCmd)}`);
|
99 | }
|
100 | try {
|
101 | return await proc.output();
|
102 | }
|
103 | catch (e) {
|
104 | if (e instanceof utils_subprocess_1.SubprocessError && e.code === utils_subprocess_1.ERROR_COMMAND_NOT_FOUND) {
|
105 | if (fatalOnNotFound) {
|
106 | throw new errors_1.FatalException(`Command not found: ${color_1.input(command)}`, 127);
|
107 | }
|
108 | else {
|
109 | throw e;
|
110 | }
|
111 | }
|
112 | if (!guards_1.isExitCodeException(e)) {
|
113 | throw e;
|
114 | }
|
115 | const errorMsg = `An error occurred while running ${color_1.input(truncatedCmd)} (exit code ${e.exitCode})\n`;
|
116 | if (fatalOnError) {
|
117 | throw new errors_1.FatalException(errorMsg, e.exitCode);
|
118 | }
|
119 | else {
|
120 | if (showError) {
|
121 | this.e.log.error(errorMsg);
|
122 | }
|
123 | }
|
124 | return '';
|
125 | }
|
126 | }
|
127 | |
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 | async resolveCommandPath(command, options) {
|
136 | if (utils_terminal_1.TERMINAL_INFO.windows) {
|
137 | try {
|
138 | return await this.which(command, { PATH: options.env && options.env.PATH ? options.env.PATH : process.env.PATH });
|
139 | }
|
140 | catch (e) {
|
141 |
|
142 | }
|
143 | }
|
144 | return command;
|
145 | }
|
146 | async which(command, { PATH = process.env.PATH } = {}) {
|
147 | return utils_subprocess_1.which(command, { PATH: this.alterPath(PATH || '') });
|
148 | }
|
149 | async spawn(command, args, { showCommand = true, ...crossSpawnOptions }) {
|
150 | this.prepareSpawnOptions(crossSpawnOptions);
|
151 | const proc = await this.createSubprocess(command, args, crossSpawnOptions);
|
152 | const p = proc.spawn();
|
153 | if (showCommand && this.e.log.level >= cli_framework_1.LOGGER_LEVELS.INFO) {
|
154 | this.e.log.rawmsg(`> ${color_1.input(proc.bashify())}`);
|
155 | }
|
156 | return p;
|
157 | }
|
158 | async cmdinfo(command, args = []) {
|
159 | const opts = {};
|
160 | this.prepareSpawnOptions(opts);
|
161 | const proc = await this.createSubprocess(command, args, opts);
|
162 | try {
|
163 | const out = await proc.output();
|
164 | return out.split('\n').join(' ').trim();
|
165 | }
|
166 | catch (e) {
|
167 |
|
168 | }
|
169 | }
|
170 | async createSubprocess(command, args = [], options = {}) {
|
171 | const cmdpath = await this.resolveCommandPath(command, options);
|
172 | const proc = new utils_subprocess_1.Subprocess(cmdpath, args, options);
|
173 | return proc;
|
174 | }
|
175 | prepareSpawnOptions(options) {
|
176 | var _a;
|
177 |
|
178 |
|
179 |
|
180 |
|
181 | options.env = utils_process_1.createProcessEnv(process.env, (_a = options.env) !== null && _a !== void 0 ? _a : {}, {
|
182 | PATH: this.alterPath(process.env.PATH || ''),
|
183 | FORCE_COLOR: chalk.level > 0 ? '1' : '0',
|
184 | });
|
185 | }
|
186 | }
|
187 | exports.Shell = Shell;
|
188 | function prependNodeModulesBinToPath(projectDir, p) {
|
189 | return path.resolve(projectDir, 'node_modules', '.bin') + path.delimiter + p;
|
190 | }
|
191 | exports.prependNodeModulesBinToPath = prependNodeModulesBinToPath;
|