@@ -38,9 +38,12 @@ public class JavaScriptExecutor {
3838
3939 let arguments : [ String ]
4040
41+ let env : [ ( String , String ) ]
42+
4143 /// Depending on the type this constructor will try to find the requested shell or fail
42- public init ? ( type: ExecutorType = . any, withArguments maybeArguments: [ String ] ? = nil ) {
44+ public init ? ( type: ExecutorType = . any, withArguments maybeArguments: [ String ] ? = nil , withEnv maybeEnv : [ ( String , String ) ] ? = nil ) {
4345 self . arguments = maybeArguments ?? [ ]
46+ self . env = maybeEnv ?? [ ]
4447 let path : String ?
4548
4649 switch type {
@@ -59,23 +62,24 @@ public class JavaScriptExecutor {
5962 self . executablePath = path!
6063 }
6164
62- public init ( withExecutablePath executablePath: String , arguments: [ String ] ) {
65+ public init ( withExecutablePath executablePath: String , arguments: [ String ] , env : [ ( String , String ) ] ) {
6366 self . executablePath = executablePath
6467 self . arguments = arguments
68+ self . env = env
6569 }
6670
6771 /// Executes the JavaScript script using the configured engine and returns the stdout.
6872 public func executeScript( _ script: String , withTimeout timeout: TimeInterval ? = nil ) throws -> Result {
69- return try execute ( executablePath, withInput: prefix + script. data ( using: . utf8) !, withArguments: self . arguments, timeout: timeout)
73+ return try execute ( executablePath, withInput: prefix + script. data ( using: . utf8) !, withArguments: self . arguments, withEnv : self . env , timeout: timeout)
7074 }
7175
7276 /// Executes the JavaScript script at the specified path using the configured engine and returns the stdout.
7377 public func executeScript( at url: URL , withTimeout timeout: TimeInterval ? = nil ) throws -> Result {
7478 let script = try Data ( contentsOf: url)
75- return try execute ( executablePath, withInput: prefix + script, withArguments: self . arguments, timeout: timeout)
79+ return try execute ( executablePath, withInput: prefix + script, withArguments: self . arguments, withEnv : self . env , timeout: timeout)
7680 }
7781
78- func execute( _ path: String , withInput input: Data = Data ( ) , withArguments arguments: [ String ] = [ ] , timeout maybeTimeout: TimeInterval ? = nil ) throws -> Result {
82+ func execute( _ path: String , withInput input: Data = Data ( ) , withArguments arguments: [ String ] = [ ] , withEnv env : [ ( String , String ) ] = [ ] , timeout maybeTimeout: TimeInterval ? = nil ) throws -> Result {
7983 let inputPipe = Pipe ( )
8084 let outputPipe = Pipe ( )
8185 let errorPipe = Pipe ( )
@@ -89,13 +93,16 @@ public class JavaScriptExecutor {
8993 // Close stdin
9094 try inputPipe. fileHandleForWriting. close ( )
9195
96+ let environment = ProcessInfo . processInfo. environment. merging ( env, uniquingKeysWith: { _, new in new } )
97+
9298 // Execute the subprocess.
9399 let task = Process ( )
94100 task. standardOutput = outputPipe
95101 task. standardError = errorPipe
96102 task. arguments = arguments + [ url. path]
97103 task. executableURL = URL ( fileURLWithPath: path)
98104 task. standardInput = inputPipe
105+ task. environment = environment
99106 try task. run ( )
100107
101108 var timedOut = false
0 commit comments