Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
2.3 kB · 87 lines
TypeScript
at commit 9737bd12
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788import { accessSync, constants, existsSync } from 'fs';import { delimiter, join } from 'path';import { execFileSync, spawn } from 'child_process';
export interface CommandResult { ok: boolean; stdout: string; stderr: string;}
// Cross-platform PATH probe. On Windows also walks PATHEXT so `docker` matches docker.exe;// on POSIX checks the X_OK bit so non-executable files in PATH don't false-positive.export function commandExists(command: string): boolean { const pathValue = process.env.Path || process.env.PATH || ''; if (!pathValue) return false;
const hasPathSeparator = /[\\/]/.test(command); const pathEntries = hasPathSeparator ? [''] : pathValue.split(delimiter).filter(Boolean);
if (process.platform === 'win32') { const pathExt = (process.env.PATHEXT || '.EXE;.CMD;.BAT;.COM') .split(';') .filter(Boolean); const hasKnownExt = /\.[^\\/]+$/i.test(command); const candidates = hasKnownExt ? [command] : [command, ...pathExt.map(ext => `${command}${ext.toLowerCase()}`)];
return pathEntries.some(entry => candidates.some(candidate => existsSync(entry ? join(entry, candidate) : candidate))); }
return pathEntries.some(entry => { const candidate = entry ? join(entry, command) : command; try { accessSync(candidate, constants.X_OK); return true; } catch { return false; } });}
export function readCommand(command: string, args: string[], cwd?: string): string { return execFileSync(command, args, { cwd, encoding: 'utf-8', stdio: ['ignore', 'pipe', 'pipe'], }).trim();}
export function tryReadCommand(command: string, args: string[], cwd?: string): string | null { try { return readCommand(command, args, cwd); } catch { return null; }}
export function runInteractive( command: string, args: string[], opts?: { cwd?: string; env?: NodeJS.ProcessEnv; },): void { execFileSync(command, args, { cwd: opts?.cwd, env: opts?.env, stdio: 'inherit', });}
export function spawnInherited( command: string, args: string[], opts?: { cwd?: string; env?: NodeJS.ProcessEnv; },): void { const child = spawn(command, args, { cwd: opts?.cwd, env: opts?.env, stdio: 'inherit', });
child.on('error', () => {});}