@zowe/imperative
Version:
framework for building configurable CLIs
180 lines • 7.39 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.V1ProfileRead = void 0;
const error_1 = require("../../../error");
const utilities_1 = require("../../../utilities");
const fs = require("fs");
const pathPackage = require("path");
const readYaml = require("js-yaml");
/**
* The V1ProfileRead class is for reading V1 profiles from disk.
* V1 profiles are not supported as of Zowe V3. Profiles should only be read
* for the purpose of converting the V1 profiles to a current zowe client configuration.
* All "fs" calls are wrapped here and errors are transformed to ImperativeError for
* error handling/flow throughout Imperative.
*
* @export
* @class V1ProfileRead
* @internal
*/
class V1ProfileRead {
/**
* Read the profile meta file using Yaml "safeLoad" (ensures that no code executes, etc. during the load). The
* meta profile file for a type contains the default profile specification. The meta profile is ALWAYS in YAML
* format (controlled exclusively by the Imperative framework).
* @static
* @param {string} path - The path to the meta profile
* @returns {IMetaProfile} - The meta profile
* @memberof V1ProfileRead
*/
static readMetaFile(path) {
V1ProfileRead.crashInTeamConfigMode();
let meta;
try {
meta = readYaml.load(fs.readFileSync(path), "utf8");
}
catch (err) {
throw new error_1.ImperativeError({
msg: `Error reading profile file ("${path}"). Error Details: ${err.message}`,
additionalDetails: err
}, { tag: V1ProfileRead.ERROR_ID });
}
return meta;
}
/**
* Accepts the profiles root directory and returns all directories within. The directories within the root
* directory are all assumed to be profile type directories (potentially containing a meta file and profiles
* of that type).
* @static
* @param {string} profileRootDirectory - The profiles root directory to obtain all profiles from.
* @returns {string[]} - The list of profiles returned or a blank array
* @memberof V1ProfileRead
*/
static getAllProfileDirectories(profileRootDirectory) {
V1ProfileRead.crashInTeamConfigMode();
let names = [];
try {
names = fs.readdirSync(profileRootDirectory);
names = names.filter((name) => {
// only return directories, not files
const stats = fs.statSync(pathPackage.join(profileRootDirectory, name));
return stats.isDirectory();
});
}
catch (e) {
throw new error_1.ImperativeError({
msg: `An error occurred attempting to read all profile directories from "${profileRootDirectory}". ` +
`Error Details: ${e.message}`,
additionalDetails: e
}, { tag: V1ProfileRead.ERROR_ID });
}
return names;
}
/**
* Accepts the profile directory location for a type, reads all filenames, and returns a list of
* profile names that are present within the directory (excluding the meta profile)
* @static
* @param {string} profileTypeDir - The directory for the type
* @param {string} ext - the extension for the profile files (normally YAML)
* @param {string} metaNameForType - the meta name for this type
* @returns {string[]} - A list of all profile names (without path/ext)
* @memberof V1ProfileRead
*/
static getAllProfileNames(profileTypeDir, ext, metaNameForType) {
V1ProfileRead.crashInTeamConfigMode();
const names = [];
try {
let profileFiles = fs.readdirSync(profileTypeDir);
profileFiles = profileFiles.filter((file) => {
const fullFile = pathPackage.resolve(profileTypeDir, file);
const isYamlFile = fullFile.length > ext.length && fullFile.substring(fullFile.length - ext.length) === ext;
return isYamlFile && V1ProfileRead.fileToProfileName(fullFile, ext) !== metaNameForType;
});
for (const file of profileFiles) {
names.push(V1ProfileRead.fileToProfileName(file, ext));
}
}
catch (e) {
throw new error_1.ImperativeError({
msg: `An error occurred attempting to read all profile names from "${profileTypeDir}". ` +
`Error Details: ${e.message}`,
additionalDetails: e
}, { tag: V1ProfileRead.ERROR_ID });
}
return names;
}
/**
* Read a profile from disk. Profiles are always assumed to be YAML (YAML "safeLoad" is invoked to perform the load).
* @static
* @param {string} filePath - Path to the profile.
* @param {string} type - The profile type; used to populate the "type" in the profile object (type property not persisted on disk).
* @returns {IProfile} - The profile object.
* @memberof V1ProfileRead
*/
static readProfileFile(filePath, _type) {
V1ProfileRead.crashInTeamConfigMode();
let profile;
try {
profile = readYaml.load(fs.readFileSync(filePath, "utf8"));
}
catch (err) {
throw new error_1.ImperativeError({
msg: `Error reading profile file ("${filePath}"). Error Details: ${err.message}`,
additionalDetails: err
}, { tag: V1ProfileRead.ERROR_ID });
}
return profile;
}
/**
* Crash if we detect that we are running in team-config mode.
* You should not be able to operate on old-school profiles
* when you are in team-config mode. Give a meaningful
* message as part of our crash.
*/
static crashInTeamConfigMode() {
var _a;
if ((_a = utilities_1.ImperativeConfig.instance.config) === null || _a === void 0 ? void 0 : _a.exists) {
try {
throw new Error("Attempted to convert a Zowe V1 profile when a newer Zowe client configuration already exists.");
}
catch (err) {
throw new error_1.ImperativeError({
msg: err.message,
additionalDetails: err.stack,
}, { tag: V1ProfileRead.ERROR_ID });
}
}
}
/**
* Extracts the profile name from the file path/name
* @static
* @param {string} file - the file path to extract the profile name
* @param {string} ext - the extension of the file
* @returns {string} - the profile name
* @memberof V1ProfileRead
*/
static fileToProfileName(file, ext) {
file = pathPackage.basename(file);
return file.substring(0, file.lastIndexOf(ext));
}
}
exports.V1ProfileRead = V1ProfileRead;
/**
* Error IO tag for Imperative Errors
* @private
* @static
* @type {string}
* @memberof V1ProfileRead
*/
V1ProfileRead.ERROR_ID = "V1ProfileRead Read Error";
//# sourceMappingURL=V1ProfileRead.js.map