Skip to content

Commit

Permalink
Force shell commands onto a background thread; fixes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
NanoSector committed Mar 13, 2021
1 parent e0f244b commit b7b1ea0
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions Brewed/Helpers/Shell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,36 @@ import PromiseKit

struct Shell {
typealias processResult = (exitCode: Int32, stdout: String, stderr: String)

static func exec(_ command: String) -> Promise<processResult> {
Promise { seal in
let task = Process()
let stdout = Pipe()
let stderr = Pipe()

task.standardOutput = stdout
task.standardError = stderr
task.arguments = ["-c", command]
task.launchPath = "/bin/zsh"
task.launch()
task.waitUntilExit()

guard task.terminationStatus == 0 else {
throw ProcessError.NonZeroExitCode(stderr: stderr.toString())

DispatchQueue.global(qos: .background).async {
task.launch()
task.waitUntilExit()

guard task.terminationStatus == 0 else {
seal.reject(ProcessError.NonZeroExitCode(stderr: stderr.toString()))
return
}

seal.fulfill((task.terminationStatus, stdout.toString(), stderr.toString()))
}

seal.fulfill((task.terminationStatus, stdout.toString(), stderr.toString()))
}
}
}

protocol ShellCommandWrapper {
associatedtype resultType

func exec() -> Promise<resultType>
}

Expand Down

0 comments on commit b7b1ea0

Please sign in to comment.