Skip to content

Commit 2af86ac

Browse files
committed
Add custom params and output log option
1 parent fe06fc3 commit 2af86ac

File tree

3 files changed

+71
-18
lines changed

3 files changed

+71
-18
lines changed

README.md

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
# jspython CLI.
1+
# JSPython CLI.
22

3-
Command line interface to run [jspython](https://github.com/jspython-dev/jspython) files (*.jspy)
3+
We provide a command line interface to run [JSPython](https://github.com/jspython-dev/jspython) scripts
44

5-
### Install
5+
### Install from NPM
66

77
```
8-
npm install -g @jspython-dev/jspython-cli
8+
npm install -g jspython-cli
99
```
1010

1111
### Run in terminal
@@ -16,10 +16,27 @@ Command line interface to run [jspython](https://github.com/jspython-dev/jspytho
1616
1717
```
1818

19+
### Pass parameters to script
20+
In CLI
21+
```
22+
jspython --file=path/to/jspython/file --param1=value --param
23+
jspython path/to/jspython/file param1=value param
24+
```
25+
In script
26+
```py
27+
params("param1") == "value" # true
28+
params("param") == false # true
29+
```
30+
31+
### Save evaluate log into file
32+
```
33+
jspython --file=path/to/jspython/file.jspy --output=path/to/log.txt
34+
```
35+
1936
### Development
20-
Run example using node. (Works only if you have `@jspython-dev/jspython-cli` devDependencies in you project)
37+
Run example using node. (Works only if you have build project `npm run build`)
2138
```
22-
node ./bin/jspython --file=examples/project/axios-test.jspy
23-
node ./bin/jspython --file examples/project/parse.jspy
39+
node ./bin/jspython --file=../jspython-examples/axios-test.jspy
40+
node ./bin/jspython --file ../jspython-examples/parse.jspy
2441
```
2542

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jspython-cli",
3-
"version": "0.0.6",
3+
"version": "0.0.7",
44
"description": "CLI for jspython. Allows you to run jspython (*.jspy) files",
55
"main": "src/index.ts",
66
"bin": {
@@ -32,9 +32,9 @@
3232
"jspython-interpreter": "~0.1.3"
3333
},
3434
"devDependencies": {
35-
"rollup": "^1.27.6",
35+
"rollup": "^1.27.13",
3636
"rollup-plugin-copy": "^3.1.0",
37-
"rollup-plugin-typescript2": "^0.25.2",
38-
"typescript": "^3.7.2"
37+
"rollup-plugin-typescript2": "^0.25.3",
38+
"typescript": "^3.7.3"
3939
}
4040
}

src/index.ts

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { httpGet, httpPost, httpDelete, httpPut } from './http';
55

66
const pkg = require('../package.json');
77
const context: any = {
8-
asserts: []
8+
asserts: [],
9+
params: {}
910
}
1011
export const interpreter: Interpreter = jsPython() as Interpreter;
1112
interpreter.addFunction('httpGet', httpGet);
@@ -15,9 +16,13 @@ interpreter.addFunction('httpPut', httpPut);
1516
interpreter.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+
2126
run();
2227

2328
async 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

Comments
 (0)