Skip to content

Commit

Permalink
feat: add $.cd
Browse files Browse the repository at this point in the history
  • Loading branch information
c4spar committed May 8, 2021
1 parent 8e994b9 commit 9dafdfd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
26 changes: 26 additions & 0 deletions dzx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ $._stack = [];
$.shell = "/bin/sh";
$.verbose = false;
$.cwd = Deno.cwd();
$.cd = cd;

window.$ = $;

Expand Down Expand Up @@ -105,3 +106,28 @@ async function read(
}
}
}

export function cd(path: string) {
if ($.verbose) {
console.log($.brightBlue("$ %s"), `cd ${path}`);
}

try {
Deno.lstatSync(path);
} catch (error) {
if (error instanceof Deno.errors.NotFound) {
const stack: string = (new Error().stack!.split("at ")[2]).trim();
console.error(`cd: ${path}: No such directory`);
console.error(` at ${stack}`);
Deno.exit(1);
} else if (error instanceof Deno.errors.PermissionDenied) {
const stack: string = (new Error().stack!.split("at ")[2]).trim();
console.error(`cd: ${path}: Permission denied`);
console.error(` at ${stack}`);
Deno.exit(1);
}
throw error;
}

$.cwd = path;
}
1 change: 1 addition & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ type DZX = typeof exec & typeof colors & {
verbose: boolean;
cwd: string;
shell: string;
cd: (path: string) => void;
};

declare global {
Expand Down

0 comments on commit 9dafdfd

Please sign in to comment.