@@ -41,53 +41,6 @@ function trimChar(text: string, charToRemove: string): string {
41
41
return text ;
42
42
}
43
43
44
- function registerFileLoader ( interpreter : Interpreter ) : Interpreter {
45
- function getScriptFunctionsListWithArgs ( scripts : string ) : any {
46
- if ( scripts ) {
47
- return scripts . split ( '\n' ) . map ( line => {
48
- const match = line . match ( / ( d e f \s ) ( .* ) ( \( .* \) ) : / ) ;
49
- if ( match ) {
50
- const args = ( match [ 3 ] as string ) . replace ( / [ \( \) \s + ] / g, '' ) . split ( ',' ) as string [ ] ;
51
- return [ match [ 2 ] . replace ( / / g, '' ) as string , args ] as [ string , string [ ] ] ;
52
- }
53
- } ) . filter ( func => Boolean ( func ) ) ;
54
- }
55
- }
56
-
57
- const loader = async ( filePath : string ) => {
58
- const module = filePath ;
59
- try {
60
-
61
- const script = fs . readFileSync ( `${ options . srcRoot } ${ trimChar ( filePath , '/' ) } .jspy` , 'utf8' ) ;
62
- const funcs = getScriptFunctionsListWithArgs ( script ) ;
63
- const result = funcs
64
- . reduce ( ( prev : any , [ func , paramKeys ] : any ) => {
65
- prev [ func ] = async ( ...args : any ) => {
66
- const params = ( paramKeys || [ ] ) . reduce ( ( prev2 : any , key : any , i : any ) => {
67
- prev2 [ key ] = args [ i ] || null ;
68
- return prev2 ;
69
- } , { } ) ;
70
- const context = { } ; // this.getAppContext();
71
- try {
72
- return await interpreter . evaluate ( script , { ...context , ...params } , func , module ) ;
73
- } catch ( e ) {
74
- throw Error ( `${ e . message } ` ) ;
75
- }
76
- } ;
77
- return prev ;
78
- } , { }
79
- ) ;
80
-
81
- return result ;
82
- } catch ( e ) {
83
- console . error ( e ) ;
84
- }
85
-
86
- } ;
87
- interpreter . registerFileLoader ( loader ) ;
88
- return interpreter ;
89
- }
90
-
91
44
async function initialize ( baseSource : string ) {
92
45
// process app.json (if exists)
93
46
// add configuration to the 'app'
@@ -171,6 +124,21 @@ function getOptionsFromArguments(rawArgs: string[]) {
171
124
return res ;
172
125
}
173
126
127
+ function moduleLoader ( filePath : string ) : Promise < string > {
128
+ try {
129
+ const script = fs . readFileSync ( `${ options . srcRoot } ${ trimChar ( filePath , '/' ) } .jspy` , 'utf8' ) ;
130
+ return Promise . resolve ( script ) ;
131
+ } catch ( e ) {
132
+ try {
133
+ // try without JSPY
134
+ const script = fs . readFileSync ( `${ options . srcRoot } ${ trimChar ( filePath , '/' ) } ` , 'utf8' ) ;
135
+ return Promise . resolve ( script ) ;
136
+ } catch ( e ) {
137
+ return Promise . reject ( e ) ;
138
+ }
139
+ }
140
+ }
141
+
174
142
/**@type {PackageLoader } */
175
143
function packageLoader ( packageName : string ) : any {
176
144
try {
@@ -213,7 +181,8 @@ async function main() {
213
181
214
182
if ( options . file ) {
215
183
interpreter . registerPackagesLoader ( packageLoader as PackageLoader ) ;
216
- registerFileLoader ( interpreter )
184
+ interpreter . registerModuleLoader ( moduleLoader ) ;
185
+
217
186
const scripts = fs . readFileSync ( `${ options . srcRoot } ${ options . file } ` , 'utf8' ) ;
218
187
context . asserts . length = 0 ;
219
188
console . log ( interpreter . jsPythonInfo ( ) )
0 commit comments