Files
openshell_controller/app/lib/hostCommands.ts
T

135 lines
4.8 KiB
TypeScript

import { existsSync, readdirSync, statSync } from "node:fs"
import path from "node:path"
const HOME = process.env.HOME || ""
function firstExisting(candidates: Array<string | undefined>, fallback: string) {
return candidates.filter((value): value is string => Boolean(value)).find((candidate) => existsSync(candidate)) || fallback
}
function unique(values: Array<string | undefined>) {
return Array.from(new Set(values.filter((value): value is string => Boolean(value))))
}
function safeIsDirectory(candidate: string) {
try {
return statSync(candidate).isDirectory()
} catch {
return false
}
}
function homeChildDirectories() {
if (!HOME || !safeIsDirectory(HOME)) return []
try {
return readdirSync(HOME, { withFileTypes: true })
.filter((entry) => entry.isDirectory())
.map((entry) => path.join(HOME, entry.name))
.filter((directory) => !/(^|\/)(node_modules|\.cache|\.npm|\.local\/share\/Trash)$/.test(directory))
} catch {
return []
}
}
function discoverHomeFiles(relativePath: string) {
const found: string[] = []
for (const directory of homeChildDirectories()) {
const direct = path.join(directory, relativePath)
if (existsSync(direct)) found.push(direct)
try {
for (const entry of readdirSync(directory, { withFileTypes: true })) {
if (!entry.isDirectory()) continue
if (["node_modules", ".git", ".next", "dist", "build"].includes(entry.name)) continue
const nested = path.join(directory, entry.name, relativePath)
if (existsSync(nested)) found.push(nested)
}
} catch {
// Ignore unreadable home subdirectories.
}
}
return found
}
function pathEntries() {
return [
process.env.TERMINAL_EXTRA_PATH,
HOME ? path.join(HOME, ".local/bin") : undefined,
HOME ? path.join(HOME, ".nvm/versions/node/v22.22.2/bin") : undefined,
HOME ? path.join(HOME, ".nvm/versions/node/v22.22.1/bin") : undefined,
HOME ? path.join(HOME, ".nemoclaw/source/node_modules/.bin") : undefined,
HOME ? path.join(HOME, "NemoClaw/node_modules/.bin") : undefined,
"/opt/homebrew/bin",
"/usr/local/bin",
"/usr/bin",
"/bin",
"/usr/sbin",
"/sbin",
process.env.PATH,
].filter((value): value is string => Boolean(value))
}
export const HOST_PATH = unique(pathEntries()).join(":")
export const NODE_BIN = firstExisting([
process.env.NODE_BIN,
HOME ? path.join(HOME, ".nvm/versions/node/v22.22.2/bin/node") : undefined,
HOME ? path.join(HOME, ".nvm/versions/node/v22.22.1/bin/node") : undefined,
"/opt/homebrew/bin/node",
"/usr/local/bin/node",
"/usr/bin/node",
], "node")
export const OPENSHELL_BIN = firstExisting([
process.env.OPENSHELL_BIN,
HOME ? path.join(HOME, ".local/bin/openshell") : undefined,
"/opt/homebrew/bin/openshell",
"/usr/local/bin/openshell",
], "openshell")
export const OPENCLAW_BIN = firstExisting([
process.env.OPENCLAW_BIN,
HOME ? path.join(HOME, ".local/bin/openclaw") : undefined,
HOME ? path.join(HOME, ".nemoclaw/source/node_modules/.bin/openclaw") : undefined,
HOME ? path.join(HOME, "NemoClaw/node_modules/.bin/openclaw") : undefined,
"/opt/homebrew/bin/openclaw",
"/usr/local/bin/openclaw",
], "openclaw")
export const NEMOCLAW_BIN_CANDIDATES = unique([
process.env.NEMOCLAW_BIN,
HOME ? path.join(HOME, ".local/bin/nemoclaw") : undefined,
HOME ? path.join(HOME, ".nemoclaw/source/bin/nemoclaw.js") : undefined,
HOME ? path.join(HOME, "NemoClaw/bin/nemoclaw.js") : undefined,
HOME ? path.join(HOME, "nemoclaw/bin/nemoclaw.js") : undefined,
HOME ? path.join(HOME, "nemoclaw.js") : undefined,
...discoverHomeFiles("bin/nemoclaw.js"),
HOME ? path.join(HOME, ".nvm/versions/node/v22.22.2/bin/nemoclaw") : undefined,
"/opt/homebrew/bin/nemoclaw",
"/usr/local/bin/nemoclaw",
])
export const NEMOCLAW_BIN = firstExisting(NEMOCLAW_BIN_CANDIDATES, "nemoclaw")
export const NEMOCLAW_SETUP_CANDIDATES = unique([
process.env.NEMOCLAW_SETUP,
HOME ? path.join(HOME, ".nemoclaw/source/scripts/setup.sh") : undefined,
HOME ? path.join(HOME, "NemoClaw/scripts/setup.sh") : undefined,
HOME ? path.join(HOME, "nemoclaw/scripts/setup.sh") : undefined,
...discoverHomeFiles("scripts/setup.sh"),
])
export const NEMOCLAW_SETUP = firstExisting(NEMOCLAW_SETUP_CANDIDATES, "")
export const NEMOCLAW_CWD_CANDIDATES = unique([
process.env.NEMOCLAW_CWD,
NEMOCLAW_SETUP ? path.dirname(path.dirname(NEMOCLAW_SETUP)) : undefined,
NEMOCLAW_BIN.includes("/") ? path.dirname(path.dirname(NEMOCLAW_BIN)) : undefined,
HOME ? path.join(HOME, ".nemoclaw/source") : undefined,
HOME ? path.join(HOME, "NemoClaw") : undefined,
HOME ? path.join(HOME, "nemoclaw") : undefined,
])
export const NEMOCLAW_CWD = firstExisting(NEMOCLAW_CWD_CANDIDATES, process.cwd())
export function commandExists(command: string) {
return command.includes("/") ? existsSync(command) : true
}