mirror of
https://github.com/infiniflow/ragflow.git
synced 2025-12-08 20:42:30 +08:00
Refa: improve usability of Node.js/JavaScript code executor (#8979)
### What problem does this PR solve? Improve usability of Node.js/JavaScript code executor. ### Type of change - [x] Refactoring --------- Co-authored-by: Kevin Hu <kevinhu.sh@gmail.com> Co-authored-by: writinwaters <93570324+writinwaters@users.noreply.github.com>
This commit is contained in:
@ -82,20 +82,40 @@ const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
const args = JSON.parse(process.argv[2]);
|
||||
|
||||
const mainPath = path.join(__dirname, 'main.js');
|
||||
|
||||
function isPromise(value) {
|
||||
return Boolean(value && typeof value.then === 'function');
|
||||
}
|
||||
|
||||
if (fs.existsSync(mainPath)) {
|
||||
const { main } = require(mainPath);
|
||||
const mod = require(mainPath);
|
||||
const main = typeof mod === 'function' ? mod : mod.main;
|
||||
|
||||
if (typeof main !== 'function') {
|
||||
console.error('Error: main is not a function');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
if (typeof args === 'object' && args !== null) {
|
||||
main(args).then(result => {
|
||||
if (result !== null) {
|
||||
console.log(result);
|
||||
try {
|
||||
const result = main(args);
|
||||
if (isPromise(result)) {
|
||||
result.then(output => {
|
||||
if (output !== null) {
|
||||
console.log(output);
|
||||
}
|
||||
}).catch(err => {
|
||||
console.error('Error in async main function:', err);
|
||||
});
|
||||
} else {
|
||||
if (result !== null) {
|
||||
console.log(result);
|
||||
}
|
||||
}
|
||||
}).catch(err => {
|
||||
console.error('Error in main function:', err);
|
||||
});
|
||||
} catch (err) {
|
||||
console.error('Error when executing main:', err);
|
||||
}
|
||||
} else {
|
||||
console.error('Error: args is not a valid object:', args);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user