@zowe/cli
Version:
Zowe CLI is a command line interface (CLI) that provides a simple and streamlined way to interact with IBM z/OS.
163 lines • 6.04 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 });
const path = require("path");
const fs = require("fs");
const imperative_1 = require("@zowe/imperative");
/**
* Common IO utilities
*/
class ZosFilesUtils {
/**
* Break up a dataset name of either:
* USER.WORK.JCL(TEMPLATE) to user/work/jcl/template
* Or:
* USER.WORK.PS to user/work/ps
* @param {string} dataSet - data set to break up into folders
*/
static getDirsFromDataSet(dataSet) {
let localDirectory = dataSet.replace(new RegExp(`\\${this.DSN_SEP}`, "g"), imperative_1.IO.FILE_DELIM).toLowerCase();
if (localDirectory.indexOf("(") >= 0 && localDirectory.indexOf(")") >= 0) {
localDirectory = localDirectory.replace(/\(/, imperative_1.IO.FILE_DELIM);
localDirectory = localDirectory.slice(0, -1);
}
return localDirectory;
}
/**
* Get fullpath name from input path.
* @param {string} inputPath - input path
* @return {string} full path version of the input path
*/
static getFullPath(inputPath) {
let fullPath = path.normalize(inputPath);
if (fullPath.indexOf(":\\") !== -1 || fullPath.indexOf("/") === 0) {
fullPath = path.normalize(fullPath);
}
else {
fullPath = path.resolve(process.cwd(), fullPath);
}
return fullPath;
}
/**
* Return an array contain the list of all files in the input path. It does not trevor to
* directory in the input path.
* @param {string} inputPath input path to gather file list
* @param {boolean} [inFullPathFormat=true] is the return file path in full path mode flag
* @param {boolean} [isIgnoreHidden=true] is listing hidden files flag
* @return {string[]} Array of all files finds in path
*/
static getFileListFromPath(inputPath, inFullPathFormat = true, isIgnoreHidden = true) {
const returnFileList = [];
const fullpath = this.getFullPath(inputPath);
if (imperative_1.IO.isDir(fullpath)) {
const fileList = fs.readdirSync(fullpath);
fileList.forEach((file) => {
const tempPath = path.resolve(fullpath, file);
if (fs.lstatSync(tempPath).isFile()) {
if (!(isIgnoreHidden && path.basename(file).startsWith("."))) {
if (inFullPathFormat) {
returnFileList.push(tempPath);
}
else {
returnFileList.push(file);
}
}
}
});
}
else if (fs.lstatSync(fullpath).isFile()) {
if (inFullPathFormat) {
returnFileList.push(fullpath);
}
else {
returnFileList.push(inputPath);
}
}
else {
// todo add handler for symplink here
}
return returnFileList;
}
/**
* Generate member name from input string base on IBM specification
* @param {string} fileName input name used to generate member name with
* @return {string} generated member name
*/
static generateMemberName(fileName) {
let memberName = path.basename(fileName).toUpperCase();
// Remove extention
memberName = memberName.replace(path.extname(memberName), "");
// First character must be either a letter or #, @, $.
memberName = memberName.replace(/[^A-Z0-9@#$]/g, "");
// Member also does not allow to start with a number
memberName = memberName.replace(/[\d]/gy, "");
// Trunkage lenght to max lenght allowed
memberName = memberName.substr(0, this.MAX_MEMBER_LENGTH);
return memberName;
}
/**
* Check if the input data set name contain masking characters
* @param {string} dataSetName input data set name to be checked
* @return {boolean} status if data set name contain masking characters
*/
static isDataSetNameContainMasking(dataSetName) {
let returnStatus = false;
if (dataSetName.match(/[*%]/g)) {
returnStatus = true;
}
return returnStatus;
}
/**
* Normalize all Windows newline to Unix newline
* @param {Buffer} buffer data to convert
* @return {Buffer} converted data
*/
static normalizeNewline(buffer) {
return Buffer.from(buffer.toString().replace(new RegExp("\r\n", "g"), "\n"));
}
/**
* Normanize and URL-encode a USS path to be passed to z/OSMF
* @param ussPath path to sanitize
*/
static sanitizeUssPathForRestCall(ussPath) {
let sanitizedPath = path.posix.normalize(ussPath);
if (sanitizedPath.charAt(0) === "/") {
// trim leading slash from unix files - API doesn't like it
sanitizedPath = sanitizedPath.substring(1);
}
return encodeURIComponent(sanitizedPath);
}
/**
* Format USS filepaths in the way that the APIs expect (no leading /)
* @param {string} usspath - the path to format
*/
static formatUnixFilepath(usspath) {
if (usspath.charAt(0) === "/") {
// trim leading slash from unix files - API doesn't like it
usspath = usspath.substring(1);
}
return usspath;
}
}
exports.ZosFilesUtils = ZosFilesUtils;
/**
* Data set name qualifier separator
* @type {string}
*/
ZosFilesUtils.DSN_SEP = ".";
/**
* Default file extension
* @type {string}
*/
ZosFilesUtils.DEFAULT_FILE_EXTENSION = "txt";
ZosFilesUtils.MAX_MEMBER_LENGTH = 8;
//# sourceMappingURL=ZosFilesUtils.js.map