11import fs from 'fs' ;
22import { jsPython , Interpreter , PackageLoader } from 'jspython-interpreter' ;
3+ import { Assert } from './assert' ;
34import { InterpreterOptions } from './types' ;
45import { trimChar } from './utils' ;
56
67const rootFolder = process . cwd ( ) . split ( '\\' ) . join ( '/' ) ;
7- const initialScope : any = { } ;
8+ export const initialScope : any = {
9+ asserts : [ ] as AssertInfo [ ]
10+ } ;
811
912type NodeJsInterpreter = Interpreter & { evaluateFile : ( fileName : string ) => Promise < any > } ;
13+ export type AssertInfo = { success : boolean ; name : string ; description ?: string } ;
14+ type LogInfo = { level : 'info' | 'fail' | 'success' ; message : string ; time : Date ; logId ?: string } ;
1015
11- const context : any = {
12- asserts : [ ] ,
16+ const context : {
17+ params : any ;
18+ } = {
1319 params : { }
1420} ;
1521
16- initialScope . assert = ( condition : boolean , name ?: string , description ?: string ) =>
17- context . asserts . push ( { condition, name, description } ) ;
18- initialScope . showAsserts = ( ) => console . table ( context . asserts ) ;
22+ function logFn ( msg : LogInfo ) : void {
23+ const level = msg . level === 'success' ? msg . level : msg . level + ' ' ;
24+
25+ console . log ( `| ${ msg . time . toTimeString ( ) . slice ( 0 , 8 ) } | ${ level } | ${ msg . message } ` ) ;
26+ }
27+
28+ function assert ( name : string , dataContext ?: boolean | any ) : Assert | void {
29+ // an original case when
30+ if ( typeof dataContext === 'boolean' ) {
31+ logFn ( {
32+ level : dataContext ? 'success' : 'fail' ,
33+ message : name || '' ,
34+ time : new Date ( )
35+ } ) ;
36+ initialScope . asserts . push ( { success : ! ! dataContext , name } ) ;
37+ return ;
38+ }
39+
40+ function assertCallback ( success : boolean , message : string ) {
41+ logFn ( {
42+ logId : name ,
43+ level : success ? 'success' : 'fail' ,
44+ message : `${ name } ${ message ? ':' : '' } ${ message || '' } ` ,
45+ time : new Date ( )
46+ } ) ;
47+
48+ const existingAssert = initialScope . asserts ?. find ( ( a : AssertInfo ) => a . name === name ) ;
49+ if ( existingAssert ) {
50+ // only if condition it is not fail yet
51+ if ( ! ! existingAssert . success ) {
52+ existingAssert . success = ! ! success ;
53+ existingAssert . description = message ;
54+ }
55+ } else {
56+ initialScope . asserts . push ( { success : ! ! success , name : name , description : message } ) ;
57+ }
58+ }
59+
60+ return new Assert ( name , dataContext , assertCallback ) ;
61+ }
62+
63+ function getScript ( fileName : string ) : string {
64+ if ( ! fs . existsSync ( fileName ) ) {
65+ throw Error ( `File not found` ) ;
66+ }
67+
68+ const scripts = fs . readFileSync ( fileName , 'utf8' ) ;
69+ return scripts ;
70+ }
71+
72+ async function initialize ( baseSource : string ) {
73+ // process app.js (if exists)
74+ // - run _init
75+ // - delete _ init
76+ // - run _initAsync
77+ // - delete _initAsync
78+ // - load content into 'app'
79+
80+ let appJsPath = `${ rootFolder } /${ baseSource } app.js` ;
81+ console . log ( { rootFolder, baseSource } ) ;
82+ if ( ! fs . existsSync ( appJsPath ) ) {
83+ appJsPath = `${ rootFolder } /src/app.js` ;
84+ }
85+
86+ if ( fs . existsSync ( appJsPath ) ) {
87+ const app = require ( appJsPath ) ;
88+
89+ if ( typeof app . _init == 'function' ) {
90+ app . _init ( ) ;
91+ delete app . _init ;
92+ }
93+
94+ if ( typeof app . _initAsync == 'function' ) {
95+ await app . _initAsync ( ) ;
96+ delete app . _initAsync ;
97+ }
98+
99+ Object . assign ( initialScope , app ) ;
100+ }
101+ }
102+
103+ initialScope . assert = ( name : string , dataContext : any ) => assert ( name , dataContext ) ;
104+ initialScope . showAsserts = ( ) => console . table ( initialScope . asserts ) ;
105+ initialScope . print = ( ...args : any ) =>
106+ logFn ( {
107+ time : new Date ( ) ,
108+ level : 'info' ,
109+ message : args . map ( ( v : any ) => ( typeof v === 'object' ? JSON . stringify ( v ) : v ) ) . join ( ' ' )
110+ } ) ;
19111initialScope . params = ( name : string ) => {
20112 const value = context . params [ name ] ;
21113 return value === undefined ? null : value ;
@@ -41,7 +133,7 @@ export function jsPythonForNode(
41133 entryFunctionName ?: string | undefined ,
42134 moduleName ?: string | undefined
43135 ) {
44- context . asserts . length = 0 ;
136+ initialScope . asserts . splice ( 0 , initialScope . asserts . length ) ;
45137 await initialize ( options . srcRoot || '' ) ;
46138 return evaluate . call ( interpreter , script , evaluationContext , entryFunctionName , moduleName ) ;
47139 } ;
@@ -112,43 +204,3 @@ export function jsPythonForNode(
112204 }
113205 }
114206}
115-
116- function getScript ( fileName : string ) : string {
117- if ( ! fs . existsSync ( fileName ) ) {
118- throw Error ( `File not found` ) ;
119- }
120-
121- const scripts = fs . readFileSync ( fileName , 'utf8' ) ;
122- return scripts ;
123- }
124-
125- async function initialize ( baseSource : string ) {
126- // process app.js (if exists)
127- // - run _init
128- // - delete _ init
129- // - run _initAsync
130- // - delete _initAsync
131- // - load content into 'app'
132-
133- let appJsPath = `${ rootFolder } /${ b
38BA
aseSource } app.js` ;
134- console . log ( { rootFolder, baseSource } ) ;
135- if ( ! fs . existsSync ( appJsPath ) ) {
136- appJsPath = `${ rootFolder } /src/app.js` ;
137- }
138-
139- if ( fs . existsSync ( appJsPath ) ) {
140- const app = require ( appJsPath ) ;
141-
142- if ( typeof app . _init == 'function' ) {
143- app . _init ( ) ;
144- delete app . _init ;
145- }
146-
147- if ( typeof app . _initAsync == 'function' ) {
148- await app . _initAsync ( ) ;
149- delete app . _initAsync ;
150- }
151-
152- Object . assign ( initialScope , app ) ;
153- }
154- }
0 commit comments