Skip to content

Commit de5bb6f

Browse files
committed
changes launch an app with extra logging
1 parent 7183e23 commit de5bb6f

File tree

2 files changed

+57
-44
lines changed

2 files changed

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

src/index.ts

Lines changed: 56 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ const context: any = {
99
params: {}
1010
}
1111

12+
// catch the uncaught errors that weren't wrapped in a domain or try catch statement
13+
// do not use this in modules, but only in applications, as otherwise we could have multiple of these bound
14+
process
15+
.on('unhandledRejection', (reason, p) => {
16+
console.error(reason, 'Unhandled Rejection at Promise', p);
17+
})
18+
.on('uncaughtException', err => {
19+
console.error(err, 'Uncaught Exception thrown');
20+
process.exit(1);
21+
});
1222

1323
const initialScope: Record<string, any> = {
1424
app: {}
@@ -18,8 +28,6 @@ const rootFolder = process.cwd().split('\\').join('/');
1828
const interpreter: Interpreter = jsPython() as Interpreter;
1929
const options = getOptionsFromArguments(process.argv);
2030

21-
run();
22-
2331
function trimChar(text: string, charToRemove: string): string {
2432
while (text.charAt(0) == charToRemove) {
2533
text = text.substring(1);
@@ -79,7 +87,6 @@ function registerFileLoader(interpreter: Interpreter): Interpreter {
7987
return interpreter;
8088
}
8189

82-
8390
async function initialize(baseSource: string) {
8491
// process app.json (if exists)
8592
// add configuration to the 'app'
@@ -118,46 +125,6 @@ async function initialize(baseSource: string) {
118125
}
119126
}
120127

121-
async function run() {
122-
if (options.version) {
123-
console.log(interpreter.jsPythonInfo());
124-
console.log(`JSPython cli v${(pkg || {}).version}\n`);
125-
}
126-
127-
if (options.output) {
128-
var util = require('util');
129-
var logFile = fs.createWriteStream(options.output, { flags: 'w' });
130-
var logStdout = process.stdout;
131-
132-
console.log = function () {
133-
const req = new RegExp('\\x1b\\[\\d\\dm', 'g');
134-
logFile.write(util.format.apply(null, Array.from(arguments).map(a => a && a.replace ? a.replace(req, '') : a)) + '\n');
135-
logStdout.write(util.format.apply(null, arguments) + '\n');
136-
}
137-
console.error = console.log;
138-
}
139-
140-
await initialize(options.srcRoot);
141-
142-
if (options.file) {
143-
interpreter.registerPackagesLoader(packageLoader as PackageLoader);
144-
registerFileLoader(interpreter)
145-
const scripts = fs.readFileSync(`${options.srcRoot}${options.file}`, 'utf8');
146-
context.asserts.length = 0;
147-
console.log(interpreter.jsPythonInfo())
148-
console.log(`> ${options.file}`)
149-
try {
150-
const res = await interpreter.evaluate(scripts, initialScope, options.entryFunction || undefined, options.file);
151-
if (res !== null) {
152-
console.log(res);
153-
}
154-
} catch (err) {
155-
console.log('JSPython execution failed: ', err?.message || err, err);
156-
throw err;
157-
}
158-
}
159-
}
160-
161128
function getOptionsFromArguments(rawArgs: string[]) {
162129
const args = arg({
163130
'--file': String,
@@ -222,3 +189,49 @@ function packageLoader(packageName: string): any {
222189
throw err;
223190
}
224191
}
192+
193+
async function main() {
194+
try {
195+
if (options.version) {
196+
console.log(interpreter.jsPythonInfo());
197+
console.log(`JSPython cli v${(pkg || {}).version}\n`);
198+
}
199+
200+
if (options.output) {
201+
var util = require('util');
202+
var logFile = fs.createWriteStream(options.output, { flags: 'w' });
203+
var logStdout = process.stdout;
204+
205+
console.log = function () {
206+
const req = new RegExp('\\x1b\\[\\d\\dm', 'g');
207+
logFile.write(util.format.apply(null, Array.from(arguments).map(a => a && a.replace ? a.replace(req, '') : a)) + '\n');
208+
logStdout.write(util.format.apply(null, arguments) + '\n');
209+
}
210+
console.error = console.log;
211+
}
212+
213+
await initialize(options.srcRoot);
214+
215+
if (options.file) {
216+
interpreter.registerPackagesLoader(packageLoader as PackageLoader);
217+
registerFileLoader(interpreter)
218+
const scripts = fs.readFileSync(`${options.srcRoot}${options.file}`, 'utf8');
219+
context.asserts.length = 0;
220+
console.log(interpreter.jsPythonInfo())
221+
console.log(`> ${options.file}`)
222+
try {
223+
const res = await interpreter.evaluate(scripts, initialScope, options.entryFunction || undefined, options.file);
224+
console.log('Finished.', res !== null ? res : '');
225+
} catch (err) {
226+
console.log('JSPython execution failed: ', err?.message || err, err);
227+
throw err;
228+
}
229+
}
230+
} catch (e) {
231+
console.log('Main function error:', e?.message || e)
232+
}
233+
}
234+
235+
main()
236+
.then(s => console.log('Completed:', s))
237+
.catch(e => console.log('Error:', e?.message || e))

0 commit comments

Comments
 (0)