Last active
August 30, 2020 11:04
-
-
Save laevandus/41b68c82b24a35bd71e87fbb5ef87485 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Process { | |
@discardableResult | |
static func runZshCommand(_ command: String) -> Int32 { | |
let process = Process() | |
process.launchPath = "/bin/zsh" | |
process.arguments = ["-c", command] | |
process.standardOutput = { | |
let pipe = Pipe() | |
pipe.fileHandleForReading.readabilityHandler = { handler in | |
guard let string = String(data: handler.availableData, encoding: .utf8), !string.isEmpty else { return } | |
print(string) | |
} | |
return pipe | |
}() | |
process.standardError = { | |
let pipe = Pipe() | |
pipe.fileHandleForReading.readabilityHandler = { handler in | |
guard let string = String(data: handler.availableData, encoding: .utf8), !string.isEmpty else { return } | |
print(string) | |
} | |
return pipe | |
}() | |
process.launch() | |
process.waitUntilExit() | |
(process.standardOutput as! Pipe).fileHandleForReading.readabilityHandler = nil | |
(process.standardError as! Pipe).fileHandleForReading.readabilityHandler = nil | |
return process.terminationStatus | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment