"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.prependNodeModulesBinToPath = exports.Shell = void 0; const cli_framework_output_1 = require("@ionic/cli-framework-output"); const utils_process_1 = require("@ionic/utils-process"); const utils_subprocess_1 = require("@ionic/utils-subprocess"); const utils_terminal_1 = require("@ionic/utils-terminal"); const chalk = require("chalk"); const Debug = require("debug"); const path = require("path"); const split2 = require("split2"); const combineStreams = require("stream-combiner2"); const guards_1 = require("../guards"); const color_1 = require("./color"); const errors_1 = require("./errors"); const debug = Debug('ionic:lib:shell'); class Shell { constructor(e, options) { this.e = e; this.alterPath = options && options.alterPath ? options.alterPath : (p) => p; } async run(command, args, { stream, killOnExit = true, showCommand = true, showError = true, fatalOnNotFound = true, fatalOnError = true, truncateErrorOutput, ...crossSpawnOptions }) { this.prepareSpawnOptions(crossSpawnOptions); const proc = await this.createSubprocess(command, args, crossSpawnOptions); const fullCmd = proc.bashify(); const truncatedCmd = fullCmd.length > 80 ? fullCmd.substring(0, 80) + '...' : fullCmd; if (showCommand && this.e.log.level >= cli_framework_output_1.LOGGER_LEVELS.INFO) { this.e.log.rawmsg(`> ${color_1.input(fullCmd)}`); } const ws = stream ? stream : this.e.log.createWriteStream(cli_framework_output_1.LOGGER_LEVELS.INFO, false); try { const promise = proc.run(); if (promise.p.stdout) { const s = combineStreams(split2(), ws); // TODO: https://github.com/angular/angular-cli/issues/10922 s.on('error', (err) => { debug('Error in subprocess stdout pipe: %o', err); }); promise.p.stdout.pipe(s); } if (promise.p.stderr) { const s = combineStreams(split2(), ws); // TODO: https://github.com/angular/angular-cli/issues/10922 s.on('error', (err) => { debug('Error in subprocess stderr pipe: %o', err); }); promise.p.stderr.pipe(s); } if (killOnExit) { utils_process_1.onBeforeExit(async () => { if (promise.p.pid) { await utils_process_1.killProcessTree(promise.p.pid); } }); } await promise; } catch (e) { if (e instanceof utils_subprocess_1.SubprocessError && e.code === utils_subprocess_1.ERROR_COMMAND_NOT_FOUND) { if (fatalOnNotFound) { throw new errors_1.FatalException(`Command not found: ${color_1.input(command)}`, 127); } else { throw e; } } if (!guards_1.isExitCodeException(e)) { throw e; } let err = e.message || ''; if (truncateErrorOutput && err.length > truncateErrorOutput) { err = `${color_1.strong('(truncated)')} ... ` + err.substring(err.length - truncateErrorOutput); } const publicErrorMsg = (`An error occurred while running subprocess ${color_1.input(command)}.\n` + `${color_1.input(truncatedCmd)} exited with exit code ${e.exitCode}.\n\n` + `Re-running this command with the ${color_1.input('--verbose')} flag may provide more information.`); const privateErrorMsg = `Subprocess (${color_1.input(command)}) encountered an error (exit code ${e.exitCode}).`; if (fatalOnError) { if (showError) { throw new errors_1.FatalException(publicErrorMsg, e.exitCode); } else { throw new errors_1.FatalException(privateErrorMsg, e.exitCode); } } else { if (showError) { this.e.log.error(publicErrorMsg); } } throw e; } } async output(command, args, { fatalOnNotFound = true, fatalOnError = true, showError = true, showCommand = false, ...crossSpawnOptions }) { const proc = await this.createSubprocess(command, args, crossSpawnOptions); const fullCmd = proc.bashify(); const truncatedCmd = fullCmd.length > 80 ? fullCmd.substring(0, 80) + '...' : fullCmd; if (showCommand && this.e.log.level >= cli_framework_output_1.LOGGER_LEVELS.INFO) { this.e.log.rawmsg(`> ${color_1.input(fullCmd)}`); } try { return await proc.output(); } catch (e) { if (e instanceof utils_subprocess_1.SubprocessError && e.code === utils_subprocess_1.ERROR_COMMAND_NOT_FOUND) { if (fatalOnNotFound) { throw new errors_1.FatalException(`Command not found: ${color_1.input(command)}`, 127); } else { throw e; } } if (!guards_1.isExitCodeException(e)) { throw e; } const errorMsg = `An error occurred while running ${color_1.input(truncatedCmd)} (exit code ${e.exitCode})\n`; if (fatalOnError) { throw new errors_1.FatalException(errorMsg, e.exitCode); } else { if (showError) { this.e.log.error(errorMsg); } } return ''; } } /** * When `child_process.spawn` isn't provided a full path to the command * binary, it behaves differently on Windows than other platforms. For * Windows, discover the full path to the binary, otherwise fallback to the * command provided. * * @see https://github.com/ionic-team/ionic-cli/issues/3563#issuecomment-425232005 */ async resolveCommandPath(command, options) { if (utils_terminal_1.TERMINAL_INFO.windows) { try { return await this.which(command, { PATH: options.env && options.env.PATH ? options.env.PATH : process.env.PATH }); } catch (e) { // ignore } } return command; } async which(command, { PATH = process.env.PATH } = {}) { return utils_subprocess_1.which(command, { PATH: this.alterPath(PATH || '') }); } async spawn(command, args, { showCommand = true, ...crossSpawnOptions }) { this.prepareSpawnOptions(crossSpawnOptions); const proc = await this.createSubprocess(command, args, crossSpawnOptions); const p = proc.spawn(); if (showCommand && this.e.log.level >= cli_framework_output_1.LOGGER_LEVELS.INFO) { this.e.log.rawmsg(`> ${color_1.input(proc.bashify())}`); } return p; } async cmdinfo(command, args = []) { const opts = {}; this.prepareSpawnOptions(opts); const proc = await this.createSubprocess(command, args, opts); try { const out = await proc.output(); return out.split('\n').join(' ').trim(); } catch (e) { // no command info at this point } } async createSubprocess(command, args = [], options = {}) { const cmdpath = await this.resolveCommandPath(command, options); const proc = new utils_subprocess_1.Subprocess(cmdpath, args, options); return proc; } prepareSpawnOptions(options) { var _a; // Create a `process.env`-type object from all key/values of `process.env`, // then `options.env`, then add several key/values. PATH is supplemented // with the `node_modules\.bin` folder in the project directory so that we // can run binaries inside a project. options.env = utils_process_1.createProcessEnv(process.env, (_a = options.env) !== null && _a !== void 0 ? _a : {}, { PATH: this.alterPath(process.env.PATH || ''), FORCE_COLOR: chalk.level > 0 ? '1' : '0', }); } } exports.Shell = Shell; function prependNodeModulesBinToPath(projectDir, p) { return path.resolve(projectDir, 'node_modules', '.bin') + path.delimiter + p; } exports.prependNodeModulesBinToPath = prependNodeModulesBinToPath;
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
doctor | Folder | 0755 |
|
|
integrations | Folder | 0755 |
|
|
oauth | Folder | 0755 |
|
|
project | Folder | 0755 |
|
|
utils | Folder | 0755 |
|
|
app.d.ts | File | 1.15 KB | 0644 |
|
app.js | File | 2.5 KB | 0644 |
|
build.d.ts | File | 3.43 KB | 0644 |
|
build.js | File | 8.51 KB | 0644 |
|
color.d.ts | File | 484 B | 0644 |
|
color.js | File | 768 B | 0644 |
|
command.d.ts | File | 924 B | 0644 |
|
command.js | File | 5.47 KB | 0644 |
|
config.d.ts | File | 843 B | 0644 |
|
config.js | File | 3.83 KB | 0644 |
|
cordova-res.d.ts | File | 1.07 KB | 0644 |
|
cordova-res.js | File | 2.62 KB | 0644 |
|
diff.d.ts | File | 100 B | 0644 |
|
diff.js | File | 709 B | 0644 |
|
environment.d.ts | File | 1004 B | 0644 |
|
environment.js | File | 516 B | 0644 |
|
errors.d.ts | File | 891 B | 0644 |
|
errors.js | File | 1.77 KB | 0644 |
|
events.d.ts | File | 240 B | 0644 |
|
events.js | File | 498 B | 0644 |
|
executor.d.ts | File | 1 KB | 0644 |
|
executor.js | File | 3.7 KB | 0644 |
|
generate.d.ts | File | 923 B | 0644 |
|
generate.js | File | 351 B | 0644 |
|
git.d.ts | File | 747 B | 0644 |
|
git.js | File | 1.66 KB | 0644 |
|
help.d.ts | File | 2.4 KB | 0644 |
|
help.js | File | 3.66 KB | 0644 |
|
helper.d.ts | File | 252 B | 0644 |
|
helper.js | File | 654 B | 0644 |
|
hooks.d.ts | File | 841 B | 0644 |
|
hooks.js | File | 4.72 KB | 0644 |
|
http.d.ts | File | 2.62 KB | 0644 |
|
http.js | File | 7.56 KB | 0644 |
|
index.d.ts | File | 226 B | 0644 |
|
index.js | File | 6.09 KB | 0644 |
|
ionitron.d.ts | File | 148 B | 0644 |
|
ionitron.js | File | 4.72 KB | 0644 |
|
namespace.d.ts | File | 854 B | 0644 |
|
namespace.js | File | 686 B | 0644 |
|
native-run.d.ts | File | 1.5 KB | 0644 |
|
native-run.js | File | 4.98 KB | 0644 |
|
open.d.ts | File | 164 B | 0644 |
|
open.js | File | 507 B | 0644 |
|
prompts.d.ts | File | 402 B | 0644 |
|
prompts.js | File | 716 B | 0644 |
|
security.d.ts | File | 511 B | 0644 |
|
security.js | File | 785 B | 0644 |
|
serve.d.ts | File | 5.79 KB | 0644 |
|
serve.js | File | 22.4 KB | 0644 |
|
session.d.ts | File | 921 B | 0644 |
|
session.js | File | 7.82 KB | 0644 |
|
shell.d.ts | File | 1.95 KB | 0644 |
|
shell.js | File | 8.41 KB | 0644 |
|
snapshot.d.ts | File | 765 B | 0644 |
|
snapshot.js | File | 1.13 KB | 0644 |
|
ssh-config.d.ts | File | 646 B | 0644 |
|
ssh-config.js | File | 3.04 KB | 0644 |
|
ssh.d.ts | File | 1.67 KB | 0644 |
|
ssh.js | File | 3.53 KB | 0644 |
|
start.d.ts | File | 1.34 KB | 0644 |
|
start.js | File | 12.1 KB | 0644 |
|
telemetry.d.ts | File | 972 B | 0644 |
|
telemetry.js | File | 3.59 KB | 0644 |
|
updates.d.ts | File | 1.01 KB | 0644 |
|
updates.js | File | 3.82 KB | 0644 |
|
user.d.ts | File | 865 B | 0644 |
|
user.js | File | 2.37 KB | 0644 |
|