Script for easily configuring, using, switching and comparing local offline coding models
at main 48 lines 1.2 kB view raw
1import { execSync, execFileSync } from "node:child_process"; 2import { writeFile, mkdir } from "node:fs/promises"; 3import { dirname } from "node:path"; 4 5export function commandExists(name: string): boolean { 6 try { 7 execSync(`command -v ${name}`, { stdio: "ignore" }); 8 return true; 9 } catch { 10 return false; 11 } 12} 13 14export function run(cmd: string, opts?: { silent?: boolean }): string { 15 const stdio = opts?.silent ? "pipe" : "inherit"; 16 const result = execSync(cmd, { 17 stdio: [stdio, "pipe", stdio], 18 encoding: "utf-8", 19 }); 20 return result?.trim() ?? ""; 21} 22 23export function runPassthrough(cmd: string): void { 24 execSync(cmd, { stdio: "inherit" }); 25} 26 27export function runShell(cmd: string): string { 28 return execFileSync("/bin/bash", ["-c", cmd], { 29 encoding: "utf-8", 30 stdio: ["pipe", "pipe", "pipe"], 31 }).trim(); 32} 33 34export async function writeExecutable( 35 path: string, 36 content: string, 37): Promise<void> { 38 await mkdir(dirname(path), { recursive: true }); 39 await writeFile(path, content, { mode: 0o755 }); 40} 41 42export async function writeConfig( 43 path: string, 44 content: string, 45): Promise<void> { 46 await mkdir(dirname(path), { recursive: true }); 47 await writeFile(path, content, { mode: 0o644 }); 48}