@zowe/imperative
Version:
framework for building configurable CLIs
247 lines • 8.32 kB
JavaScript
"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.ImperativeConfig = void 0;
const constants_1 = require("../../constants");
const path_1 = require("path");
const error_1 = require("../../error");
const EnvironmentalVariableSettings_1 = require("../../imperative/src/env/EnvironmentalVariableSettings");
const find_up_1 = require("find-up");
/**
* This class is used to contain all configuration being set by Imperative.
* It is a singleton and should be accessed via ImperativeConfig.instance.
*/
class ImperativeConfig {
constructor() {
/**
* This parameter is used as the container of all loaded configuration for
* Imperative.
*/
this.mLoadedConfig = null;
/**
* This parameter is used to contain the caller location of imperative configuration file.
*/
this.mCallerLocation = null;
}
/**
* Gets a single instance of the ImperativeConfig. On the first call of
* ImperativeConfig.instance, a new ImperativeConfig object is initialized and returned.
* Every subsequent call will use the one that was first created.
*
* @returns {ImperativeConfig} The newly initialized ImperativeConfig object.
*/
static get instance() {
if (this.mInstance == null) {
this.mInstance = new ImperativeConfig();
}
return this.mInstance;
}
/**
* Get the configured environmental variable prefix for the user's CLI
* @returns {string} - the configured or default prefix for environmental variables for use in the environmental variable service
*/
get envVariablePrefix() {
if (this.loadedConfig) {
return this.loadedConfig.envVariablePrefix == null ? this.loadedConfig.name : this.loadedConfig.envVariablePrefix;
}
else {
return "ZOWE"; // Turns out this doesn't always return something, so set a default.
}
}
/**
* Set the caller location.
* @param {string} location new location to be updated with
*/
set callerLocation(location) {
this.mCallerLocation = location;
}
/**
* Return file location of imperative configuration file.
* @returns {streturnsring} - location of configuration file
*/
get callerLocation() {
return this.mCallerLocation;
}
/**
* Set the loaded config data.
* @param {IImperativeConfig} config to be set.
*/
set loadedConfig(config) {
this.mLoadedConfig = config;
}
/**
* Retrieve the loaded config (if init has
* @returns {IImperativeConfig} - the config that has been loaded, if any
*/
get loadedConfig() {
return this.mLoadedConfig;
}
/**
* Set our root command name.
* @param rootCommandName - The name of our calling CLI's command.
*/
set rootCommandName(rootCommandName) {
this.mRootCommandName = rootCommandName;
}
/**
* Get our root command name.
* @returns The name of our calling CLI's command.
*/
get rootCommandName() {
return this.mRootCommandName;
}
/**
* Retrieve the host package name from which imperative was called.
*/
get hostPackageName() {
if (!this.mHostPackageName) {
this.mHostPackageName = this.callerPackageJson.name;
}
return this.mHostPackageName;
}
/**
* Retrieve the package name of the imperative application.
*/
get imperativePackageName() {
if (!this.mImperativePackageName) {
this.mImperativePackageName = require((0, path_1.join)(__dirname, "../../../package.json")).name;
}
return this.mImperativePackageName;
}
/**
* Parses the package.json file and searches for the symlink name used under "bin".
* @returns {string} - return bin symlink name if present, otherwise null
*/
findPackageBinName() {
const pkg = this.callerPackageJson;
if (typeof pkg.bin === "string") {
return pkg.name;
}
else if (typeof pkg.bin === "object") {
return Object.keys(pkg.bin).pop();
}
return null;
}
/**
* Return the cli Home path.
* @return {string} path to cli Home.
*/
get cliHome() {
const settings = EnvironmentalVariableSettings_1.EnvironmentalVariableSettings.read(this.envVariablePrefix);
if (settings.cliHome.value != null) {
return settings.cliHome.value;
}
return this.loadedConfig.defaultHome;
}
/**
* Return profile Directory.
* @return {string} profile directory.
*/
get profileDir() {
return this.loadedConfig.defaultHome + constants_1.Constants.PROFILES_DIR + "/";
}
/**
* Return package.json of the imperative user
* @returns {any} - package.json file of caller
*/
get callerPackageJson() {
return this.getCallerFile("package.json");
}
/**
* Retrieve the command line.
* @example
* For example, in "banana a b --c", "a b --c" is the command line.
* @returns {string} - command line
*/
get commandLine() {
return this.mCommandLine;
}
/**
* Set the command line (needed for daemon where command changes and is not static)
* @memberof Imperative
*/
set commandLine(args) {
this.mCommandLine = args;
}
/**
* Set context for daemon to retrieve if no handler is called.
* @type {IDaemonContext}
* @memberof ImperativeConfig
*/
get daemonContext() {
return this.mDaemonContext;
}
/**
* Context for daemon when no handler is invoked.
* @memberof ImperativeConfig
*/
set daemonContext(context) {
this.mDaemonContext = context;
}
/**
* Set the config
*/
set config(c) {
this.mConfig = c;
}
/**
* Get the config properties
*/
get config() {
return this.mConfig;
}
/**
* Require a file from a project using imperative accounting for imperative being contained
* separately from the current implementers directory.
* @param {string} file - the file to require from project using imperative
*/
getCallerFile(file) {
// try to locate the file using find-up first
let findupErr;
try {
const filePath = (0, find_up_1.sync)(file, { cwd: ImperativeConfig.instance.callerLocation });
return require(filePath);
}
catch (e) {
// couldn't locate using find-up, try to require directly
findupErr = e;
}
// if we couldn't find the file path through find-up, try requiring the string directly
try {
return require(file);
}
catch (e) {
e.message = "Could not locate the specified module through requiring directly, nor through " +
"searching the directories above " + ImperativeConfig.instance.callerLocation +
". 'require()' error message: " + e.message +
" \n 'find-up' (directory search) error message:" + findupErr.message;
throw new error_1.ImperativeError({ msg: e.message });
}
}
/**
* @returns a key/value object where the key is the profile type and the
* value is the profile type schema
*/
get profileSchemas() {
const schemas = {};
if (ImperativeConfig.instance.loadedConfig.profiles != null)
ImperativeConfig.instance.loadedConfig.profiles.forEach(profile => schemas[profile.type] = profile.schema);
return schemas;
}
}
exports.ImperativeConfig = ImperativeConfig;
/**
* This is the variable that stores the specific instance of Imperative Config.
* Defined as static so that it can be accessed from anywhere.
*/
ImperativeConfig.mInstance = null;
//# sourceMappingURL=ImperativeConfig.js.map