import { execSync, execFileSync } from "node:child_process"; import { writeFile, mkdir } from "node:fs/promises"; import { dirname } from "node:path"; export function commandExists(name: string): boolean { try { execSync(`command -v ${name}`, { stdio: "ignore" }); return true; } catch { return false; } } export function run(cmd: string, opts?: { silent?: boolean }): string { const stdio = opts?.silent ? "pipe" : "inherit"; const result = execSync(cmd, { stdio: [stdio, "pipe", stdio], encoding: "utf-8", }); return result?.trim() ?? ""; } export function runPassthrough(cmd: string): void { execSync(cmd, { stdio: "inherit" }); } export function runShell(cmd: string): string { return execFileSync("/bin/bash", ["-c", cmd], { encoding: "utf-8", stdio: ["pipe", "pipe", "pipe"], }).trim(); } export async function writeExecutable( path: string, content: string, ): Promise { await mkdir(dirname(path), { recursive: true }); await writeFile(path, content, { mode: 0o755 }); } export async function writeConfig( path: string, content: string, ): Promise { await mkdir(dirname(path), { recursive: true }); await writeFile(path, content, { mode: 0o644 }); }