@@ -9,21 +9,34 @@ const context: any = {
99 params : { }
1010}
1111
12- const appFolder = process . cwd ( ) . split ( '\\' ) . join ( '/' ) ;
1312
1413const initialScope : Record < string , any > = {
1514 app : { }
1615} ;
1716
17+ const rootFolder = process . cwd ( ) . split ( '\\' ) . join ( '/' ) ;
1818const interpreter : Interpreter = jsPython ( ) as Interpreter ;
19+ const options = getOptionsFromArguments ( process . argv ) ;
1920
2021run ( ) ;
2122
22- async function initialize ( ) {
23+ function trimChar ( text : string , charToRemove : string ) : string {
24+ while ( text . charAt ( 0 ) == charToRemove ) {
25+ text = text . substring ( 1 ) ;
26+ }
27+
28+ while ( text . charAt ( text . length - 1 ) == charToRemove ) {
29+ text = text . substring ( 0 , text . length - 1 ) ;
30+ }
31+
32+ return text ;
33+ }
34+
35+ async function initialize ( baseSource : string ) {
2336 // process app.json (if exists)
2437 // add configuration to the 'app'
25- if ( fs . existsSync ( `${ appFolder } / app.json` ) ) {
26- const app = require ( `${ appFolder } / app.json` ) ;
38+ if ( fs . existsSync ( `${ rootFolder } / ${ baseSource } app.json` ) ) {
39+ const app = require ( `${ rootFolder } / ${ baseSource } app.json` ) ;
2740 initialScope . app = Object . assign ( initialScope . app || { } , app ) ;
2841 }
2942
@@ -33,8 +46,8 @@ async function initialize() {
3346 // - run _initAsync
3447 // - delete _initAsync
3548 // - load content into 'app'
36- if ( fs . existsSync ( `${ appFolder } / app.js` ) ) {
37- const app = require ( `${ appFolder } / app.js` ) ;
49+ if ( fs . existsSync ( `${ rootFolder } / ${ baseSource } app.js` ) ) {
50+ const app = require ( `${ rootFolder } / ${ baseSource } app.js` ) ;
3851
3952 if ( typeof app . _init == 'function' ) {
4053 app . _init ( ) ;
@@ -58,9 +71,6 @@ async function initialize() {
5871}
5972
6073async function run ( ) {
61- await initialize ( ) ;
62-
63- const options = getOptionsFromArguments ( process . argv ) ;
6474 if ( options . version ) {
6575 console . log ( interpreter . jsPythonInfo ( ) ) ;
6676 console . log ( `JSPython cli v${ ( pkg || { } ) . version } \n` ) ;
@@ -79,27 +89,40 @@ async function run() {
7989 console . error = console . log ;
8090 }
8191
92+ await initialize ( options . srcRoot ) ;
93+
8294 if ( options . file ) {
95+ interpreter . registerFileLoader ( ( jspyPath : any ) => {
96+ console . log ( ' * file loader * ' , rootFolder , jspyPath ) ;
97+ return Promise . resolve ( { sum : ( x : number , y : number ) => x + y } ) ;
98+ } )
8399 interpreter . registerPackagesLoader ( packageLoader as PackageLoader ) ;
84- const scripts = fs . readFileSync ( options . file , 'utf8' ) ;
100+ const scripts = fs . readFileSync ( ` ${ options . srcRoot } ${ options . file } ` , 'utf8' ) ;
85101 context . asserts . length = 0 ;
86102 console . log ( interpreter . jsPythonInfo ( ) )
87103 console . log ( `> ${ options . file } ` )
88- const res = await interpreter . evaluate ( scripts , initialScope , options . entryFunction || undefined , options . file ) ;
89- if ( res !== null ) {
90- console . log ( res ) ;
104+ try {
105+ const res = await interpreter . evaluate ( scripts , initialScope , options . entryFunction || undefined , options . file ) ;
106+ if ( res !== null ) {
107+ console . log ( res ) ;
108+ }
109+ } catch ( err ) {
110+ console . log ( 'JSPython execution failed: ' , err ?. message || err , err ) ;
111+ throw err ;
91112 }
92113 }
93114}
94115
95116function getOptionsFromArguments ( rawArgs : string [ ] ) {
96117 const args = arg ( {
97118 '--file' : String ,
119+ '--srcRoot' : String ,
98120 '--entryFunction' : String ,
99121 '--version' : Boolean ,
100122 '--output' : String ,
101123 '-f' : '--file' ,
102- '-ef' : '--entryFunction' ,
124+ '-s' : '--srcRoot' ,
125+ '-e' : '--entryFunction' ,
103126 '-v' : '--version' ,
104127 '-o' : '--output'
105128 } , {
@@ -121,9 +144,15 @@ function getOptionsFromArguments(rawArgs: string[]) {
121144 file : args [ '--file' ] || ( rawArgs . length === 3 && ! rawArgs [ 2 ] . startsWith ( '-' ) ? rawArgs [ 2 ] : '' ) ,
122145 version : args [ '--version' ] ,
123146 output : args [ '--output' ] ,
124- entryFunction : args [ '--entryFunction' ]
147+ entryFunction : args [ '--entryFunction' ] ,
148+ srcRoot : args [ '--srcRoot' ] || ''
125149 } ;
126150
151+ res . srcRoot = trimChar ( res . srcRoot || '' , '/' ) ;
152+ if ( res . srcRoot . length ) {
153+ res . srcRoot = res . srcRoot + '/' ;
154+ }
155+
127156 context . params = { ...res , ...params } ;
128157
129158 return res ;
@@ -138,13 +167,13 @@ function packageLoader(packageName: string): any {
138167 }
139168
140169 if ( packageName . toLowerCase ( ) . endsWith ( '.js' ) || packageName . toLowerCase ( ) . endsWith ( '.json' ) ) {
141- return require ( `${ process . cwd ( ) . split ( '\\' ) . join ( '/' ) } / ${ packageName } ` )
170+ return require ( `${ rootFolder } / ${ options . srcRoot } ${ packageName } ` )
142171 }
143172
144- return require ( `${ process . cwd ( ) . split ( '\\' ) . join ( '/' ) } /node_modules/${ packageName } ` ) ;
173+ return require ( `${ rootFolder } /node_modules/${ packageName } ` ) ;
145174 }
146175 catch ( err ) {
147- console . log ( 'Import Error: ' , err ) ;
176+ console . log ( 'Import Error: ' , err ?. message || err ) ;
148177 throw err ;
149178 }
150179}
0 commit comments