import * as fs from "fs" ; import { AbstractSession , Headers , ImperativeError , ImperativeExpect , Logger } from "@zowe/imperative" ; import { posix } from "path" ; import * as util from "util" ; import { IHeaderContent , ZosmfRestClient , ZosmfHeaders , getErrorContext } from "@zowe/core-for-zowe-sdk" ; import { ZosFilesConstants } from "../../constants/ZosFiles.constants" ; import { ZosFilesMessages } from "../../constants/ZosFiles.messages" ; import { IZosFilesResponse } from "../../doc/IZosFilesResponse" ; import { IZosFilesOptions } from "../../doc/IZosFilesOptions" ; /**
 * This class holds helper functions that are used to execute AMS control statements through the z/OS MF APIs
 */ export class Invoke { param { AbstractSession } session param { string | string [ ] } controlStatements returns { Promise < IZosFilesResponse > } throws throws {Error} When the  ZosmfRestClient throws an error
     *
     *  see https public static async ams ( session , controlStatements , options ? ) { ImperativeExpect . toNotBeNullOrUndefined ( controlStatements , ZosFilesMessages . missingStatements . message ) ; ImperativeExpect . toNotBeEqual ( controlStatements . length , 0 , ZosFilesMessages . missingStatements . message ) ; let statements ; if ( typeof controlStatements === "string" ) { try { statements = fs . readFileSync ( controlStatements ) . toString ( ) . toUpperCase ( ) . split ( /\r?\n/ ) ; } catch ( error ) { Logger . getAppLogger ( ) . error ( error ) ; throw error ; } } else { statements = controlStatements . map ( ( stmt ) => stmt . toUpperCase ( ) ) ; } const longStmtIndex = statements . findIndex ( ( stmt ) => stmt . length > ZosFilesConstants . MAX_AMS_LINE ) ; if ( longStmtIndex >= 0 ) { throw new ImperativeError ( { msg : util . format ( ZosFilesMessages . longAmsStatement . message , longStmtIndex + 1 , ZosFilesConstants . MAX_AMS_LINE , getErrorContext ( statements , longStmtIndex ) ) } ) ; } try { const endpoint = posix . join ( ZosFilesConstants . RESOURCE , ZosFilesConstants . RES_AMS ) ; Logger . getAppLogger ( ) . debug ( `Endpoint: ${ endpoint }` ) ; const reqPayload = { input : statements } ; const reqHeaders = [ Headers . APPLICATION_JSON , { [ Headers . CONTENT_LENGTH ] : JSON . stringify ( reqPayload ) . length . toString ( ) } , ZosmfHeaders . ACCEPT_ENCODING ] ; if ( options && options . responseTimeout != null ) { reqHeaders . push ( { [ ZosmfHeaders . X_IBM_RESPONSE_TIMEOUT ] : options . responseTimeout . toString ( ) } ) ; } const response = await ZosmfRestClient . putExpectJSON ( session , endpoint , reqHeaders , reqPayload ) ; return { success : true , commandResponse : ZosFilesMessages . amsCommandExecutedSuccessfully . message , apiResponse : response } ; } catch ( error ) { Logger . getAppLogger ( ) . error ( error ) ; throw error ; } } }