Skip to content

Commit 8177dd4

Browse files
committed
feat: add throw errors
1 parent 02d5f2e commit 8177dd4

File tree

4 files changed

+31
-23
lines changed

4 files changed

+31
-23
lines changed

dzx.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/// <reference path="./types.d.ts" />
22

33
import { join, readAll } from "./deps.ts";
4-
import { ProcessError } from "./src/process_error.ts";
54
import { $, cd, quote } from "./mod.ts";
5+
import { error } from "./src/_utils.ts";
66

77
window.$ = $;
88
window.cd = cd;
@@ -19,9 +19,10 @@ try {
1919
`data:application/typescript,${encodeURIComponent(data)}`
2020
);
2121
} else {
22-
console.error(`usage: dzx <script>`);
23-
Deno.exit(2);
22+
error(`usage: dzx <script>`, 2);
2423
}
24+
} else {
25+
error(`usage: dzx <script>`);
2526
}
2627
} else if (
2728
script.startsWith("http://") || script.startsWith("https://") ||
@@ -31,12 +32,8 @@ try {
3132
} else if (script) {
3233
await import("file://" + join($.cwd, script));
3334
} else {
34-
console.error(`usage: dzx <script>`);
35-
Deno.exit(1);
35+
error(`usage: dzx <script>`);
3636
}
37-
} catch (error) {
38-
if (error instanceof ProcessError) {
39-
console.error(error);
40-
}
41-
throw error;
37+
} catch (err) {
38+
error(err);
4239
}

mod.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ export type $ = typeof exec & typeof colors & {
88
cwd: string;
99
shell: string;
1010
quote: typeof escapeStr;
11+
throwErors: boolean;
1112
};
1213

1314
export const $: $ = exec as $;
1415

15-
export { quote };
16-
1716
Object.setPrototypeOf($, Object.getPrototypeOf(colors));
1817

1918
$._stack = [];
2019
$.shell = "/bin/sh";
2120
$.verbose = false;
2221
$.cwd = Deno.cwd();
2322
$.quote = escapeStr;
23+
$.throwErors = false;
2424

25-
export { cd };
25+
export { cd, quote };

src/_utils.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export function error(message: string | Error, exitCode = 1) {
2+
if ($.throwErors) {
3+
throw (message instanceof Error
4+
? message
5+
: new Error(getErrorMessage(message)));
6+
}
7+
console.error(message instanceof Error ? message : getErrorMessage(message));
8+
Deno.exit(exitCode);
9+
}
10+
11+
function getErrorMessage(message: string) {
12+
return $.red(`${$.bold("error:")} ${message}`);
13+
}

src/cd.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
1+
import { error } from "./_utils.ts";
2+
13
export function cd(path: string) {
24
if ($.verbose) {
35
console.log($.brightBlue("$ %s"), `cd ${path}`);
46
}
57

68
try {
79
Deno.lstatSync(path);
8-
} catch (error) {
9-
if (error instanceof Deno.errors.NotFound) {
10+
} catch (err) {
11+
if (err instanceof Deno.errors.NotFound) {
1012
const stack: string = (new Error().stack!.split("at ")[2]).trim();
11-
console.error(`cd: ${path}: No such directory`);
12-
console.error(` at ${stack}`);
13-
Deno.exit(1);
14-
} else if (error instanceof Deno.errors.PermissionDenied) {
13+
error(`cd: ${path}: No such directory\n at ${stack}`);
14+
} else if (err instanceof Deno.errors.PermissionDenied) {
1515
const stack: string = (new Error().stack!.split("at ")[2]).trim();
16-
console.error(`cd: ${path}: Permission denied`);
17-
console.error(` at ${stack}`);
18-
Deno.exit(1);
16+
error(`cd: ${path}: Permission denied\n at ${stack}`);
1917
}
20-
throw error;
18+
error(err);
2119
}
2220

2321
$.cwd = path;

0 commit comments

Comments
 (0)