66 lines
2.1 KiB
TypeScript
66 lines
2.1 KiB
TypeScript
import { spawn } from "node:child_process"
|
|
|
|
export type CommandResult = {
|
|
stdout: string
|
|
stderr: string
|
|
code: number
|
|
}
|
|
|
|
export const commandExists = (command: string) =>
|
|
new Promise<boolean>((resolve) => {
|
|
const child = spawn("sh", ["-c", 'command -v "$1" >/dev/null 2>&1', "--", command])
|
|
child.on("error", () => resolve(false))
|
|
child.on("close", (code) => resolve(code === 0))
|
|
})
|
|
|
|
export const runCommand = (
|
|
command: string,
|
|
args: string[],
|
|
options: { timeoutMs?: number; cwd?: string } = {}
|
|
) =>
|
|
new Promise<CommandResult>((resolve, reject) => {
|
|
const child = spawn(command, args, {
|
|
stdio: ["ignore", "pipe", "pipe"],
|
|
cwd: options.cwd,
|
|
})
|
|
|
|
const stdout: Buffer[] = []
|
|
const stderr: Buffer[] = []
|
|
|
|
let timedOut = false
|
|
let killTimeout: ReturnType<typeof setTimeout> | undefined
|
|
const timeout = options.timeoutMs
|
|
? setTimeout(() => {
|
|
timedOut = true
|
|
child.kill("SIGTERM")
|
|
killTimeout = setTimeout(() => child.kill("SIGKILL"), 2000)
|
|
}, options.timeoutMs)
|
|
: null
|
|
|
|
child.stdout.on("data", (chunk) => stdout.push(Buffer.from(chunk)))
|
|
child.stderr.on("data", (chunk) => stderr.push(Buffer.from(chunk)))
|
|
child.on("error", (error) => {
|
|
if (timeout) clearTimeout(timeout)
|
|
if (killTimeout) clearTimeout(killTimeout)
|
|
reject(error)
|
|
})
|
|
child.on("close", (code, signal) => {
|
|
if (timeout) clearTimeout(timeout)
|
|
if (killTimeout) clearTimeout(killTimeout)
|
|
if (timedOut) {
|
|
reject(new Error(`${command} wurde nach ${options.timeoutMs} ms beendet`))
|
|
return
|
|
}
|
|
if (signal) {
|
|
reject(new Error(`${command} wurde durch ${signal} beendet`))
|
|
return
|
|
}
|
|
|
|
resolve({
|
|
stdout: Buffer.concat(stdout).toString("utf8"),
|
|
stderr: Buffer.concat(stderr).toString("utf8"),
|
|
code: code ?? 1,
|
|
})
|
|
})
|
|
})
|