@@ -61,17 +61,21 @@ if (emptyCall || showHelp) {
61
61
62
62
program . parse ( process . argv ) ;
63
63
64
- preprocess ( ) ;
65
-
66
- if ( program . compile ) {
67
- output ( getCompiled ( ) ) ;
68
- } else if ( program . render ) {
69
- doRender ( ) ;
70
- } else if ( program . interactive ) {
71
- intreactive ( ) ;
72
- } else {
73
- exec ( ) ;
74
- }
64
+ ( async ( ) => {
65
+ preprocess ( ) ;
66
+
67
+ if ( program . compile ) {
68
+ output ( await getCompiled ( ) ) ;
69
+ } else if ( program . render ) {
70
+ doRender ( ) ;
71
+ } else if ( program . interactive ) {
72
+ await intreactive ( ) ;
73
+ } else {
74
+ await exec ( ) ;
75
+ }
76
+ } ) ( ) . catch ( e => {
77
+ console . error ( e ) ;
78
+ } ) ;
75
79
76
80
// ====== Utils ======
77
81
@@ -103,16 +107,27 @@ function preprocess() {
103
107
104
108
function getCompiled ( ) {
105
109
const source = getSource ( ) ;
110
+ const importPaths = getImportPaths ( ) ;
111
+ // console.log(importPaths)
112
+
106
113
return compile ( program . lang , source , {
107
114
romanizeIdentifiers : program . roman ,
108
115
logCallback : logHandler ( program . log , "a" ) ,
109
116
errorCallback : function ( x ) {
110
117
console . error ( x ) ;
111
118
process . exit ( ) ;
112
- }
119
+ } ,
120
+ importPaths
113
121
} ) ;
114
122
}
115
123
124
+ function getImportPaths ( ) {
125
+ const dir = new Set (
126
+ program . files . map ( file => path . resolve ( path . dirname ( file ) ) )
127
+ ) ;
128
+ return [ ...dir , path . resolve ( "." ) ] ;
129
+ }
130
+
116
131
function resolvePath ( x ) {
117
132
return path . resolve ( x ) ;
118
133
}
@@ -125,6 +140,7 @@ function getSource() {
125
140
: fs . readFileSync ( resolvePath ( x ) ) . toString ( )
126
141
)
127
142
. join ( "\n" ) ;
143
+
128
144
if ( program . eval ) scripts += `\n${ program . eval } ` ;
129
145
130
146
return scripts ;
@@ -176,25 +192,25 @@ function doRender() {
176
192
}
177
193
}
178
194
179
- function intreactive ( ) {
195
+ async function intreactive ( ) {
180
196
if ( program . lang !== "js" ) {
181
197
console . error (
182
198
`Target language "${ program . lang } " is not supported for intreactive mode.`
183
199
) ;
184
200
process . exit ( 1 ) ;
185
201
}
186
202
replscope ( ) ;
187
- repl ( getCompiled ( ) ) ;
203
+ repl ( await getCompiled ( ) ) ;
188
204
}
189
- function exec ( ) {
205
+ async function exec ( ) {
190
206
if ( program . lang !== "js" ) {
191
207
console . error (
192
208
`Target language "${ program . lang } " is not supported for direct executing. Please use --compile option instead.`
193
209
) ;
194
210
process . exit ( 1 ) ;
195
211
}
196
212
197
- evalCompiled ( getCompiled ( ) , {
213
+ evalCompiled ( await getCompiled ( ) , {
198
214
outputHanzi : program . outputHanzi ,
199
215
lang : program . lang
200
216
} ) ;
0 commit comments