Skip to content

Commit

Permalink
Fix single export "TypeError: cannot read properties" error
Browse files Browse the repository at this point in the history
Signed-off-by: Debdut Chakraborty <[email protected]>
  • Loading branch information
debdutdeb committed Jan 6, 2022
1 parent f574fff commit 3a5b719
Showing 1 changed file with 7 additions and 14 deletions.
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
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
* if result = vm.runInContext(...), then result == Object.values(exports)[0] or exports[filename]
* since compiledRequired is not classFile only, better to return all exports
* and extract the main class in the validation method.
*/
if (Object.keys(exports).length === 1) {
return result;
}
return exports;
}
}

0 comments on commit 3a5b719

Please sign in to comment.