8000 added alternative 'src' folder · jspython-dev/jspython-cli@70b1cd0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 70b1cd0

Browse files
committed
added alternative 'src' folder
1 parent 1f82623 commit 70b1cd0

File tree

2 files changed

+47
-23
lines changed

2 files changed

+47
-23
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.1.5",
3+
"version": "2.1.6",
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.1.5"
33+
"jspython-interpreter": "^2.1.7"
3434
},
3535
"devDependencies": {
3636
"rollup": "^1.27.13",

src/index.ts

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,22 @@ function trimChar(text: string, charToRemove: string): string {
4343
}
4444

4545
async function initialize(baseSource: string) {
46-
// process app.json (if exists)
47-
// add configuration to the 'app'
48-
if (fs.existsSync(`${rootFolder}/${baseSource}app.json`)) {
49-
const app = require(`${rootFolder}/${baseSource}app.json`);
50-
initialScope.app = Object.assign(initialScope.app || {}, app);
51-
}
5246

5347
// process app.js (if exists)
5448
// - run _init
5549
// - delete _ init
5650
// - run _initAsync
5751
// - delete _initAsync
5852
// - load content into 'app'
59-
if (fs.existsSync(`${rootFolder}/${baseSource}app.js`)) {
60-
const app = require(`${rootFolder}/${baseSource}app.js`);
53+
54+
let appJsPath = `${rootFolder}/${baseSource}app.js`;
55+
console.log({ rootFolder, baseSource })
56+
if (!fs.existsSync(appJsPath)) {
57+
appJsPath = `${rootFolder}/src/app.js`
58+
}
59+
60+
if (fs.existsSync(appJsPath)) {
61+
const app = require(appJsPath);
6162

6263
if (typeof app._init == 'function') {
6364
app._init();
@@ -126,18 +127,23 @@ function getOptionsFromArguments(rawArgs: string[]) {
126127
}
127128

128129
function moduleLoader(filePath: string): Promise<string> {
130+
filePath = trimChar(trimChar(filePath, '/'), '.');
131+
let fileName = `${options.srcRoot}${filePath}.jspy`;
132+
133+
if (!fs.existsSync(fileName)) {
134+
fileName = `${options.srcRoot}${filePath}`;
135+
}
136+
137+
if (!fs.existsSync(fileName)) {
138+
fileName = `src/${filePath}`;
139+
}
140+
129141
try {
130-
const script = fs.readFileSync(`${options.srcRoot}${trimChar(filePath, '/')}.jspy`, 'utf8');
142+
const script = fs.readFileSync(fileName, 'utf8');
131143
return Promise.resolve(script);
132144
} catch (e) {
133-
try {
134-
// try without JSPY
135-
const script = fs.readFileSync(`${options.srcRoot}${trimChar(filePath, '/')}`, 'utf8');
136-
return Promise.resolve(script);
137-
} catch (e) {
138-
console.log('* module loader error ', e?.message || e)
139-
return Promise.reject(e);
140-
}
145+
console.log('* module loader error ', e?.message || e)
146+
return Promise.reject(e);
141147
}
142148
}
143149

@@ -162,6 +168,17 @@ function packageLoader(packageName: string): any {
162168
}
163169

164170
async function main() {
171+
if (!options.file && !options.version) {
172+
console.log(interpreter.jsPythonInfo());
173+
console.log(`JSPython cli v${(pkg || {}).version}\n`);
174+
console.log(` :\> jspython (fileName.jspy)`);
175+
console.log(` :\> jspython -f=(fileName.jspy)`);
176+
console.log(` :\> jspython --file=(fileName.jspy)`);
177+
console.log(` :\> jspython --file=(fileName.jspy) --srcRoot=src`);
178+
console.log(' ');
179+
return;
180+
}
181+
165182
if (options.version) {
166183
console.log(interpreter.jsPythonInfo());
167184
console.log(`JSPython cli v${(pkg || {}).version}\n`);
@@ -182,15 +199,22 @@ async function main() {
182199
await initialize(options.srcRoot);
183200

184201
if (options.file) {
185-
const scripts = fs.readFileSync(`${options.srcRoot}${options.file}`, 'utf8');
202+
let fileName = `${options.srcRoot}${options.file}`;
203+
204+
// try to check if file exists in 'src' folder
205+
if (!fs.existsSync(fileName)) {
206+
fileName = `src/${options.file}`;
207+
}
208+
209+
const scripts = fs.readFileSync(fileName, 'utf8');
186210
context.asserts.length = 0;
187211
console.log(interpreter.jsPythonInfo())
188212
console.log(`> ${options.file}`)
189213
try {
190214
const res = await interpreter
191-
.registerPackagesLoader(packageLoader as PackageLoader)
192-
.registerModuleLoader(moduleLoader)
193-
.evaluate(scripts, initialScope, options.entryFunction || undefined, options.file);
215+
.registerPackagesLoader(packageLoader as PackageLoader)
216+
.registerModuleLoader(moduleLoader)
217+
.evaluate(scripts, initialScope, options.entryFunction || undefined, options.file);
194218

195219
if (!!res || res === 0) {
196220
console.log('>', res);

0 commit comments

Comments
 (0)
0