UNPKG

@zowe/imperative

Version:
139 lines 8.18 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. * */ 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 }); exports.BaseAutoInitHandler = void 0; const rest_1 = require("../../../../../../rest"); const config_1 = require("../../../../../../config"); const jest_diff_1 = require("jest-diff"); const JSONC = require("comment-json"); const lodash = require("lodash"); const utilities_1 = require("../../../../../../utilities"); const OverridesLoader_1 = require("../../../../OverridesLoader"); const stripAnsi = require("strip-ansi"); /** * This class is used by the auto init command handler as the base class for its implementation. */ class BaseAutoInitHandler { /** * This handler is used for the "auto-init" command, and calls processAutoInit * * @param {IHandlerParameters} commandParameters Command parameters sent by imperative. * * @returns {Promise<void>} */ process(commandParameters) { return __awaiter(this, void 0, void 0, function* () { yield this.processAutoInit(commandParameters); }); } /** * Processes the auto init command to the auto init service. * Applies the changes to whichever config layer is specified by IHandlerParameters. * Can also perform a dry run and display the changes, or open the config for editing. * @param {IHandlerParameters} params Command parameters sent by imperative. */ processAutoInit(params) { return __awaiter(this, void 0, void 0, function* () { const sessCfg = this.createSessCfgFromArgs(params.arguments); const sessCfgWithCreds = yield rest_1.ConnectionPropsForSessCfg.addPropsOrPrompt(sessCfg, params.arguments, { parms: params, doPrompting: true, serviceDescription: this.mServiceDescription }); this.mSession = new rest_1.Session(sessCfgWithCreds); if (this.mSession.ISession.tokenValue) { this.mSession.ISession.base64EncodedAuth = this.mSession.ISession.user = this.mSession.ISession.password = this.mSession.ISession.cert = this.mSession.ISession.certKey = undefined; } // Use params to set which config layer to apply to yield OverridesLoader_1.OverridesLoader.ensureCredentialManagerLoaded(); const configDir = params.arguments.globalConfig ? null : process.cwd(); utilities_1.ImperativeConfig.instance.config.api.layers.activate(params.arguments.userConfig, params.arguments.globalConfig, configDir); // Call handler's implementation of auto-init const profileConfig = yield this.doAutoInit(this.mSession, params); if (params.arguments.dryRun && params.arguments.dryRun === true) { // Merge and display, do not save // TODO preserve comments // Handle if the file doesn't actually exist let original = utilities_1.ImperativeConfig.instance.config.api.layers.get(); let originalProperties; if (original.exists === false) { originalProperties = {}; } else { // Typecasting because of this issue: https://github.com/kaelzhang/node-comment-json/issues/42 originalProperties = JSONC.parse(JSONC.stringify(original.properties, null, config_1.ConfigConstants.INDENT)); // Hide secure stuff for (const secureProp of utilities_1.ImperativeConfig.instance.config.api.secure.secureFields(original)) { if (lodash.has(originalProperties, secureProp)) { lodash.unset(originalProperties, secureProp); } } } let dryRun = utilities_1.ImperativeConfig.instance.config.api.layers.merge(profileConfig, true); // Typecasting because of this issue: https://github.com/kaelzhang/node-comment-json/issues/42 const dryRunProperties = JSONC.parse(JSONC.stringify(dryRun.properties, null, config_1.ConfigConstants.INDENT)); // Hide secure stuff for (const secureProp of utilities_1.ImperativeConfig.instance.config.api.secure.findSecure(dryRun.properties.profiles, "profiles")) { if (lodash.has(dryRunProperties, secureProp)) { lodash.unset(dryRunProperties, secureProp); } } original = JSONC.stringify(originalProperties, null, config_1.ConfigConstants.INDENT); dryRun = JSONC.stringify(dryRunProperties, null, config_1.ConfigConstants.INDENT); let jsonDiff = (0, jest_diff_1.diff)(original, dryRun, { aAnnotation: "Removed", bAnnotation: "Added", aColor: utilities_1.TextUtils.chalk.red, bColor: utilities_1.TextUtils.chalk.green }); if (stripAnsi(jsonDiff) === "Compared values have no visual difference.") { jsonDiff = dryRun; } params.response.console.log(jsonDiff); params.response.data.setObj(jsonDiff); } else if (params.arguments.overwrite && params.arguments.overwrite === true) { if (params.arguments.forSure && params.arguments.forSure === true) { // Clear layer, merge, generate schema, and save utilities_1.ImperativeConfig.instance.config.api.layers.set(profileConfig); const schema = config_1.ConfigSchema.buildSchema(utilities_1.ImperativeConfig.instance.loadedConfig.profiles); utilities_1.ImperativeConfig.instance.config.setSchema(schema); yield utilities_1.ImperativeConfig.instance.config.save(); } } else { // Merge, generate schema, and save utilities_1.ImperativeConfig.instance.config.api.layers.merge(profileConfig); if (utilities_1.ImperativeConfig.instance.config.api.layers.get().properties.$schema == null) { // TODO What condition should we use to decide whether to (re)generate schema? const schema = config_1.ConfigSchema.buildSchema(utilities_1.ImperativeConfig.instance.loadedConfig.profiles); utilities_1.ImperativeConfig.instance.config.setSchema(schema); } yield utilities_1.ImperativeConfig.instance.config.save(); } // we only display changes if we made changes if (!params.arguments.dryRun || params.arguments.dryRun === false) { this.displayAutoInitChanges(params.response); if (params.arguments.edit && params.arguments.edit === true) { yield utilities_1.ProcessUtils.openInEditor(utilities_1.ImperativeConfig.instance.config.api.layers.get().path, params.arguments.editor); } } }); } } exports.BaseAutoInitHandler = BaseAutoInitHandler; //# sourceMappingURL=BaseAutoInitHandler.js.map