Skip to content

Commit 422ed96

Browse files
PavloPavlo
authored andcommitted
replaced file loader with module loader
1 parent 2b74b1e commit 422ed96

File tree

2 files changed

+19
-50
lines changed

2 files changed

+19
-50
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jspython-cli",
3-
"version": "2.0.21",
3+
"version": "2.0.22",
44
"description": "CLI for jspython. Allows you to run jspython (*.jspy) files",
55
"main": "src/index.ts",
66
"bin": {
@@ -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.17"
33+
"jspython-interpreter": "~2.1.2"
3434
},
3535
"devDependencies": {
3636
"rollup": "^1.27.13",

src/index.ts

Lines changed: 17 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -41,53 +41,6 @@ function trimChar(text: string, charToRemove: string): string {
4141
return text;
4242
}
4343

44-
function registerFileLoader(interpreter: Interpreter): Interpreter {
45-
function getScriptFunctionsListWithArgs(scripts: string): any {
46-
if (scripts) {
47-
return scripts.split('\n').map(line => {
48-
const match = line.match(/(def\s)(.*)(\(.*\)):/);
49-
if (match) {
50-
const args = (match[3] as string).replace(/[\(\)\s+]/g, '').split(',') as string[];
51-
return [match[2].replace(/ /g, '') as string, args] as [string, string[]];
52-
}
53-
}).filter(func => Boolean(func));
54-
}
55-
}
56-
57-
const loader = async (filePath: string) => {
58-
const module = filePath;
59-
try {
60-
61-
const script = fs.readFileSync(`${options.srcRoot}${trimChar(filePath, '/')}.jspy`, 'utf8');
62-
const funcs = getScriptFunctionsListWithArgs(script);
63-
const result = funcs
64-
.reduce((prev: any, [func, paramKeys]: any) => {
65-
prev[func] = async (...args: any) => {
66-
const params = (paramKeys || []).reduce((prev2: any, key: any, i: any) => {
67-
prev2[key] = args[i] || null;
68-
return prev2;
69-
}, {});
70-
const context = {};// this.getAppContext();
71-
try {
72-
return await interpreter.evaluate(script, { ...context, ...params }, func, module);
73-
} catch (e) {
74-
throw Error(`${e.message} `);
75-
}
76-
};
77-
return prev;
78-
}, {}
79-
);
80-
81-
return result;
82-
} catch (e) {
83-
console.error(e);
84-
}
85-
86-
};
87-
interpreter.registerFileLoader(loader);
88-
return interpreter;
89-
}
90-
9144
async function initialize(baseSource: string) {
9245
// process app.json (if exists)
9346
// add configuration to the 'app'
@@ -171,6 +124,21 @@ function getOptionsFromArguments(rawArgs: string[]) {
171124
return res;
172125
}
173126

127+
function moduleLoader(filePath: string): Promise<string> {
128+
try {
129+
const script = fs.readFileSync(`${options.srcRoot}${trimChar(filePath, '/')}.jspy`, 'utf8');
130+
return Promise.resolve(script);
131+
} catch (e) {
132+
try {
133+
// try without JSPY
134+
const script = fs.readFileSync(`${options.srcRoot}${trimChar(filePath, '/')}`, 'utf8');
135+
return Promise.resolve(script);
136+
} catch (e) {
137+
return Promise.reject(e);
138+
}
139+
}
140+
}
141+
174142
/**@type {PackageLoader} */
175143
function packageLoader(packageName: string): any {
176144
try {
@@ -213,7 +181,8 @@ async function main() {
213181

214182
if (options.file) {
215183
interpreter.registerPackagesLoader(packageLoader as PackageLoader);
216-
registerFileLoader(interpreter)
184+
interpreter.registerModuleLoader(moduleLoader);
185+
217186
const scripts = fs.readFileSync(`${options.srcRoot}${options.file}`, 'utf8');
218187
context.asserts.length = 0;
219188
console.log(interpreter.jsPythonInfo())

0 commit comments

Comments
 (0)