import { BACKGROUND_CONTEXT } from "@earendil-works/chord/context"; import type { Environment } from "@berry/api"; /** An interactive shell on the target, preferring PowerShell 7 to Windows PowerShell. */ export async function shell(env: Environment): Promise { if (env.platform !== "win32") return [env.variables.SHELL || "/bin/sh", "-l"]; const variables = Object.fromEntries( Object.entries(env.variables).map(([key, value]) => [key.toUpperCase(), value]), ); const onPath = (name: string) => (variables.PATH ?? "").split(";").filter(Boolean).flatMap((dir) => (variables.PATHEXT ?? ".EXE").split(";").filter(Boolean).map((ext) => [ dir.replace(/^"(.*)"$/, "$1"), name + ext, ]) ); const candidates = [ ...onPath("pwsh"), ...[variables.PROGRAMFILES, variables.PROGRAMW6432].filter(Boolean).map((dir) => [ dir, "PowerShell", "7", "pwsh.exe", ]), ...onPath("powershell"), ...(variables.SYSTEMROOT ? [[variables.SYSTEMROOT, "System32", "WindowsPowerShell", "v1.0", "powershell.exe"]] : []), ]; for (const parts of candidates) { const path = await env.joinPath(parts, BACKGROUND_CONTEXT); if (!path.ok) continue; const info = await env.fileInfo(path.value, BACKGROUND_CONTEXT); if (info.ok && info.value.kind !== "directory") return [path.value, "-NoLogo"]; } throw new Error("PowerShell not found. Install PowerShell 7 or add it to PATH."); }