UNPKG

@atomist/automation-client

Version:

Atomist API for software low-level client

117 lines 5.18 kB
"use strict"; /* * Copyright © 2018 Atomist, Inc. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const spawn = require("cross-spawn"); const process = require("process"); const logger_1 = require("./logger"); /** * Error thrown when a command cannot be executed, the command is * killed, or returns a non-zero exit status. * @deprecated use ExecPromiseError */ class ExecError extends Error { constructor(message, cmd, code, signal, stdout, stderr) { super(message); this.message = message; this.cmd = cmd; this.code = code; this.signal = signal; this.stdout = stdout; this.stderr = stderr; Object.setPrototypeOf(this, new.target.prototype); } } exports.ExecError = ExecError; /** * Run a child process using cross-spawn, capturing and returning * stdout and stderr, like exec, in a promise. If an error occurs, * the process is killed by a signal, or the process exits with a * non-zero status, the Promise is rejected. stdin is inherited from * the parent process. Like with child_process.exec, this is not a * good choice if the command produces a large amount of data on * stdout or stderr. * * @param {string} cmd name of command, can be a shell script or MS Windows .bat or .cmd * @param {string[]} args command arguments * @param {"child_process".SpawnOptions} opts standard spawn options * @return {Promise<ExecResult>} exec-like callback arguments having stdout and stderr properties * @deprecated use execPromise */ function safeExec(cmd, args = [], opts = {}) { return __awaiter(this, void 0, void 0, function* () { const cmdString = (opts.cwd ? opts.cwd : process.cwd()) + " ==> " + cmd + (args.length > 0 ? " '" + args.join("' '") + "'" : ""); logger_1.logger.debug(`Running: ${cmdString}`); opts.stdio = ["pipe", "pipe", "pipe"]; const childProcess = spawn(cmd, args, opts); return new Promise((resolve, reject) => { let stdout = ""; let stderr = ""; childProcess.stdout.on("data", data => { stdout += data.toString(); }); childProcess.stderr.on("data", data => { stderr += data.toString(); }); childProcess.on("close", (code, signal) => { logger_1.logger.debug(`Child process exited with code ${code} and signal ${signal}: ${cmdString}`); if (code) { const msg = `Child process ${process.pid} exited with non-zero status ${code}: ${cmdString}\n${stderr}`; reject(new ExecError(msg, cmdString, code, signal, stdout, stderr)); return; } if (signal) { const msg = `Child process ${process.pid} received signal ${signal}: ${cmdString}\n${stderr}`; reject(new ExecError(msg, cmdString, code, signal, stdout, stderr)); return; } resolve({ stdout, stderr }); }); childProcess.on("error", err => { logger_1.logger.error(`Failed to run command: ${cmdString}: ${err.message}`); reject(Object.assign(Object.assign({}, err), { cmd: cmdString, stdout, stderr })); }); }); }); } exports.safeExec = safeExec; /** * Safely exec a command in a specific directory. * * @param baseDir directory to run command in * @param cmd command to run * @param args command arguments * @return Promise of { stdout, stderr } * @deprecated use execPromise */ function execIn(baseDir, cmd, args) { return __awaiter(this, void 0, void 0, function* () { return safeExec(cmd, args, { cwd: baseDir }); }); } exports.execIn = execIn; //# sourceMappingURL=exec.js.map