- Extract shared command resolution helpers (stripWrappingQuotes, resolvePathEnvironmentVariable, resolveWindowsPathExtensions, resolveCommandCandidates, isExecutableFile) into a new commandResolution.ts module, eliminating duplication between processRunner.ts and open.ts. - Make CodexAppServerManager constructor require services parameter instead of using an unsafe cast that erases Effect service requirements when services are omitted. Co-authored-by: Julius Marminge <[email protected]>
72 lines
2.2 KiB
TypeScript
72 lines
2.2 KiB
TypeScript
import { accessSync, constants, statSync } from "node:fs";
|
|
import { extname } from "node:path";
|
|
|
|
export function stripWrappingQuotes(value: string): string {
|
|
return value.replace(/^"+|"+$/g, "");
|
|
}
|
|
|
|
export function resolvePathEnvironmentVariable(env: NodeJS.ProcessEnv): string {
|
|
return env.PATH ?? env.Path ?? env.path ?? "";
|
|
}
|
|
|
|
export function resolveWindowsPathExtensions(env: NodeJS.ProcessEnv): ReadonlyArray<string> {
|
|
const rawValue = env.PATHEXT;
|
|
const fallback = [".COM", ".EXE", ".BAT", ".CMD"];
|
|
if (!rawValue) return fallback;
|
|
|
|
const parsed = rawValue
|
|
.split(";")
|
|
.map((entry) => entry.trim())
|
|
.filter((entry) => entry.length > 0)
|
|
.map((entry) => (entry.startsWith(".") ? entry.toUpperCase() : `.${entry.toUpperCase()}`));
|
|
return parsed.length > 0 ? Array.from(new Set(parsed)) : fallback;
|
|
}
|
|
|
|
export function resolveCommandCandidates(
|
|
command: string,
|
|
platform: NodeJS.Platform,
|
|
windowsPathExtensions: ReadonlyArray<string>,
|
|
): ReadonlyArray<string> {
|
|
if (platform !== "win32") return [command];
|
|
const extension = extname(command);
|
|
const normalizedExtension = extension.toUpperCase();
|
|
|
|
if (extension.length > 0 && windowsPathExtensions.includes(normalizedExtension)) {
|
|
const commandWithoutExtension = command.slice(0, -extension.length);
|
|
return Array.from(
|
|
new Set([
|
|
command,
|
|
`${commandWithoutExtension}${normalizedExtension}`,
|
|
`${commandWithoutExtension}${normalizedExtension.toLowerCase()}`,
|
|
]),
|
|
);
|
|
}
|
|
|
|
const candidates: string[] = [];
|
|
for (const candidateExtension of windowsPathExtensions) {
|
|
candidates.push(`${command}${candidateExtension}`);
|
|
candidates.push(`${command}${candidateExtension.toLowerCase()}`);
|
|
}
|
|
return Array.from(new Set(candidates));
|
|
}
|
|
|
|
export function isExecutableFile(
|
|
filePath: string,
|
|
platform: NodeJS.Platform,
|
|
windowsPathExtensions: ReadonlyArray<string>,
|
|
): boolean {
|
|
try {
|
|
const stat = statSync(filePath);
|
|
if (!stat.isFile()) return false;
|
|
if (platform === "win32") {
|
|
const extension = extname(filePath);
|
|
if (extension.length === 0) return false;
|
|
return windowsPathExtensions.includes(extension.toUpperCase());
|
|
}
|
|
accessSync(filePath, constants.X_OK);
|
|
return true;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|