@@ -9,8 +9,10 @@ import {
9
9
ValidationError ,
10
10
} from "./deps.ts" ;
11
11
import { addProtocol } from "../_utils.ts" ;
12
- import { getMarkdownModule } from "./markdown.ts" ;
13
- import { spawnWorker } from "./worker.ts" ;
12
+ import { importModule } from "./lib/bootstrap.ts" ;
13
+ import { getModuleFromStdin } from "./lib/stream.ts" ;
14
+ import { getMarkdownModule } from "./lib/markdown.ts" ;
15
+ import { spawnWorker } from "./lib/worker.ts" ;
14
16
import { replCommand } from "./repl.ts" ;
15
17
16
18
export function dzx ( ) {
@@ -72,26 +74,29 @@ export function dzx() {
72
74
script ?: string ,
73
75
args : Array < string > = [ ] ,
74
76
) => {
75
- $ . args = args ;
77
+ if ( ! script && Deno . isatty ( Deno . stdin . rid ) ) {
78
+ throw new ValidationError ( `Missing argument(s): script` ) ;
79
+ }
80
+
81
+ let mainModule : string ;
76
82
if ( script ) {
77
83
const scriptExt = path . extname ( script ) ;
78
-
79
- $ . mainModule = [ ".md" , ".markdown" ] . includes ( scriptExt )
84
+ mainModule = [ ".md" , ".markdown" ] . includes ( scriptExt )
80
85
? await getMarkdownModule ( script )
81
86
: addProtocol ( script ) ;
82
-
83
- if ( worker ) {
84
- spawnWorker ( perms ) ;
85
- } else {
86
- await import ( $ . mainModule ) ;
87
- }
88
- } else if ( Deno . isatty ( Deno . stdin . rid ) ) {
89
- throw new ValidationError ( `Missing argument(s): script` ) ;
90
87
} else {
91
- await importFromStdin ( ) ;
88
+ mainModule = await getModuleFromStdin ( ) ;
92
89
}
93
- if ( $ . verbose ) {
94
- console . log ( $ . bold ( "time: %ss" ) , Math . round ( $ . time ) / 1000 ) ;
90
+
91
+ if ( worker ) {
92
+ spawnWorker ( {
93
+ perms,
94
+ mainModule,
95
+ args,
96
+ startTime : $ . startTime ,
97
+ } ) ;
98
+ } else {
99
+ await importModule ( { mainModule, args } ) ;
95
100
}
96
101
} ,
97
102
)
@@ -105,14 +110,4 @@ export function dzx() {
105
110
provider : new DenoLandProvider ( ) ,
106
111
} ) ,
107
112
) ;
108
-
109
- async function importFromStdin ( ) : Promise < void > {
110
- const data = new TextDecoder ( ) . decode ( await streams . readAll ( Deno . stdin ) ) ;
111
- if ( data ) {
112
- $ . mainModule = `data:application/typescript,${ encodeURIComponent ( data ) } ` ;
113
- await import ( $ . mainModule ) ;
114
- } else {
115
- throw new ValidationError ( `Failed to read from stdin.` , { exitCode : 2 } ) ;
116
- }
117
- }
118
113
}
0 commit comments