Open History to see the last commit
1import { homedir } from "node:os";
2import { join } from "node:path";
3
4/**
5 * Expand a leading `~` (or `~/…`, `~\…`) in a user-supplied path to the
6 * current user's home directory. Spawned processes don't get shell
7 * expansion, so env vars like `CODEX_HOME=~/.codex-work` would be passed
8 * verbatim and treated as relative paths by the receiver.
9 *
10 * Matches the behavior of the other `expandHomePath` helpers in the
11 * workspace layers and CLI bootstrap: `~` alone and both `~/` and `~\`
12 * separators are handled. Returns the input unchanged if it doesn't
13 * start with `~` or is empty. Does not handle `~user` (other-user)
14 * expansion.
15 */
16export function expandHomePath(value: string): string {
17 if (!value) return value;
18 if (value === "~") return homedir();
19 if (value.startsWith("~/") || value.startsWith("~\\")) {
20 return join(homedir(), value.slice(2));
21 }
22 return value;
23}