Skip to content

Commit 8337782

Browse files
committed
added import *.js amd *.json files
1 parent fd73398 commit 8337782

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

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.0.4",
3+
"version": "2.0.5",
44
"description": "CLI for jspython. Allows you to run jspython (*.jspy) files",
55
"main": "src/index.ts",
66
"bin": {

src/index.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ interpreter.addFunction('showAsserts', () => {
2020
console.table(context.asserts);
2121
});
2222
interpreter.addFunction('params', (name: string) => {
23-
return context.params[name];
23+
const value = context.params[name];
24+
return value === undefined ? null : value;
2425
});
2526

2627
run();
@@ -48,9 +49,12 @@ async function run() {
4849
interpreter.registerPackagesLoader(packageLoader as PackageLoader);
4950
const scripts = fs.readFileSync(options.file, 'utf8');
5051
context.asserts.length = 0;
51-
const res = await interpreter.evaluate(scripts);
52-
console.log('Execution result:\n', res);
53-
console.log('Finish');
52+
console.log("JSPython (c) FalconSoft Ltd")
53+
console.log(`${options.file}`)
54+
const res = await interpreter.evaluate(scripts, undefined, undefined, options.file);
55+
if (res !== null) {
56+
console.log(res);
57+
}
5458
}
5559
}
5660

@@ -90,15 +94,20 @@ function getOptionsFromArguments(rawArgs: string[]) {
9094

9195
/**@type {PackageLoader} */
9296
function packageLoader(packageName: string): any {
93-
if (['fs', 'path', 'readline', 'timers', 'child_process', 'util', 'zlib', 'stream', 'net', 'https', 'http', 'events', 'os', 'buffer']
94-
.includes(packageName)) {
95-
return require(packageName)
96-
}
97-
9897
try {
98+
if (['fs', 'path', 'readline', 'timers', 'child_process', 'util', 'zlib', 'stream', 'net', 'https', 'http', 'events', 'os', 'buffer']
99+
.includes(packageName)) {
100+
return require(packageName)
101+
}
102+
103+
if (packageName.toLowerCase().endsWith('.js') || packageName.toLowerCase().endsWith('.json')) {
104+
return require(`${process.cwd().split('\\').join('/')}/${packageName}`)
105+
}
106+
99107
return require(`${process.cwd().split('\\').join('/')}/node_modules/${packageName}`);
100108
}
101109
catch (err) {
102110
console.log('Import Error: ', err);
111+
throw err;
103112
}
104113
}

0 commit comments

Comments
 (0)