Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 7183e23

Browse filesBrowse files
committed
added file loader code (same as worksheet systems)
1 parent 7fde788 commit 7183e23
Copy full SHA for 7183e23

File tree

Expand file treeCollapse file tree

3 files changed

+51
-5
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+51
-5
lines changed

‎README.md

Copy file name to clipboardExpand all lines: README.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ Run example using node. (Works only if you have build project `npm run build`)
8989
```
9090
node ./bin/jspython --file=../jspython-examples/axios-test.jspy
9191
node ./bin/jspython --file ../jspython-examples/parse.jspy
92+
node ./bin/jspython --file=test.jspy --srcRoot=../jspython-examples/
9293
```
9394

9495
# License

‎package.json

Copy file name to clipboardExpand all lines: package.json
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"homepage": "https://github.com/jspython-dev/jspython-cli#readme",
3131
"dependencies": {
3232
"arg": "^4.1.2",
33-
"jspython-interpreter": "~2.0.13"
33+
"jspython-interpreter": "~2.0.14"
3434
},
3535
"devDependencies": {
3636
"rollup": "^1.27.13",

‎src/index.ts

Copy file name to clipboardExpand all lines: src/index.ts
+49-4Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,54 @@ function trimChar(text: string, charToRemove: string): string {
3232
return text;
3333
}
3434

35+
function registerFileLoader(interpreter: Interpreter): Interpreter {
36+
function getScriptFunctionsListWithArgs(scripts: string): any {
37+
if (scripts) {
38+
return scripts.split('\n').map(line => {
39+
const match = line.match(/(def\s)(.*)(\(.*\)):/);
40+
if (match) {
41+
const args = (match[3] as string).replace(/[\(\)\s+]/g, '').split(',') as string[];
42+
return [match[2].replace(/ /g, '') as string, args] as [string, string[]];
43+
}
44+
}).filter(func => Boolean(func));
45+
}
46+
}
47+
48+
const loader = async (filePath: string) => {
49+
const module = filePath;
50+
try {
51+
52+
const script = fs.readFileSync(`${options.srcRoot}${trimChar(filePath, '/')}.jspy`, 'utf8');
53+
const funcs = getScriptFunctionsListWithArgs(script);
54+
const result = funcs
55+
.reduce((prev: any, [func, paramKeys]: any) => {
56+
prev[func] = async (...args: any) => {
57+
const params = (paramKeys || []).reduce((prev2: any, key: any, i: any) => {
58+
prev2[key] = args[i] || null;
59+
return prev2;
60+
}, {});
61+
const context = {};// this.getAppContext();
62+
try {
63+
return await interpreter.evaluate(script, { ...context, ...params }, func, module);
64+
} catch (e) {
65+
throw Error(`${e.message} `);
66+
}
67+
};
68+
return prev;
69+
}, {}
70+
);
71+
72+
return result;
73+
} catch (e) {
74+
console.error(e);
75+
}
76+
77+
};
78+
interpreter.registerFileLoader(loader);
79+
return interpreter;
80+
}
81+
82+
3583
async function initialize(baseSource: string) {
3684
// process app.json (if exists)
3785
// add configuration to the 'app'
@@ -92,11 +140,8 @@ async function run() {
92140
await initialize(options.srcRoot);
93141

94142
if (options.file) {
95-
interpreter.registerFileLoader((jspyPath: any) => {
96-
console.log(' * file loader * ', rootFolder, jspyPath);
97-
return Promise.resolve({ sum: (x: number, y: number) => x + y });
98-
})
99143
interpreter.registerPackagesLoader(packageLoader as PackageLoader);
144+
registerFileLoader(interpreter)
100145
const scripts = fs.readFileSync(`${options.srcRoot}${options.file}`, 'utf8');
101146
context.asserts.length = 0;
102147
console.log(interpreter.jsPythonInfo())

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.