@zowe/imperative
Version:
framework for building configurable CLIs
110 lines • 5 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.
*
*/
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.ConfigBuilder = void 0;
const lodash = require("lodash");
const Config_1 = require("./Config");
const ConfigUtils_1 = require("./ConfigUtils");
class ConfigBuilder {
/**
* Build a new Config object from an Imperative CLI app configuration.
* @param impConfig The Imperative CLI app configuration.
* @param globalConfig Is the config to be a global config?
* @param opts Options to control aspects of the builder.
*/
static build(impConfig, globalConfig, opts) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
opts = opts || {};
const builtConfig = Config_1.Config.empty();
for (const profile of impConfig.profiles) {
const defaultProfile = ConfigBuilder.buildDefaultProfile(profile, opts);
// Name our profile differently in a global config vs a project config
const profileName = ConfigUtils_1.ConfigUtils.formGlobOrProjProfileNm(profile.type, globalConfig);
// Add the profile to config and set it as default
lodash.set(builtConfig, `profiles.${profileName}`, defaultProfile);
if (opts.populateProperties) {
builtConfig.defaults[profile.type] = profileName;
}
}
// Prompt for properties missing from base profile
if (impConfig.baseProfile != null && opts.getValueBack != null) {
const baseProfileNm = ConfigUtils_1.ConfigUtils.formGlobOrProjProfileNm(impConfig.baseProfile.type, globalConfig);
for (const [k, v] of Object.entries(impConfig.baseProfile.schema.properties)) {
if (v.includeInTemplate && ((_a = v.optionDefinition) === null || _a === void 0 ? void 0 : _a.defaultValue) == null) {
const propValue = yield opts.getValueBack(k, v);
if (propValue != null) {
lodash.set(builtConfig, `profiles.${baseProfileNm}.properties.${k}`, propValue);
}
}
}
}
return Object.assign(Object.assign({}, builtConfig), { autoStore: true });
});
}
static buildDefaultProfile(profile, opts) {
const properties = {};
const secureProps = [];
for (const [k, v] of Object.entries(profile.schema.properties)) {
if (opts.populateProperties && v.includeInTemplate) {
if (v.secure) {
secureProps.push(k);
}
else {
if (v.optionDefinition != null) {
// Use default value of ICommandOptionDefinition if present
properties[k] = v.optionDefinition.defaultValue;
}
if (properties[k] === undefined) {
// Fall back to an empty value
properties[k] = this.getDefaultValue(v.type);
}
}
}
}
return {
type: profile.type,
properties,
secure: secureProps
};
}
/**
* Returns empty value that is appropriate for the property type.
* @param propType The type of profile property
* @returns Null or empty object
*/
static getDefaultValue(propType) {
// TODO How to handle profile property with multiple types
if (Array.isArray(propType)) {
propType = propType[0];
}
switch (propType) {
case "string": return "";
case "number": return 0;
case "object": return {};
case "array": return [];
case "boolean": return false;
default: return null;
}
}
}
exports.ConfigBuilder = ConfigBuilder;
//# sourceMappingURL=ConfigBuilder.js.map
;