Skip to content

Commit a534ea9

Browse files
committed
feat: add support for home directory "~" in cd
1 parent c8d8f72 commit a534ea9

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/runtime/cd.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ export function cd(dir: string) {
88
console.log($.brightBlue("$ %s"), `cd ${dir}`);
99
}
1010
try {
11-
if (dir[0] !== path.sep) {
11+
if (dir[0] === "~") {
12+
dir = path.join(homedir() as string, dir.slice(1));
13+
} else if (dir[0] !== path.sep) {
1214
dir = new URL(dir, path.toFileUrl(cwd + path.sep)).pathname;
1315
}
1416
Deno.chdir(dir);
@@ -23,3 +25,15 @@ export function cd(dir: string) {
2325
error(err);
2426
}
2527
}
28+
29+
function homedir(): string | null {
30+
switch (Deno.build.os) {
31+
case "windows":
32+
return Deno.env.get("USERPROFILE") || null;
33+
case "linux":
34+
case "darwin":
35+
return Deno.env.get("HOME") || null;
36+
default:
37+
throw Error("Failed to retrive home directory.");
38+
}
39+
}

0 commit comments

Comments
 (0)