@zowe/imperative
Version:
framework for building configurable CLIs
83 lines • 3.93 kB
JavaScript
;
/*
* 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");
const ImperativeError_1 = require("../../error/src/ImperativeError");
/**
* 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, _b;
// Implementation based on the child_process module
// https://github.com/nodejs/node/blob/main/lib/child_process.js
if (result.error != null) {
throw new ImperativeError_1.ImperativeError({
msg: `Failed to launch the following command:\n ${argv.join(" ")}`,
additionalDetails: result.error.toString()
});
}
else if (result.status !== 0) {
// form a detailed message
let additionalDetails = `Command that failed:\n ${argv.join(" ")}`;
if (result.status) {
additionalDetails += `\nExit code = ${result.status.toString()}`;
}
if (((_a = result.stderr) === null || _a === void 0 ? void 0 : _a.length) > 0) {
additionalDetails += `\n\nCommand's standard error stream:\n${result.stderr.toString()}`;
}
if (((_b = result.stdout) === null || _b === void 0 ? void 0 : _b.length) > 0) {
additionalDetails += `\nCommand's standard output stream:\n${result.stdout.toString()}`;
}
// give additional tips to debug npm commands
if (argv[0].toLowerCase().includes("npm")) {
additionalDetails += "\nIf you are using an NPM registry, " +
"confirm that NPM can access the registry with the following command:\n" +
" npm view <your_desired_package_name>\n" +
"Confirm that NPM can download your package from your registry:\n" +
" npm pack <your_desired_package_name>";
}
throw new ImperativeError_1.ImperativeError({
msg: `Errors occurred in this program: ${argv[0]}`,
additionalDetails: additionalDetails
});
}
return result.stdout;
}
}
exports.ExecUtils = ExecUtils;
//# sourceMappingURL=ExecUtils.js.map