@@ -9,6 +9,16 @@ const context: any = {
9
9
params : { }
10
10
}
11
11
12
+ // catch the uncaught errors that weren't wrapped in a domain or try catch statement
13
+ // do not use this in modules, but only in applications, as otherwise we could have multiple of these bound
14
+ process
15
+ . on ( 'unhandledRejection' , ( reason , p ) => {
16
+ console . error ( reason , 'Unhandled Rejection at Promise' , p ) ;
17
+ } )
18
+ . on ( 'uncaughtException' , err => {
19
+ console . error ( err , 'Uncaught Exception thrown' ) ;
20
+ process . exit ( 1 ) ;
21
+ } ) ;
12
22
13
23
const initialScope : Record < string , any > = {
14
24
app : { }
@@ -18,8 +28,6 @@ const rootFolder = process.cwd().split('\\').join('/');
18
28
const interpreter : Interpreter = jsPython ( ) as Interpreter ;
19
29
const options = getOptionsFromArguments ( process . argv ) ;
20
30
21
- run ( ) ;
22
-
23
31
function trimChar ( text : string , charToRemove : string ) : string {
24
32
while ( text . charAt ( 0 ) == charToRemove ) {
25
33
text = text . substring ( 1 ) ;
@@ -79,7 +87,6 @@ function registerFileLoader(interpreter: Interpreter): Interpreter {
79
87
return interpreter ;
80
88
}
81
89
82
-
83
90
async function initialize ( baseSource : string ) {
84
91
// process app.json (if exists)
85
92
// add configuration to the 'app'
@@ -118,46 +125,6 @@ async function initialize(baseSource: string) {
118
125
}
119
126
}
120
127
121
- async function run ( ) {
122
- if ( options . version ) {
123
- console . log ( interpreter . jsPythonInfo ( ) ) ;
124
- console . log ( `JSPython cli v${ ( pkg || { } ) . version } \n` ) ;
125
- }
126
-
127
- if ( options . output ) {
128
- var util = require ( 'util' ) ;
129
- var logFile = fs . createWriteStream ( options . output , { flags : 'w' } ) ;
130
- var logStdout = process . stdout ;
131
-
132
- console . log = function ( ) {
133
- const req = new RegExp ( '\\x1b\\[\\d\\dm' , 'g' ) ;
134
- logFile . write ( util . format . apply ( null , Array . from ( arguments ) . map ( a => a && a . replace ? a . replace ( req , '' ) : a ) ) + '\n' ) ;
135
- logStdout . write ( util . format . apply ( null , arguments ) + '\n' ) ;
136
- }
137
- console . error = console . log ;
138
- }
139
-
140
- await initialize ( options . srcRoot ) ;
141
-
142
- if ( options . file ) {
143
- interpreter . registerPackagesLoader ( packageLoader as PackageLoader ) ;
144
- registerFileLoader ( interpreter )
145
- const scripts = fs . readFileSync ( `${ options . srcRoot } ${ options . file } ` , 'utf8' ) ;
146
- context . asserts . length = 0 ;
147
- console . log ( interpreter . jsPythonInfo ( ) )
148
- console . log ( `> ${ options . file } ` )
149
- try {
150
- const res = await interpreter . evaluate ( scripts , initialScope , options . entryFunction || undefined , options . file ) ;
151
- if ( res !== null ) {
152
- console . log ( res ) ;
153
- }
154
- } catch ( err ) {
155
- console . log ( 'JSPython execution failed: ' , err ?. message || err , err ) ;
156
- throw err ;
157
- }
158
- }
159
- }
160
-
161
128
function getOptionsFromArguments ( rawArgs : string [ ] ) {
162
129
const args = arg ( {
163
130
'--file' : String ,
@@ -222,3 +189,49 @@ function packageLoader(packageName: string): any {
222
189
throw err ;
223
190
}
224
191
}
192
+
193
+ async function main ( ) {
194
+ try {
195
+ if ( options . version ) {
196
+ console . log ( interpreter . jsPythonInfo ( ) ) ;
197
+ console . log ( `JSPython cli v${ ( pkg || { } ) . version } \n` ) ;
198
+ }
199
+
200
+ if ( options . output ) {
201
+ var util = require ( 'util' ) ;
202
+ var logFile = fs . createWriteStream ( options . output , { flags : 'w' } ) ;
203
+ var logStdout = process . stdout ;
204
+
205
+ console . log = function ( ) {
206
+ const req = new RegExp ( '\\x1b\\[\\d\\dm' , 'g' ) ;
207
+ logFile . write ( util . format . apply ( null , Array . from ( arguments ) . map ( a => a && a . replace ? a . replace ( req , '' ) : a ) ) + '\n' ) ;
208
+ logStdout . write ( util . format . apply ( null , arguments ) + '\n' ) ;
209
+ }
210
+ console . error = console . log ;
211
+ }
212
+
213
+ await initialize ( options . srcRoot ) ;
214
+
215
+ if ( options . file ) {
216
+ interpreter . registerPackagesLoader ( packageLoader as PackageLoader ) ;
217
+ registerFileLoader ( interpreter )
218
+ const scripts = fs . readFileSync ( `${ options . srcRoot } ${ options . file } ` , 'utf8' ) ;
219
+ context . asserts . length = 0 ;
220
+ console . log ( interpreter . jsPythonInfo ( ) )
221
+ console . log ( `> ${ options . file } ` )
222
+ try {
223
+ const res = await interpreter . evaluate ( scripts , initialScope , options . entryFunction || undefined , options . file ) ;
224
+ console . log ( 'Finished.' , res !== null ? res : '' ) ;
225
+ } catch ( err ) {
226
+ console . log ( 'JSPython execution failed: ' , err ?. message || err , err ) ;
227
+ throw err ;
228
+ }
229
+ }
230
+ } catch ( e ) {
231
+ console . log ( 'Main function error:' , e ?. message || e )
232
+ }
233
+ }
234
+
235
+ main ( )
236
+ . then ( s => console . log ( 'Completed:' , s ) )
237
+ . catch ( e => console . log ( 'Error:' , e ?. message || e ) )
0 commit comments