Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

pathExpansion.ts

42 lines1.5 KB

1// @effect-diagnostics nodeBuiltinImport:off
2import * as NodeOS from "node:os";
3import * as NodePath from "node:path";
4​
5import type * as Path from "effect/Path";
6​
7/**
8 * Expand a leading `~` (or `~/…`, `~\…`) in a user-supplied path to the
9 * current user's home directory. Spawned processes don't get shell
10 * expansion, so env vars like `CODEX_HOME=~/.codex-work` would be passed
11 * verbatim and treated as relative paths by the receiver.
12 *
13 * Matches the behavior of the other `expandHomePath` helpers in the
14 * workspace layers and CLI bootstrap: `~` alone and both `~/` and `~\`
15 * separators are handled. Returns the input unchanged if it doesn't
16 * start with `~` or is empty. Does not handle `~user` (other-user)
17 * expansion.
18 */
19export function expandHomePath(value: string): string {
20 if (!value) return value;
21 if (value === "~") return NodeOS.homedir();
22 if (value.startsWith("~/") || value.startsWith("~\\")) {
23 return NodePath.join(NodeOS.homedir(), value.slice(2));
24 }
25 return value;
27​
28/**
29 * Same expansion as `expandHomePath`, but joins with a caller-supplied
30 * `Path.Path` service instead of `node:path`. Use this inside Effect code that
31 * already has `Path.Path` in context so the platform layer stays in control of
32 * separator handling.
33 */
34export function expandHomePathWith(value: string, path: Path.Path): string {
35 if (value === "~") {
36 return NodeOS.homedir();
37 }
38 if (value.startsWith("~/") || value.startsWith("~\\")) {
39 return path.join(NodeOS.homedir(), value.slice(2));
40 }
41 return value;