@@ -3,33 +3,67 @@ import fs from 'fs';
33import { jsPython , Interpreter , PackageLoader } from 'jspython-interpreter' ;
44
55const pkg = require ( '../package.json' ) ;
6- const appConfig = require ( `${ process . cwd ( ) . split ( '\\' ) . join ( '/' ) } /jspy.config.js` )
7- || require ( `${ process . cwd ( ) . split ( '\\' ) . join ( '/' ) } /jspy.config.json` )
8- || { } ;
96
107const context : any = {
118 asserts : [ ] ,
129 params : { }
1310}
14- export const interpreter : Interpreter = jsPython ( ) as Interpreter ;
15- interpreter . addFunction ( 'assert' , ( condition : boolean , name ?: string , description ?: string ) => {
16- context . asserts . push ( { condition, name, description } ) ;
17- } ) ;
18- interpreter . addFunction ( 'showAsserts' , ( ) => {
19- console . table ( context . asserts ) ;
20- } ) ;
21- interpreter . addFunction ( 'params' , ( name : string ) => {
22- const value = context . params [ name ] ;
23- return value === undefined ? null : value ;
24- } ) ;
11+
12+ const appFolder = process . cwd ( ) . split ( '\\' ) . join ( '/' ) ;
13+
14+ const initialScope : Record < string , any > = {
15+ app : { }
16+ } ;
17+
18+ const interpreter : Interpreter = jsPython ( ) as Interpreter ;
2519
2620run ( ) ;
2721
22+ async function initialize ( ) {
23+ // process app.json (if exists)
24+ // add configuration to the 'app'
25+ if ( fs . existsSync ( `${ appFolder } /app.json` ) ) {
26+ const app = require ( `${ appFolder } /app.json` ) ;
27+ initialScope . app = Object . assign ( initialScope . app || { } , app ) ;
28+ }
29+
30+ // process app.js (if exists)
31+ // - run _init
32+ // - delete _ init
33+ // - run _initAsync
34+ // - delete _initAsync
35+ // - load content into 'app'
36+ if ( fs . existsSync ( `${ appFolder } /app.js` ) ) {
37+ const app = require ( `${ appFolder } /app.js` ) ;
38+
39+ if ( typeof app . _init == 'function' ) {
40+ app . _init ( ) ;
41+ delete app . _init ;
42+ }
43+
44+ if ( typeof app . _initAsync == 'function' ) {
45+ await app . _initAsync ( ) ;
46+ delete app . _initAsync ;
47+ }
48+
49+ Object . assign ( initialScope , app ) ;
50+ }
51+
52+ initialScope . assert = ( condition : boolean , name ?: string , description ?: string ) => context . asserts . push ( { condition, name, description } ) ;
53+ initialScope . showAsserts = ( ) => console . table ( context . asserts ) ;
54+ initialScope . params = ( name : string ) => {
55+ const value = context . params [ name ] ;
56+ return value === undefined ? null : value ;
57+ }
58+ }
59+
2860async function run ( ) {
61+ await initialize ( ) ;
62+
2963 const options = getOptionsFromArguments ( process . argv ) ;
3064 if ( options . version ) {
3165 console . log ( interpreter . jsPythonInfo ( ) ) ;
32- console . log ( `JSPython cli v${ ( pkg || { } ) . version } \n` ) ;
66+ console . log ( `JSPython cli v${ ( pkg || { } ) . version } \n` ) ;
3367 }
3468
3569 if ( options . output ) {
@@ -51,7 +85,7 @@ async function run() {
5185 context . asserts . length = 0 ;
5286 console . log ( interpreter . jsPythonInfo ( ) )
5387 console . log ( `> ${ options . file } ` )
54- const res = await interpreter . evaluate ( scripts , appConfig , undefined , options . file ) ;
88+ const res = await interpreter . evaluate ( scripts , initialScope , options . entryFunction || undefined , options . file ) ;
5589 if ( res !== null ) {
5690 console . log ( res ) ;
5791 }
@@ -61,9 +95,11 @@ async function run() {
6195function getOptionsFromArguments ( rawArgs : string [ ] ) {
6296 const args = arg ( {
6397 '--file' : String ,
98+ '--entryFunction' : String ,
6499 '--version' : Boolean ,
65100 '--output' : String ,
66101 '-f' : '--file' ,
102+ '-ef' : '--entryFunction' ,
67103 '-v' : '--version' ,
68104 '-o' : '--output'
69105 } , {
@@ -84,7 +120,8 @@ function getOptionsFromArguments(rawArgs: string[]) {
84120 const res = {
85121 file : args [ '--file' ] || ( rawArgs . length === 3 && ! rawArgs [ 2 ] . startsWith ( '-' ) ? rawArgs [ 2 ] : '' ) ,
86122 version : args [ '--version' ] ,
87- output : args [ '--output' ]
123+ output : args [ '--output' ] ,
124+ entryFunction : args [ '--entryFunction' ]
88125 } ;
89126
90127 context . params = { ...res , ...params } ;
0 commit comments