Skip to content

Commit af9fc6b

Browse files
committed
arguments and logging fixes
1 parent ab7f15a commit af9fc6b

File tree

4 files changed

+17
-14
lines changed

4 files changed

+17
-14
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ Command line interface to run [JSPython](https://github.com/jspython-dev/jspytho
1717
1818
```
1919

20-
### Pass parameters to script
20+
### JSPython command line arguments
2121
In CLI
2222
```
23-
jspython --file=path/to/jspython/file --param1=value --param
24-
jspython path/to/jspython/file param1=value param
23+
jspython --file=path/to/jspython/file --arg1=value --arg2='test value 1'
24+
jspython path/to/jspython/file arg1=value
2525
```
26-
In script
26+
Inside your JSPython script yu can access arguments with `args` object.
2727
```py
28-
params.param1 == "value" # true
29-
params.param == false # true
28+
a1 = args.arg1
29+
a2 = args.arg2 or 'another value'
3030
```
3131

3232
### Run file
@@ -54,7 +54,7 @@ jspython -f path/to/jspython/file.jspy -e myFunc1
5454
```
5555
jspython --file=path/to/jspython/file.jspy --srcRoot=src
5656
```
57-
Normally you would expect package.json and node_modules to be in the root level and all scripts in the `src` folder
57+
Normally, you would expect package.json and node_modules to be in the root level and all scripts in the `src` folder
5858

5959
```
6060
-|- .git

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jspython-cli",
3-
"version": "2.1.15",
3+
"version": "2.1.16",
44
"description": "CLI for jspython. Allows you to run jspython (*.jspy) files",
55
"main": "./lib/public-api.js",
66
"bin": {

src/cli.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ function getOptionsFromArguments(rawArgs: string[]): InterpreterOptions {
7575
async function main() {
7676
const options = getOptionsFromArguments(process.argv);
7777
const interpreter: Interpreter = (await jsPythonForNode(options)) as Interpreter;
78-
const jspyContext: Record<string, any> = { ...initialScope, ...{ params: options.params } };
78+
const jspyContext: Record<string, any> = { ...initialScope, ...{ args: options.params } };
7979

8080
if (options.version) {
8181
console.log(interpreter.jsPythonInfo());

src/jspython-node.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@ import { trimChar } from './utils';
66

77
const rootFolder = process.cwd().split('\\').join('/');
88
export const initialScope: any = {
9+
session: {},
910
asserts: [] as AssertInfo[]
1011
};
1112

1213
type NodeJsInterpreter = Interpreter & { evaluateFile: (fileName: string) => Promise<any> };
1314
export type AssertInfo = { success: boolean; name: string; description?: string };
1415
type LogInfo = { level: 'info' | 'fail' | 'success'; message: string; time: Date; logId?: string };
1516

17+
let previousLogMessage = '';
18+
1619
const context: {
1720
params: any;
1821
} = {
@@ -21,8 +24,12 @@ const context: {
2124

2225
function logFn(msg: LogInfo): void {
2326
const level = msg.level === 'success' ? msg.level : msg.level + ' ';
27+
const message = `${level} | ${msg.message}`;
2428

25-
console.log(`| ${msg.time.toTimeString().slice(0, 8)} | ${level} | ${msg.message}`);
29+
if (message !== previousLogMessage) {
30+
console.log(`| ${msg.time.toTimeString().slice(0, 8)} | ${message}`);
31+
previousLogMessage = message;
32+
}
2633
}
2734

2835
function assert(name: string, dataContext?: boolean | any): Assert | void {
@@ -114,10 +121,6 @@ initialScope.print = (...args: any) =>
114121
level: 'info',
115122
message: args.map((v: any) => (typeof v === 'object' ? JSON.stringify(v) : v)).join(' ')
116123
});
117-
initialScope.params = (name: string) => {
118-
const value = context.params[name];
119-
return value === undefined ? null : value;
120-
};
121124

122125
export async function jsPythonForNode(
123126
options: InterpreterOptions = {

0 commit comments

Comments
 (0)