File tree Expand file tree Collapse file tree 4 files changed +31
-23
lines changed Expand file tree Collapse file tree 4 files changed +31
-23
lines changed Original file line number Diff line number Diff line change 1
1
/// <reference path="./types.d.ts" />
2
2
3
3
import { join , readAll } from "./deps.ts" ;
4
- import { ProcessError } from "./src/process_error.ts" ;
5
4
import { $ , cd , quote } from "./mod.ts" ;
5
+ import { error } from "./src/_utils.ts" ;
6
6
7
7
window . $ = $ ;
8
8
window . cd = cd ;
19
19
`data:application/typescript,${ encodeURIComponent ( data ) } `
20
20
) ;
21
21
} else {
22
- console . error ( `usage: dzx <script>` ) ;
23
- Deno . exit ( 2 ) ;
22
+ error ( `usage: dzx <script>` , 2 ) ;
24
23
}
24
+ } else {
25
+ error ( `usage: dzx <script>` ) ;
25
26
}
26
27
} else if (
27
28
script . startsWith ( "http://" ) || script . startsWith ( "https://" ) ||
31
32
} else if ( script ) {
32
33
await import ( "file://" + join ( $ . cwd , script ) ) ;
33
34
} else {
34
- console . error ( `usage: dzx <script>` ) ;
35
- Deno . exit ( 1 ) ;
35
+ error ( `usage: dzx <script>` ) ;
36
36
}
37
- } catch ( error ) {
38
- if ( error instanceof ProcessError ) {
39
- console . error ( error ) ;
40
- }
41
- throw error ;
37
+ } catch ( err ) {
38
+ error ( err ) ;
42
39
}
Original file line number Diff line number Diff line change @@ -8,18 +8,18 @@ export type $ = typeof exec & typeof colors & {
8
8
cwd : string ;
9
9
shell : string ;
10
10
quote : typeof escapeStr ;
11
+ throwErors : boolean ;
11
12
} ;
12
13
13
14
export const $ : $ = exec as $ ;
14
15
15
- export { quote } ;
16
-
17
16
Object . setPrototypeOf ( $ , Object . getPrototypeOf ( colors ) ) ;
18
17
19
18
$ . _stack = [ ] ;
20
19
$ . shell = "/bin/sh" ;
21
20
$ . verbose = false ;
22
21
$ . cwd = Deno . cwd ( ) ;
23
22
$ . quote = escapeStr ;
23
+ $ . throwErors = false ;
24
24
25
- export { cd } ;
25
+ export { cd , quote } ;
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
1
+ import { error } from "./_utils.ts" ;
2
+
1
3
export function cd ( path : string ) {
2
4
if ( $ . verbose ) {
3
5
console . log ( $ . brightBlue ( "$ %s" ) , `cd ${ path } ` ) ;
4
6
}
5
7
6
8
try {
7
9
Deno . lstatSync ( path ) ;
8
- } catch ( error ) {
9
- if ( error instanceof Deno . errors . NotFound ) {
10
+ } catch ( err ) {
11
+ if ( err instanceof Deno . errors . NotFound ) {
10
12
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 ) {
15
15
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 } ` ) ;
19
17
}
20
- throw error ;
18
+ error ( err ) ;
21
19
}
22
20
23
21
$ . cwd = path ;
You can’t perform that action at this time.
0 commit comments