@@ -3,33 +3,67 @@ import fs from 'fs';
3
3
import { jsPython , Interpreter , PackageLoader } from 'jspython-interpreter' ;
4
4
5
5
const 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
- || { } ;
9
6
10
7
const context : any = {
11
8
asserts : [ ] ,
12
9
params : { }
13
10
}
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 ;
25
19
26
20
run ( ) ;
27
21
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
+
28
60
async function run ( ) {
61
+ await initialize ( ) ;
62
+
29
63
const options = getOptionsFromArguments ( process . argv ) ;
30
64
if ( options . version ) {
31
65
console . log ( interpreter . jsPythonInfo ( ) ) ;
32
- console . log ( `JSPython cli v${ ( pkg || { } ) . version } \n` ) ;
66
+ console . log ( `JSPython cli v${ ( pkg || { } ) . version } \n` ) ;
33
67
}
34
68
35
69
if ( options . output ) {
@@ -51,7 +85,7 @@ async function run() {
51
85
context . asserts . length = 0 ;
52
86
console . log ( interpreter . jsPythonInfo ( ) )
53
87
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 ) ;
55
89
if ( res !== null ) {
56
90
console . log ( res ) ;
57
91
}
@@ -61,9 +95,11 @@ async function run() {
61
95
function getOptionsFromArguments ( rawArgs : string [ ] ) {
62
96
const args = arg ( {
63
97
'--file' : String ,
98
+ '--entryFunction' : String ,
64
99
'--version' : Boolean ,
65
100
'--output' : String ,
66
101
'-f' : '--file' ,
102
+ '-ef' : '--entryFunction' ,
67
103
'-v' : '--version' ,
68
104
'-o' : '--output'
69
105
} , {
@@ -84,7 +120,8 @@ function getOptionsFromArguments(rawArgs: string[]) {
84
120
const res = {
85
121
file : args [ '--file' ] || ( rawArgs . length === 3 && ! rawArgs [ 2 ] . startsWith ( '-' ) ? rawArgs [ 2 ] : '' ) ,
86
122
version : args [ '--version' ] ,
87
- output : args [ '--output' ]
123
+ output : args [ '--output' ] ,
124
+ entryFunction : args [ '--entryFunction' ]
88
125
} ;
89
126
90
127
context . params = { ...res , ...params } ;
0 commit comments