The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 2 | import { isWindowsDrivePath } from "@t3tools/shared/path"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as ChildProcess from "effect/process/ChildProcess"; |
| 5 | import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; |
| 6 | |
| 7 | // Git Bash, which runs Claude Code's Bash tool on Windows, prints `C:\x` as `/c/x`. |
| 8 | const GIT_BASH_DRIVE_PATTERN = /^\/([a-zA-Z])(?:\/|$)/; |
| 9 | |
| 10 | // Maps a drive path written for the other side of a Windows host to this host's form. |
| 11 | // WSL mounts Windows drives under a configurable automount root, so only wslpath knows where. |
| 12 | // No WSL env check: servers started by systemd lack WSL_DISTRO_NAME, and off WSL the spawn just fails. |
| 13 | // Untraced: it runs for every chip path, and only drive paths on Linux spawn anything. |
| 14 | export const toHostDrivePath = Effect.fnUntraced(function* (requestedPath: string) { |
| 15 | const platform = yield* HostProcess.Platform; |
| 16 | if (platform === "win32") { |
| 17 | return requestedPath.replace( |
| 18 | GIT_BASH_DRIVE_PATTERN, |
| 19 | (_, drive: string) => `${drive.toUpperCase()}:/`, |
| 20 | ); |
| 21 | } |
| 22 | // spawn throws on a NUL byte instead of failing. |
| 23 | if (!isWindowsDrivePath(requestedPath) || requestedPath.includes("\0")) return requestedPath; |
| 24 | if (platform !== "linux") return requestedPath; |
| 25 | const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; |
| 26 | const output = yield* spawner |
| 27 | .string( |
| 28 | ChildProcess.make("wslpath", ["-u", requestedPath], { |
| 29 | env: yield* HostProcess.Environment, |
| 30 | stdin: "ignore", |
| 31 | stderr: "ignore", |
| 32 | }), |
| 33 | ) |
| 34 | .pipe( |
| 35 | Effect.timeout("1 second"), |
| 36 | Effect.orElseSucceed(() => ""), |
| 37 | ); |
| 38 | const linuxPath = output.endsWith("\n") ? output.slice(0, -1) : output; |
| 39 | return linuxPath.startsWith("/") ? linuxPath : requestedPath; |
| 40 | }); |