@zowe/imperative
Version:
framework for building configurable CLIs
87 lines • 3.57 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.runValidatePlugin = runValidatePlugin;
const logger_1 = require("../../../../logger");
const utilities_1 = require("../../../../utilities");
const PMFConstants_1 = require("./PMFConstants");
const error_1 = require("../../../../error");
/**
* Run another instance of the host CLI command to validate a plugin that has
* just been installed. We use a separate process instead of an API call
* because when the user re-installs an existing plugin we cannot validate
* if the plugin has conflicting command names because the plugin has
* already been incorporated into the Imperative command tree, and thus it
* could conflict with its own commands. However, if we run a validate command
* in a new process, we start with a clean slate and we get accurate results.
*
* @param pluginName - The name of a plugin to be validated.
*
* @returns - The text output of the validate plugin command.
*/
function runValidatePlugin(pluginName) {
var _a;
const extLen = 3;
const cmdToRun = process.execPath;
const cliPgmToRun = (_a = utilities_1.ImperativeConfig.instance.callerLocation) !== null && _a !== void 0 ? _a : require.main.filename;
let cmdToRunArgs = [];
if (cliPgmToRun.substring(cliPgmToRun.length - extLen) === ".ts") {
cmdToRunArgs = ["--require", "ts-node/register"];
}
cmdToRunArgs.push(cliPgmToRun);
const impLogger = logger_1.Logger.getImperativeLogger();
impLogger.debug(`Running plugin validation command = ${cmdToRun} plugins validate "${pluginName}" --response-format-json --no-fail-on-error`);
const valOutputJsonTxt = utilities_1.ExecUtils.spawnAndGetOutput(cmdToRun, [
...cmdToRunArgs,
"plugins", "validate", pluginName,
"--response-format-json",
"--no-fail-on-error"
], {
cwd: PMFConstants_1.PMFConstants.instance.PMF_ROOT
}).toString();
// Debug trace information
impLogger.trace(`Command Output: ${valOutputJsonTxt}`);
let valResultJsonObj;
try {
valResultJsonObj = JSON.parse(valOutputJsonTxt);
}
catch (thrownErr) {
throw new error_1.ImperativeError({
msg: "Unable to parse the JSON output of the 'plugins validate' command." +
"\nReason = " + thrownErr.message +
"\nText that was being parsed:\n" + valOutputJsonTxt
});
}
return formValidateMsg(valResultJsonObj);
}
// _______________________________________________________________________
/**
* Form the final validation message. We concatenate the stderr and stdout
* of the validation command.
*
* @param {string} valResultJsonObj - The output of plugin validation command.
*
* @returns {String} The final message to be displayed to the end user.
*/
function formValidateMsg(valResultJsonObj) {
const validateOutput = valResultJsonObj.stdout;
const validateErr = valResultJsonObj.stderr;
let fullMsg = "";
if (validateErr && validateErr.length > 0) {
fullMsg += validateErr + "\n";
}
if (validateOutput && validateOutput.length > 0) {
fullMsg += validateOutput + "\n";
}
return fullMsg;
}
//# sourceMappingURL=runValidatePlugin.js.map