@@ -43,21 +43,22 @@ function trimChar(text: string, charToRemove: string): string {
43
43
}
44
44
45
45
async function initialize ( baseSource : string ) {
46
- // process app.json (if exists)
47
- // add configuration to the 'app'
48
- if ( fs . existsSync ( `${ rootFolder } /${ baseSource } app.json` ) ) {
49
- const app = require ( `${ rootFolder } /${ baseSource } app.json` ) ;
50
- initialScope . app = Object . assign ( initialScope . app || { } , app ) ;
51
- }
52
46
53
47
// process app.js (if exists)
54
48
// - run _init
55
49
// - delete _ init
56
50
// - run _initAsync
57
51
// - delete _initAsync
58
52
// - load content into 'app'
59
- if ( fs . existsSync ( `${ rootFolder } /${ baseSource } app.js` ) ) {
60
- const app = require ( `${ rootFolder } /${ baseSource } app.js` ) ;
53
+
54
+ let appJsPath = `${ rootFolder } /${ baseSource } app.js` ;
55
+ console . log ( { rootFolder, baseSource } )
56
+ if ( ! fs . existsSync ( appJsPath ) ) {
57
+ appJsPath = `${ rootFolder } /src/app.js`
58
+ }
59
+
60
+ if ( fs . existsSync ( appJsPath ) ) {
61
+ const app = require ( appJsPath ) ;
61
62
62
63
if ( typeof app . _init == 'function' ) {
63
64
app . _init ( ) ;
@@ -126,18 +127,23 @@ function getOptionsFromArguments(rawArgs: string[]) {
126
127
}
127
128
128
129
function moduleLoader ( filePath : string ) : Promise < string > {
130
+ filePath = trimChar ( trimChar ( filePath , '/' ) , '.' ) ;
131
+ let fileName = `${ options . srcRoot } ${ filePath } .jspy` ;
132
+
133
+ if ( ! fs . existsSync ( fileName ) ) {
134
+ fileName = `${ options . srcRoot } ${ filePath } ` ;
135
+ }
136
+
137
+ if ( ! fs . existsSync ( fileName ) ) {
138
+ fileName = `src/${ filePath } ` ;
139
+ }
140
+
129
141
try {
130
- const script = fs . readFileSync ( ` ${ options . srcRoot } ${ trimChar ( filePath , '/' ) } .jspy` , 'utf8' ) ;
142
+ const script = fs . readFileSync ( fileName , 'utf8' ) ;
131
143
return Promise . resolve ( script ) ;
132
144
} catch ( e ) {
133
- try {
134
- // try without JSPY
135
- const script = fs . readFileSync ( `${ options . srcRoot } ${ trimChar ( filePath , '/' ) } ` , 'utf8' ) ;
136
- return Promise . resolve ( script ) ;
137
- } catch ( e ) {
138
- console . log ( '* module loader error ' , e ?. message || e )
139
- return Promise . reject ( e ) ;
140
- }
145
+ console . log ( '* module loader error ' , e ?. message || e )
146
+ return Promise . reject ( e ) ;
141
147
}
142
148
}
143
149
@@ -162,6 +168,17 @@ function packageLoader(packageName: string): any {
162
168
}
163
169
164
170
async function main ( ) {
171
+ if ( ! options . file && ! options . version ) {
172
+ console . log ( interpreter . jsPythonInfo ( ) ) ;
173
+ console . log ( `JSPython cli v${ ( pkg || { } ) . version } \n` ) ;
174
+ console . log ( ` :\> jspython (fileName.jspy)` ) ;
175
+ console . log ( ` :\> jspython -f=(fileName.jspy)` ) ;
176
+ console . log ( ` :\> jspython --file=(fileName.jspy)` ) ;
177
+ console . log ( ` :\> jspython --file=(fileName.jspy) --srcRoot=src` ) ;
178
+ console . log ( ' ' ) ;
179
+ return ;
180
+ }
181
+
165
182
if ( options . version ) {
166
183
console . log ( interpreter . jsPythonInfo ( ) ) ;
167
184
console . log ( `JSPython cli v${ ( pkg || { } ) . version } \n` ) ;
@@ -182,15 +199,22 @@ async function main() {
182
199
await initialize ( options . srcRoot ) ;
183
200
184
201
if ( options . file ) {
185
- const scripts = fs . readFileSync ( `${ options . srcRoot } ${ options . file } ` , 'utf8' ) ;
202
+ let fileName = `${ options . srcRoot } ${ options . file } ` ;
203
+
204
+ // try to check if file exists in 'src' folder
205
+ if ( ! fs . existsSync ( fileName ) ) {
206
+ fileName = `src/${ options . file } ` ;
207
+ }
208
+
209
+ const scripts = fs . readFileSync ( fileName , 'utf8' ) ;
186
210
context . asserts . length = 0 ;
187
211
console . log ( interpreter . jsPythonInfo ( ) )
188
212
console . log ( `> ${ options . file } ` )
189
213
try {
190
214
const res = await interpreter
191
- . registerPackagesLoader ( packageLoader as PackageLoader )
192
- . registerModuleLoader ( moduleLoader )
193
- . evaluate ( scripts , initialScope , options . entryFunction || undefined , options . file ) ;
215
+ . registerPackagesLoader ( packageLoader as PackageLoader )
216
+ . registerModuleLoader ( moduleLoader )
217
+ . evaluate ( scripts , initialScope , options . entryFunction || undefined , options . file ) ;
194
218
195
219
if ( ! ! res || res === 0 ) {
196
220
console . log ( '>' , res ) ;
0 commit comments