Files
oh-my-opencode/packages/ast-grep-mcp/src/runner.ts
T

59 lines
1.4 KiB
TypeScript
Raw Normal View History

import { existsSync } from "node:fs"
2026-05-18 21:19:15 +09:00
import {
DEFAULT_TIMEOUT_MS,
runSg as runSgCore,
type SgResult,
type SgRunArgs,
type SpawnOptions,
type SpawnResult,
} from "@oh-my-opencode/ast-grep-core"
import { spawn } from "./bun-spawn-shim"
import {
2026-05-18 21:19:15 +09:00
ensureCliAvailable,
getAstGrepPath,
isCliAvailable,
startBackgroundInit,
} from "./cli-binary-path-resolution"
import { getSgCliPath } from "./constants"
import { collectProcessOutputWithTimeout } from "./process-output-timeout"
2026-05-18 21:19:15 +09:00
export { ensureCliAvailable, getAstGrepPath, isCliAvailable, startBackgroundInit }
2026-05-18 21:19:15 +09:00
export type RunOptions = SgRunArgs
2026-05-18 21:19:15 +09:00
export async function runSg(options: RunOptions): Promise<SgResult> {
return runSgCore(options, {
resolveBinary: resolveBinaryPath,
spawnProcess,
2026-05-18 21:19:15 +09:00
})
}
2026-05-18 21:19:15 +09:00
async function resolveBinaryPath(): Promise<string> {
const cliPath = getSgCliPath()
if (cliPath && existsSync(cliPath)) {
return cliPath
2026-05-18 21:19:15 +09:00
}
const resolvedPath = await getAstGrepPath()
if (!resolvedPath) {
const noEntryError = new Error("ENOENT: ast-grep binary not found")
Reflect.set(noEntryError, "code", "ENOENT")
throw noEntryError
}
return resolvedPath
2026-05-18 21:19:15 +09:00
}
async function spawnProcess(
binary: string,
args: readonly string[],
options?: SpawnOptions,
): Promise<SpawnResult> {
const proc = spawn([binary, ...args], {
cwd: options?.cwd,
stdout: options?.stdout ?? "pipe",
stderr: options?.stderr ?? "pipe",
})
2026-05-18 21:19:15 +09:00
return collectProcessOutputWithTimeout(proc, DEFAULT_TIMEOUT_MS)
2026-05-18 21:19:15 +09:00
}