UNPKG

@zowe/imperative

Version:
61 lines 2.53 kB
"use strict"; /* * This program and the accompanying materials are made available under the terms of the * Eclipse Public License v2.0 which accompanies this distribution, and is available at * https://www.eclipse.org/legal/epl-v20.html * * SPDX-License-Identifier: EPL-2.0 * * Copyright Contributors to the Zowe Project. * */ Object.defineProperty(exports, "__esModule", { value: true }); exports.ExecUtils = void 0; const spawn = require("cross-spawn"); /** * A collection of utilities related to executing a sub-process. * @export * @class ExecUtils */ class ExecUtils { /** * Spawn a process with arguments and throw an error if the process fails. * Parameters are same as `child_process.spawnSync` (see Node.js docs). * Use this method if you want the safe argument parsing of `spawnSync` * combined with the smart output handling of `execSync`. * @returns Contents of stdout as buffer or string */ static spawnAndGetOutput(command, args, options) { const result = spawn.sync(command, args, options); return this.handleSpawnResult(result, [command, ...args !== null && args !== void 0 ? args : []]); } /** * Spawn a process with arguments and throw an error if the process fails. * Parameters are same as `child_process.spawnSync` (see Node.js docs). * Output is inherited by the parent process instead of being returned. * Use this method if you want the safe argument parsing of `spawnSync` * combined with the smart output handling of `execSync`. */ static spawnWithInheritedStdio(command, args, options) { const result = spawn.sync(command, args, Object.assign(Object.assign({}, options), { stdio: "inherit" })); return this.handleSpawnResult(result, [command, ...args !== null && args !== void 0 ? args : []]); } static handleSpawnResult(result, argv) { var _a; // Implementation based on the child_process module // https://github.com/nodejs/node/blob/main/lib/child_process.js if (result.error != null) { throw result.error; } else if (result.status !== 0) { let msg = `Command failed: ${argv.join(" ")}`; if (((_a = result.stderr) === null || _a === void 0 ? void 0 : _a.length) > 0) { msg += `\n${result.stderr.toString()}`; } throw new Error(msg); } return result.stdout; } } exports.ExecUtils = ExecUtils; //# sourceMappingURL=ExecUtils.js.map