Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 7 additions & 14 deletions src/compiler/AppsEngineValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ export class AppsEngineValidator {
const { App: EngineBaseApp } = this.appSourceRequire('@rocket.chat/apps-engine/definition/App');
const mainClassModule = this.compiledRequire(mainClassFile, compilationResult);

// if the main class is default export use that,
// else take it from the object { [mainClassFile]: Class }
const RealApp = typeof mainClassModule === 'function'
? mainClassModule
: mainClassModule.default
Expand Down Expand Up @@ -110,23 +112,14 @@ export class AppsEngineValidator {
exports,
});

const result = vm.runInContext(compilationResult.files[`${ filename }.js` as keyof ICompilerResult['files']].compiled, context);
vm.runInContext(compilationResult.files[`${ filename }.js` as keyof ICompilerResult['files']].compiled, context);

/**
* `result` will contain ONLY the result of the last line evaluated
* in the script by `vm.runInContext`, and NOT the full `exports` object.
*
* However, we need to handle this case due to backwards compatibility,
* since the main class file might export a class with an unknown name,
* which was supported in the early versions of the Apps-Engine.
*
* So here, if we find that the required file exports ONLY ONE property,
* which is what happens in the case of the main class file, we can return
* the `result`; otherwise, we return the full `exports` object.
* exports will always contain everything
* for result = vm.runInContext(...), result == Object.values(exports)[0] or exports[filename]
* since compiledRequired is not classFile only method (called recursively), better to return all exports
* and extract the main class constructor in the checkInheritance method.
*/
if (Object.keys(exports).length === 1) {
return result;
}
return exports;
}
}