import Foundation
+var task: Process?
+
@discardableResult
func run(
_ launchPath: String,
env: [String: String]? = nil,
quiet: Bool = false
) -> Int32 {
- let task = Process()
- task.launchPath = launchPath
- task.arguments = args
+ defer { task = nil }
+ task = Process()
+
+ signal(SIGINT) { _ in
+ task?.terminate() // Send terminate signal to the task
+ exit(0) // Exit the script after cleanup
+ }
+
+ task?.launchPath = launchPath
+ task?.arguments = args
if let env,
- let combinedEnv = task.environment?.merging(env, uniquingKeysWith: { (_, new) in new })
+ let combinedEnv = task?.environment?.merging(env, uniquingKeysWith: { (_, new) in new })
{
- task.environment = combinedEnv
+ task?.environment = combinedEnv
}
if quiet {
- task.standardOutput = nil
- task.standardError = nil
+ task?.standardOutput = nil
+ task?.standardError = nil
}
- task.launch()
- task.waitUntilExit()
- return task.terminationStatus
+ task?.launch()
+ task?.waitUntilExit()
+ return task?.terminationStatus ?? 1
}
func run(