Skip to content

Commit 4eef13f

Browse files
committed
Fix single export "TypeError: cannot read properties" error
Signed-off-by: Debdut Chakraborty <debdut.chakraborty@rocket.chat>
1 parent f574fff commit 4eef13f

File tree

1 file changed

+7
-14
lines changed

1 file changed

+7
-14
lines changed

src/compiler/AppsEngineValidator.ts

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ export class AppsEngineValidator {
5555
const { App: EngineBaseApp } = this.appSourceRequire('@rocket.chat/apps-engine/definition/App');
5656
const mainClassModule = this.compiledRequire(mainClassFile, compilationResult);
5757

58+
// if the main class is default export use that,
59+
// else take it from the object { [mainClassFile]: Class }
5860
const RealApp = typeof mainClassModule === 'function'
5961
? mainClassModule
6062
: mainClassModule.default
@@ -110,23 +112,14 @@ export class AppsEngineValidator {
110112
exports,
111113
});
112114

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

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

0 commit comments

Comments
 (0)