Resolves a command name to an absolute executable path by searching PATH and checking file executability, cross-platform (handles Windows PATHEXT). Reuse for CLI tools needing to locate binaries before spawning.
// Source: https://github.com/Waishnav/devspace | License: MIT | Copyright 2026 Waishnav
// Provided via justclaw.it.com; keep this notice with the code.
import { accessSync, constants, statSync } from "node:fs";
import { delimiter, resolve } from "node:path";
export function resolveExecutableCommand(
command: string,
env: NodeJS.ProcessEnv,
): string | undefined {
if (!command) return undefined;
if (command.includes("/") || command.includes("\\")) {
return isExecutableFile(command) ? command : undefined;
}
const path = env.PATH;
if (!path) return undefined;
const extensions = process.platform === "win32"
? ["", ...(env.PATHEXT ?? ".COM;.EXE;.BAT;.CMD").split(";").filter(Boolean)]
: [""];
for (const directory of path.split(delimiter)) {
if (!directory) co
... (truncated -- full source via MCP)
See the full source, get the GitHub permalink, and search for more like it.
Get a free API key