mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-18 13:30:48 +00:00
37 lines
1014 B
TypeScript
37 lines
1014 B
TypeScript
import fs from "node:fs/promises";
|
|
import path from "node:path";
|
|
import { isSafeExecutableValue } from "../infra/exec-safety.js";
|
|
import { runCommandWithTimeout } from "../process/exec.js";
|
|
import { resolveUserPath } from "../utils.js";
|
|
|
|
export async function detectBinary(name: string): Promise<boolean> {
|
|
if (!name?.trim()) {
|
|
return false;
|
|
}
|
|
if (!isSafeExecutableValue(name)) {
|
|
return false;
|
|
}
|
|
const resolved = name.startsWith("~") ? resolveUserPath(name) : name;
|
|
if (
|
|
path.isAbsolute(resolved) ||
|
|
resolved.startsWith(".") ||
|
|
resolved.includes("/") ||
|
|
resolved.includes("\\")
|
|
) {
|
|
try {
|
|
await fs.access(resolved);
|
|
return true;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
const command = process.platform === "win32" ? ["where", name] : ["/usr/bin/env", "which", name];
|
|
try {
|
|
const result = await runCommandWithTimeout(command, { timeoutMs: 2000 });
|
|
return result.code === 0 && result.stdout.trim().length > 0;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|