Skip to content

Commit

Permalink
Terminate sub processes when running vercel dev
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewBarba committed Feb 15, 2023
1 parent fc0fb59 commit b0432fa
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio.git",
"state" : {
"revision" : "4ad2c3733845abd9ee8892a323b0fa0d80f37e34",
"version" : "2.47.0"
"revision" : "45167b8006448c79dda4b7bd604e07a034c15c49",
"version" : "2.48.0"
}
}
],
Expand Down
1 change: 1 addition & 0 deletions Plugins/VercelPackager/Plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import PackagePlugin
@main
struct VercelPackager: CommandPlugin {
func performCommand(context: PackagePlugin.PluginContext, arguments: [String]) async throws {
Shell.prepare()
let vercelOutput = VercelOutput(packageManager: packageManager, context: context, arguments: arguments)
if vercelOutput.isDev {
try await dev(vercelOutput)
Expand Down
6 changes: 0 additions & 6 deletions Plugins/VercelPackager/Server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,3 @@ server.listen(port, () => {
console.log('Http Server running:', `http://localhost:${port}`)
console.log('')
})

server.on('error', () => {
console.log('')
console.log('Http Server running:', `http://localhost:${port}`)
console.log('')
})
20 changes: 20 additions & 0 deletions Plugins/VercelPackager/Shell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,24 @@ import PackagePlugin
import Glibc
#endif

private let globalSource = DispatchSource.makeSignalSource(signal: SIGINT)

private var spawnQueue = DispatchQueue(label: "vercel.spawn")

private var spawnedProcesses: Set<Process> = []

public struct Shell {

static func prepare() {
signal(SIGINT, SIG_IGN)
globalSource.setEventHandler {
print("")
spawnedProcesses.filter { $0.isRunning }.forEach { $0.terminate() }
exit(SIGINT)
}
globalSource.resume()
}

@discardableResult
public static func execute(
process: Process = .init(),
Expand All @@ -27,6 +43,10 @@ public struct Shell {
print("\(executable.string) \(arguments.joined(separator: " "))")
print("")

_ = spawnQueue.sync {
spawnedProcesses.insert(process)
}

var output = ""
let outputSync = DispatchGroup()
let outputQueue = DispatchQueue(label: "VercelPackager.output")
Expand Down

0 comments on commit b0432fa

Please sign in to comment.