@zowe/imperative
Version:
framework for building configurable CLIs
119 lines • 5.24 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.EnvFileUtils = void 0;
const fs_1 = require("fs");
const os_1 = require("os");
const path_1 = require("path");
const ImperativeError_1 = require("../../error/src/ImperativeError");
const EnvironmentalVariableSettings_1 = require("../../imperative/src/env/EnvironmentalVariableSettings");
const JSONC = require("comment-json");
/**
* Utility to load environment JSON files and set variables
* @export
* @class EnvFileUtils
*/
class EnvFileUtils {
/**
* Check and read in an environment file from the user home directory using the app name
* If the file is valid, set the environment variables
* If the file is not valid, display an error and continue
* @param {string} appName - The application name
* @param {boolean} checkCliHomeVariableFirst - Check inside of *_CLI_HOME first if it is defined
* @param {string} envPrefix - The environment variable prefix
* @returns {void}
* @throws {ImperativeError}
*/
static setEnvironmentForApp(appName, checkCliHomeVariableFirst = false, envPrefix) {
const expectedFileLocation = this.getEnvironmentFilePath(appName, checkCliHomeVariableFirst, envPrefix);
if (expectedFileLocation) {
try {
const fileContents = (0, fs_1.readFileSync)(expectedFileLocation).toString(); // Read the file in
// Typecasting because of this issue: https://github.com/kaelzhang/node-comment-json/issues/42
const fileContentsJSON = JSONC.parse(fileContents);
this.environmentJSON = fileContentsJSON;
this.resetEnvironmentForApp();
}
catch (err) {
let errorMessage = "Failed to set up environment variables from the environment file.\n" +
"Environment variables will not be available.\nFile: " + expectedFileLocation;
if (err.line != null && err.column != null) {
errorMessage += "\nLine: " + err.line.toString() + "\nColumn: " + err.column.toString();
}
throw new ImperativeError_1.ImperativeError({ msg: errorMessage, causeErrors: err });
}
}
}
/**
* Reapply environment variables that were applied before
* @returns {void}
* @throws {ImperativeError}
*/
static resetEnvironmentForApp() {
Object.keys(this.environmentJSON).forEach(key => {
process.env[key] = this.environmentJSON[key];
});
}
/**
* Get the expected path for the user's environment variable file
* @param {string} appName - The application name
* @param {boolean} checkCliHomeVariableFirst - Check inside of *_CLI_HOME first if it is defined
* @param {string} envPrefix - environment variable prefix
* @returns {string} - Returns the path string if it exists, or null if it does not
*/
static getEnvironmentFilePath(appName, checkCliHomeVariableFirst = false, envPrefix) {
if (checkCliHomeVariableFirst) {
const cliHome = this.getCliHomeEnvironmentFilePath(appName, envPrefix);
if (cliHome) {
return cliHome;
}
}
return this.getUserHomeEnvironmentFilePath(appName);
}
/**
* Get the expected path for the user's environment variable file
* @param {string} appName - The application name
* @returns {string} - Returns the path string if it exists, or null if it does not
*/
static getUserHomeEnvironmentFilePath(appName) {
const expectedBasename = "." + appName + ".env.json";
const path = (0, path_1.join)((0, os_1.homedir)(), expectedBasename);
if ((0, fs_1.existsSync)(path)) {
return path;
}
return null;
}
/**
* Get the expected path for the user's environment variable file
* @param {string} appName - The application name
* @param {string} envPrefix - The environment variable prefix
* @returns {string} - Returns the path string if it exists, or null if it does not
*/
static getCliHomeEnvironmentFilePath(appName, envPrefix) {
const environmentVariable = (envPrefix ? envPrefix : appName.toUpperCase()) + EnvironmentalVariableSettings_1.EnvironmentalVariableSettings.CLI_HOME_SUFFIX;
const expectedBasename = "." + appName + ".env.json";
const expectedDirectory = process.env[environmentVariable];
if (expectedDirectory) {
const path = (0, path_1.join)(expectedDirectory, expectedBasename);
if ((0, fs_1.existsSync)(path)) {
return path;
}
}
return null;
}
}
exports.EnvFileUtils = EnvFileUtils;
/**
* This variable holds a cached version of the EnvFileJson.
*/
EnvFileUtils.environmentJSON = {};
//# sourceMappingURL=EnvFileUtils.js.map
;