import { AbstractSession , ImperativeExpect , IO , Logger } from "@zowe/imperative" ; import { JobsConstants } from "./JobsConstants" ; import { IDownloadAllSpoolContentParms } from "./doc/input/IDownloadAllSpoolContentParms" ; import { IJobFile } from "./doc/response/IJobFile" ; import { ZosmfRestClient } from "@zowe/core-for-zowe-sdk" ; import { IDownloadSpoolContentParms } from "./doc/input/IDownloadSpoolContentParms" ; import { GetJobs } from "./GetJobs" ; export class export class DownloadJobs { static type { string } memberof public static readonly DEFAULT_JOBS_OUTPUT_DIR : string = "./output" ; static type { string } memberof public static readonly DEFAULT_JOBS_OUTPUT_FILE_EXT : string = ".txt" ; static param { AbstractSession } session param jobFile returns { Promise < string > } memberof public static async downloadSpoolContent ( session , jobFile ) { this . log . trace ( "Entered downloadSpoolContent for job file %s" , JSON . stringify ( jobFile ) ) ; return DownloadJobs . downloadSpoolContentCommon ( session , { jobFile } ) ; } static param { AbstractSession } session param { IDownloadAllSpoolContentParms } parms returns { Promise < void > } memberof public static async downloadAllSpoolContentCommon ( session , parms ) { this . log . trace ( "Entering downloadAllSpoolContentCommon with parms %s" , JSON . stringify ( parms ) ) ; ImperativeExpect . keysToBeDefined ( parms , [ "jobid" , "jobname" ] , "You must specify job ID and job name on your" + " 'parms' object to the downloadAllSpoolContent API." ) ; this . log . debug ( "Downloading all spool content for job %s(%s)" , parms . jobname , parms . jobid ) ; const jobFiles = await GetJobs . getSpoolFiles ( session , parms . jobname , parms . jobid ) ; const usedStepNames = { } ; for ( const file of jobFiles ) { let uniqueDDName = file . ddname ; if ( usedStepNames [ file . stepname ] != null ) { let index = 1 ; while ( usedStepNames [ file . stepname ] . indexOf ( uniqueDDName ) !== - 1 ) { uniqueDDName = `${ file . ddname }(${ index })` ; index ++ ; } } await DownloadJobs . downloadSpoolContentCommon ( session , { jobFile : { ... file , ddname : uniqueDDName } , outDir : parms . outDir , omitJobidDirectory : parms . omitJobidDirectory } ) ; usedStepNames [ file . stepname ] = [ ... usedStepNames [ file . stepname ] || [ ] , uniqueDDName ] ; } } static param { AbstractSession } session param { IDownloadSpoolContentParms } parms returns { Promise < void > } memberof public static async downloadSpoolContentCommon ( session , parms ) { this . log . trace ( "Entering downloadSpoolContentCommon with parms %s" , JSON . stringify ( parms ) ) ; ImperativeExpect . keysToBeDefined ( parms , [ "jobFile" ] , "You must specify a job file on your 'parms' parameter" + " object to the downloadSpoolContentCommon API." ) ; if ( parms . outDir == null ) { parms . outDir = DownloadJobs . DEFAULT_JOBS_OUTPUT_DIR ; } const file = DownloadJobs . getSpoolDownloadFile ( parms . jobFile , parms . omitJobidDirectory , parms . outDir ) ; this . log . debug ( "Downloading spool file %s for job %s(%s) to file %s" , parms . jobFile . ddname , parms . jobFile . jobname , parms . jobFile . jobid , file ) ; IO . createDirsSyncFromFilePath ( file ) ; IO . createFileSync ( file ) ; const parameters = "/" + parms . jobFile . jobname + "/" + parms . jobFile . jobid + JobsConstants . RESOURCE_SPOOL_FILES + "/" + parms . jobFile . id + JobsConstants . RESOURCE_SPOOL_CONTENT ; const writeStream = IO . createWriteStream ( file ) ; await ZosmfRestClient . getStreamed ( session , JobsConstants . RESOURCE + parameters , undefined , writeStream , true ) ; } static param { IJobFile } jobFile param { boolean } omitJobidDirectory param { string } outDir returns { string } memberof public static getSpoolDownloadFile ( jobFile , omitJobidDirectory ? , outDir = DownloadJobs . DEFAULT_JOBS_OUTPUT_DIR ) { this . log . trace ( "getSpoolDownloadFile called with jobFile %s, omitJobIDDirectory: %s, outDir: %s" , JSON . stringify ( jobFile ) , omitJobidDirectory + "" , outDir ) ; let directory = outDir ; if ( omitJobidDirectory == null || omitJobidDirectory === false ) { directory += IO . FILE_DELIM + jobFile . jobid ; } if ( jobFile . procstep != null ) { directory += IO . FILE_DELIM + jobFile . procstep ; } if ( jobFile . stepname != null ) { directory += IO . FILE_DELIM + jobFile . stepname ; } return directory + IO . FILE_DELIM + jobFile . ddname + DownloadJobs . DEFAULT_JOBS_OUTPUT_FILE_EXT ; } returns { Logger } private static get log ( ) : Logger { return Logger . getAppLogger ( ) ; } }