import * as path from "path" ; import * as fs from "fs" ; import { IO , Logger , IHeaderContent , AbstractSession , ImperativeExpect , Headers } from "@zowe/imperative" ; import { ZosFilesConstants } from "../constants/ZosFiles.constants" ; import { ZosFilesMessages } from "../constants/ZosFiles.messages" ; import { IZosFilesResponse } from "../doc/IZosFilesResponse" ; import { ZosmfRestClient , ZosmfHeaders } from "@zowe/core-for-zowe-sdk" ; import { IDeleteOptions } from "../methods/hDelete" ; import { IOptions } from "../doc/IOptions" ; /**
 * Common IO utilities
 */ export class ZosFilesUtils { type { string } public static readonly DSN_SEP : string = "." ; type { string } public static readonly DEFAULT_FILE_EXTENSION : string = "txt" ; public static readonly MAX_MEMBER_LENGTH : number = 8 ; param { string } dataSet public static getDirsFromDataSet ( dataSet ) { let localDirectory = dataSet . replace ( new RegExp ( `\\${ this . DSN_SEP }` , "g" ) , IO . FILE_DELIM ) . toLowerCase ( ) ; if ( localDirectory . indexOf ( "(" ) >= 0 && localDirectory . indexOf ( ")" ) >= 0 ) { localDirectory = localDirectory . replace ( /\(/ , IO . FILE_DELIM ) ; localDirectory = localDirectory . slice ( 0 , - 1 ) ; } return localDirectory ; } param { string } inputPath return { string } public 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 ; } param { string } inputPath param { boolean } inFullPathFormat param { boolean } isIgnoreHidden return { string [ ] } public static getFileListFromPath ( inputPath , inFullPathFormat = true , isIgnoreHidden = true ) { const returnFileList = [ ] ; const fullpath = this . getFullPath ( inputPath ) ; if ( 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 { } return returnFileList ; } private static param { IOptions } options returns { IHeaderContent [ ] } memberof public static generateHeadersBasedOnOptions ( options ) { const reqHeaders = [ ] ; if ( options . binary ) { reqHeaders . push ( ZosmfHeaders . X_IBM_BINARY ) ; } else if ( options . encoding ) { const keys = Object . keys ( ZosmfHeaders . X_IBM_TEXT ) ; const value = ZosmfHeaders . X_IBM_TEXT [ keys [ 0 ] ] + ZosmfHeaders . X_IBM_TEXT_ENCODING + options . encoding ; const header = Object . create ( ZosmfHeaders . X_IBM_TEXT ) ; header [ keys [ 0 ] ] = value ; reqHeaders . push ( header ) ; } else { } if ( ! options . binary ) { reqHeaders . push ( ZosmfHeaders . ACCEPT_ENCODING ) ; } if ( options . responseTimeout != null ) { reqHeaders . push ( { [ ZosmfHeaders . X_IBM_RESPONSE_TIMEOUT ] : options . responseTimeout . toString ( ) } ) ; } return reqHeaders ; } param { string } fileName return { string } public static generateMemberName ( fileName ) { let memberName = path . basename ( fileName ) . toUpperCase ( ) ; memberName = memberName . replace ( path . extname ( memberName ) , "" ) ; memberName = memberName . replace ( /[^A-Z0-9@#$]/g , "" ) ; memberName = memberName . replace ( /[\d]/gy , "" ) ; memberName = memberName . substr ( 0 , this . MAX_MEMBER_LENGTH ) ; return memberName ; } param { string } dataSetName return { boolean } public static isDataSetNameContainMasking ( dataSetName ) { let returnStatus = false ; if ( dataSetName . match ( /[*%]/g ) ) { returnStatus = true ; } return returnStatus ; } param { Buffer } buffer return { Buffer } public static normalizeNewline ( buffer ) { return Buffer . from ( buffer . toString ( ) . replace ( /\r\n/g , "\n" ) ) ; } param ussPath public static sanitizeUssPathForRestCall ( ussPath ) { let sanitizedPath = path . posix . normalize ( ussPath ) ; if ( sanitizedPath . charAt ( 0 ) === "/" ) { sanitizedPath = sanitizedPath . substring ( 1 ) ; } return encodeURIComponent ( sanitizedPath ) ; } param { string } usspath public static formatUnixFilepath ( usspath ) { if ( usspath . charAt ( 0 ) === "/" ) { usspath = usspath . substring ( 1 ) ; } return usspath ; } param { AbstractSession } session param { string } dataSetName param { string } returnMessage param { any } hsmCommand param { IRecallOptions | IMigrateOptions | IDeleteOptions } options public static async dfsmsHsmCommand ( session , dataSetName , returnMessage , hsmCommand , options = { } ) { ImperativeExpect . toNotBeNullOrUndefined ( dataSetName , ZosFilesMessages . missingDatasetName . message ) ; ImperativeExpect . toNotBeEqual ( dataSetName , "" , ZosFilesMessages . missingDatasetName . message ) ; try { const endpoint = path . posix . join ( ZosFilesConstants . RESOURCE , ZosFilesConstants . RES_DS_FILES , dataSetName ) ; Logger . getAppLogger ( ) . debug ( `Endpoint: ${ endpoint }` ) ; const payload = hsmCommand ; if ( options . wait != null ) { payload . wait = options . wait ; } if ( options . purge != null ) { payload . purge = options . purge ; } const headers = [ Headers . APPLICATION_JSON , { "Content-Length" : JSON . stringify ( payload ) . length . toString ( ) } , ZosmfHeaders . ACCEPT_ENCODING ] ; if ( options . responseTimeout != null ) { headers . push ( { [ ZosmfHeaders . X_IBM_RESPONSE_TIMEOUT ] : options . responseTimeout . toString ( ) } ) ; } await ZosmfRestClient . putExpectString ( session , endpoint , headers , payload ) ; return { success : true , commandResponse : returnMessage } ; } catch ( error ) { Logger . getAppLogger ( ) . error ( error ) ; throw error ; } } }