From ac42eae845cea79624a409cc4e32e6836d8c8015 Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Thu, 29 Sep 2022 20:30:46 +0000 Subject: [PATCH] Prelude.tryProcess allow cwd for every sys target except Java --- kiss/src/kiss/Prelude.hx | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/kiss/src/kiss/Prelude.hx b/kiss/src/kiss/Prelude.hx index 97429361..e4bcd679 100644 --- a/kiss/src/kiss/Prelude.hx +++ b/kiss/src/kiss/Prelude.hx @@ -657,7 +657,7 @@ class Prelude { return tryProcess(command, args, (error) -> { throw error; }, inputLines, fullProcess); } - public static function tryProcess(command:String, args:Array, handleError:String->Void, ?inputLines:Array, fullProcess = true):String { + public static function tryProcess(command:String, args:Array, handleError:String->Void, ?inputLines:Array, fullProcess = true, cwd:String = null):String { #if test Prelude.print('running $command $args $inputLines from ${Prelude.getTarget()}'); #end @@ -675,6 +675,7 @@ class Prelude { stdin: -1, // -1 represents PIPE which allows communication stdout: -1, stderr: -1, + cwd: cwd }); if (inputLines != null) { for (line in inputLines) { @@ -698,6 +699,13 @@ class Prelude { p.terminate(); return output; #elseif sys + var lastCwd = Sys.getCwd(); + if (cwd != null) { + #if java + throw 'Kiss cannot specify the working directory of a subprocess in Java'; + #end + Sys.setCwd(cwd); + } var p = new Process(command, args); if (inputLines != null) { for (line in inputLines) { @@ -716,12 +724,15 @@ class Prelude { } p.kill(); p.close(); + if (cwd != null) { + Sys.setCwd(lastCwd); + } return output; #elseif hxnodejs var p = if (inputLines != null) { - ChildProcess.spawnSync(command, args, {input: inputLines.join("\n")}); + ChildProcess.spawnSync(command, args, {input: inputLines.join("\n"), cwd: cwd}); } else { - ChildProcess.spawnSync(command, args); + ChildProcess.spawnSync(command, args, {cwd: cwd}); } var output = switch (p.status) { case 0: