import * as core from '@actions/core' ; import FileUtils from "./Utilities/FileUtils" ; import PowerShellToolRunner from "./Utilities/PowerShellToolRunner" ; import ScriptBuilder from './Utilities/ScriptBuilder' ; export default class ScriptRunner { static filePath : string ; inlineScript : string ; errorActionPreference : string ; failOnStandardErr : boolean ; constructor ( inlineScript , errorActionPreference , failOnStandardErr ) { this . inlineScript = inlineScript ; this . errorActionPreference = errorActionPreference ; this . failOnStandardErr = failOnStandardErr ; } async executeFile ( ) { const error = [ ] ; const options = { listeners : { stderr : ( data ) => { if ( error . length < 10 ) { if ( data . length > 1000 ) { error . push ( `${ data . toString ( 'utf8' , 0 , 1000 ) }<truncated>` ) ; } else { error . push ( data . toString ( 'utf8' ) ) ; } } else if ( error . length === 10 ) { error . push ( 'Additional writes to stderr truncated' ) ; } } } } ; const scriptToExecute = new ScriptBuilder ( ) . getInlineScriptFile ( this . inlineScript , this . errorActionPreference ) ; ScriptRunner . filePath = await FileUtils . createScriptFile ( scriptToExecute ) ; core . debug ( `script file to run: ${ ScriptRunner . filePath }` ) ; await PowerShellToolRunner . init ( ) ; const exitCode = await PowerShellToolRunner . executePowerShellScriptBlock ( ScriptRunner . filePath , options ) ; if ( exitCode !== 0 ) { core . setOutput ( `Azure PowerShell exited with code:` , exitCode . toString ( ) ) ; if ( this . failOnStandardErr ) { error . forEach ( ( err ) => { core . error ( err ) ; } ) ; throw new Error ( `Standard error stream contains one or more lines` ) ; } } } }