@@ -5,7 +5,8 @@ import { httpGet, httpPost, httpDelete, httpPut } from './http';
55
66const pkg = require ( '../package.json' ) ;
77const context : any = {
8- asserts : [ ]
8+ asserts : [ ] ,
9+ params : { }
910}
1011export const interpreter : Interpreter = jsPython ( ) as Interpreter ;
1112interpreter . addFunction ( 'httpGet' , httpGet ) ;
@@ -15,9 +16,13 @@ interpreter.addFunction('httpPut', httpPut);
1516interpreter . addFunction ( 'assert' , ( condition : boolean , name ?: string , description ?: string ) => {
1617 context . asserts . push ( { condition, name, description } ) ;
1718} ) ;
18- interpreter . addFunction ( 'showAsserts' , ( condition : boolean , name ?: string , description ?: string ) => {
19+ interpreter . addFunction ( 'showAsserts' , ( ) => {
1920 console . table ( context . asserts ) ;
2021} ) ;
22+ interpreter . addFunction ( 'params' , ( name : string ) => {
23+ return context . params [ name ] ;
24+ } ) ;
25+
2126run ( ) ;
2227
2328async function run ( ) {
@@ -26,11 +31,24 @@ async function run() {
2631 console . log ( `Version:\n${ pkg . version } \n` ) ;
2732 }
2833
34+ if ( options . output ) {
35+ var util = require ( 'util' ) ;
36+ var logFile = fs . createWriteStream ( options . output , { flags : 'w' } ) ;
37+ var logStdout = process . stdout ;
38+
39+ console . log = function ( ) {
40+ const req = new RegExp ( '\\x1b\\[\\d\\dm' , 'g' ) ;
41+ logFile . write ( util . format . apply ( null , Array . from ( arguments ) . map ( a => a && a . replace ? a . replace ( req , '' ) : a ) ) + '\n' ) ;
42+ logStdout . write ( util . format . apply ( null , arguments ) + '\n' ) ;
43+ }
44+ console . error = console . log ;
45+ }
46+
2947 if ( options . file ) {
3048 interpreter . registerPackagesLoader ( packageLoader as PackageLoader ) ;
3149 const scripts = fs . readFileSync ( options . file , 'utf8' ) ;
3250 context . asserts . length = 0 ;
33- const res = await interpreter . evaluate ( scripts , context ) ;
51+ const res = await interpreter . evaluate ( scripts ) ;
3452 console . log ( 'Execution result:\n' , res ) ;
3553 console . log ( 'Finish' ) ;
3654 }
@@ -40,16 +58,34 @@ function getOptionsFromArguments(rawArgs: string[]) {
4058 const args = arg ( {
4159 '--file' : String ,
4260 '--version' : Boolean ,
61+ '--output' : String ,
4362 '-f' : '--file' ,
44- '-v' : '--version'
63+ '-v' : '--version' ,
64+ '-o' : '--output'
4565 } , {
46- argv : rawArgs . slice ( 2 )
66+ argv : rawArgs . slice ( 2 ) ,
67+ permissive : true
4768 } ) ;
4869
49- return {
70+ const params = args . _ . reduce ( ( obj : { [ key : string ] : any } , a : string ) => {
71+ const kv = a . replace ( '--' , '' ) ;
72+ let [ key , val ] : any = kv . split ( '=' ) ;
73+ if ( kv === key ) {
74+ val = true ;
75+ }
76+ obj [ key ] = val ;
77+ return obj ;
78+ } , { } ) ;
79+
80+ const res = {
5081 file : args [ '--file' ] || ( rawArgs . length === 3 && ! rawArgs [ 2 ] . startsWith ( '-' ) ? rawArgs [ 2 ] : '' ) ,
51- version : args [ '--version' ]
82+ version : args [ '--version' ] ,
83+ output : args [ '--output' ]
5284 } ;
85+
86+ context . params = { ...res , ...params } ;
87+
88+ return res ;
5389}
5490
5591/**@type {PackageLoader } */
0 commit comments