Skip to content

Commit

Permalink
feat: add throw errors
Browse files Browse the repository at this point in the history
  • Loading branch information
c4spar committed May 9, 2021
1 parent 02d5f2e commit 8177dd4
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 23 deletions.
17 changes: 7 additions & 10 deletions dzx.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/// <reference path="./types.d.ts" />

import { join, readAll } from "./deps.ts";
import { ProcessError } from "./src/process_error.ts";
import { $, cd, quote } from "./mod.ts";
import { error } from "./src/_utils.ts";

window.$ = $;
window.cd = cd;
Expand All @@ -19,9 +19,10 @@ try {
`data:application/typescript,${encodeURIComponent(data)}`
);
} else {
console.error(`usage: dzx <script>`);
Deno.exit(2);
error(`usage: dzx <script>`, 2);
}
} else {
error(`usage: dzx <script>`);
}
} else if (
script.startsWith("http://") || script.startsWith("https://") ||
Expand All @@ -31,12 +32,8 @@ try {
} else if (script) {
await import("file://" + join($.cwd, script));
} else {
console.error(`usage: dzx <script>`);
Deno.exit(1);
error(`usage: dzx <script>`);
}
} catch (error) {
if (error instanceof ProcessError) {
console.error(error);
}
throw error;
} catch (err) {
error(err);
}
6 changes: 3 additions & 3 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ export type $ = typeof exec & typeof colors & {
cwd: string;
shell: string;
quote: typeof escapeStr;
throwErors: boolean;
};

export const $: $ = exec as $;

export { quote };

Object.setPrototypeOf($, Object.getPrototypeOf(colors));

$._stack = [];
$.shell = "/bin/sh";
$.verbose = false;
$.cwd = Deno.cwd();
$.quote = escapeStr;
$.throwErors = false;

export { cd };
export { cd, quote };
13 changes: 13 additions & 0 deletions src/_utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export function error(message: string | Error, exitCode = 1) {
if ($.throwErors) {
throw (message instanceof Error
? message
: new Error(getErrorMessage(message)));
}
console.error(message instanceof Error ? message : getErrorMessage(message));
Deno.exit(exitCode);
}

function getErrorMessage(message: string) {
return $.red(`${$.bold("error:")} ${message}`);
}
18 changes: 8 additions & 10 deletions src/cd.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import { error } from "./_utils.ts";

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) {
} catch (err) {
if (err 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) {
error(`cd: ${path}: No such directory\n at ${stack}`);
} else if (err 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);
error(`cd: ${path}: Permission denied\n at ${stack}`);
}
throw error;
error(err);
}

$.cwd = path;
Expand Down

0 comments on commit 8177dd4

Please sign in to comment.