@@ -11,18 +11,21 @@ type NodeJsInterpreter = Interpreter & { evaluateFile: (fileName: string) => Pro
1111const context : any = {
1212 asserts : [ ] ,
1313 params : { }
14- }
15-
16- initialScope . assert = ( condition : boolean , name ?: string , description ?: string ) => context . asserts . push ( { condition, name, description } ) ;
17- initialScope . showAsserts = ( ) => console . table ( context . asserts ) ;
18- initialScope . params = ( name : string ) => {
19- const value = context . params [ name ] ;
20- return value === undefined ? null : value ;
14+ } ;
15+
16+ initialScope . assert = ( condition : boolean , name ?: string , description ?: string ) =>
17+ context . asserts . push ( { condition, name, description } ) ;
18+ initialScope . showAsserts = ( ) => console . table ( context . asserts ) ;
19+ initialScope . params = ( name : string ) => {
20+ const value = context . params [ name ] ;
21+ return value === undefined ? null : value ;
22+ } ;
23+
24+ export function jsPythonForNode (
25+ options : InterpreterOptions = {
26+ srcRoot : ''
2127 }
22-
23- export function jsPythonForNode ( options : InterpreterOptions = {
24- srcRoot : ''
25- } ) : NodeJsInterpreter {
28+ ) : NodeJsInterpreter {
2629 const interpreter : NodeJsInterpreter = jsPython ( ) as NodeJsInterpreter ;
2730 Object . assign ( context . params , options . params ) ;
2831
@@ -32,26 +35,30 @@ export function jsPythonForNode(options: InterpreterOptions = {
3235
3336 const evaluate = interpreter . evaluate ;
3437
35- interpreter . evaluate = async function ( script : string , evaluationContext ?: object | undefined , entryFunctionName ?: string | undefined , moduleName ?: string | undefined ) {
38+ interpreter . evaluate = async function (
39+ script : string ,
40+ evaluationContext ?: object | undefined ,
41+ entryFunctionName ?: string | undefined ,
42+ moduleName ?: string | undefined
43+ ) {
3644 context . asserts . length = 0 ;
37- await initialize ( options . srcRoot ) ;
45+ await initialize ( options . srcRoot || '' ) ;
3846 return evaluate . call ( interpreter , script , evaluationContext , entryFunctionName , moduleName ) ;
39- }
47+ } ;
4048
41- interpreter . evaluateFile = function ( filePath : string , context = { } ) {
49+ interpreter . evaluateFile = function ( filePath : string , context = { } ) {
4250 const script = getScript ( filePath ) ;
4351 return interpreter . evaluate ( script , context , options . entryFunction ) ;
44- }
52+ } ;
4553
4654 return interpreter ;
4755
48-
4956 function moduleLoader ( filePath : string ) : Promise < string > {
5057 filePath = trimChar ( trimChar ( filePath , '/' ) , '.' ) ;
51- let fileName = `${ options . srcRoot } ${ filePath } .jspy` ;
58+ let fileName = `${ options . srcRoot || '' } ${ filePath } .jspy` ;
5259
5360 if ( ! fs . existsSync ( fileName ) ) {
54- fileName = `${ options . srcRoot } ${ filePath } ` ;
61+ fileName = `${ options . srcRoot || '' } ${ filePath } ` ;
5562 }
5663
5764 if ( ! fs . existsSync ( fileName ) ) {
@@ -62,26 +69,44 @@ export function jsPythonForNode(options: InterpreterOptions = {
6269 const script = fs . readFileSync ( fileName , 'utf8' ) ;
6370 return Promise . resolve ( script ) ;
6471 } catch ( e ) {
65- console . log ( '* module loader error ' , ( e as Error ) ?. message || e )
72+ console . log ( '* module loader error ' , ( e as Error ) ?. message || e ) ;
6673 return Promise . reject ( e ) ;
6774 }
6875 }
6976
7077 /**@type {PackageLoader } */
7178 function packageLoader ( packageName : string ) : any {
7279 try {
73- if ( [ 'fs' , 'path' , 'readline' , 'timers' , 'child_process' , 'util' , 'zlib' , 'stream' , 'net' , 'https' , 'http' , 'events' , 'os' , 'buffer' ]
74- . includes ( packageName ) ) {
75- return require ( packageName )
80+ if (
81+ [
82+ 'fs' ,
83+ 'path' ,
84+ 'readline' ,
85+ 'timers' ,
86+ 'child_process' ,
87+ 'util' ,
88+ 'zlib' ,
89+ 'stream' ,
90+ 'net' ,
91+ 'https' ,
92+ 'http' ,
93+ 'events' ,
94+ 'os' ,
95+ 'buffer'
96+ ] . includes ( packageName )
97+ ) {
98+ return require ( packageName ) ;
7699 }
77100
78- if ( packageName . toLowerCase ( ) . endsWith ( '.js' ) || packageName . toLowerCase ( ) . endsWith ( '.json' ) ) {
79- return require ( `${ rootFolder } /${ options . srcRoot } ${ packageName } ` )
101+ if (
102+ packageName . toLowerCase ( ) . endsWith ( '.js' ) ||
103+ packageName . toLowerCase ( ) . endsWith ( '.json' )
104+ ) {
105+ return require ( `${ rootFolder } /${ options . srcRoot || '' } ${ packageName } ` ) ;
80106 }
81107
82108 return require ( `${ rootFolder } /node_modules/${ packageName } ` ) ;
83- }
84- catch ( err ) {
109+ } catch ( err ) {
85110 console . log ( 'Import Error: ' , ( err as Error ) ?. message ?? err ) ;
86111 throw err ;
87112 }
@@ -90,15 +115,14 @@ export function jsPythonForNode(options: InterpreterOptions = {
90115
91116function getScript ( fileName : string ) : string {
92117 if ( ! fs . existsSync ( fileName ) ) {
93- throw Error ( `File not found` )
118+ throw Error ( `File not found` ) ;
94119 }
95120
96121 const scripts = fs . readFileSync ( fileName , 'utf8' ) ;
97122 return scripts ;
98123}
99124
100125async function initialize ( baseSource : string ) {
101-
102126 // process app.js (if exists)
103127 // - run _init
104128 // - delete _ init
@@ -107,9 +131,9 @@ async function initialize(baseSource: string) {
107131 // - load content into 'app'
108132
109133 let appJsPath = `${ rootFolder } /${ baseSource } app.js` ;
110- console . log ( { rootFolder, baseSource } )
134+ console . log ( { rootFolder, baseSource } ) ;
111135 if ( ! fs . existsSync ( appJsPath ) ) {
112- appJsPath = `${ rootFolder } /src/app.js`
136+ appJsPath = `${ rootFolder } /src/app.js` ;
113137 }
114138
115139 if ( fs . existsSync ( appJsPath ) ) {
0 commit comments