Skip to content

adding support for .cjs file extensions for user function #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
18 changes: 12 additions & 6 deletions src/utils/UserFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,18 @@ function _resolveHandler(object: any, nestedProperty: string): any {
}

/**
* Verify that the provided path can be loaded as a file per:
* Try to get path to a file path if it can loaded as a file per:
* https://nodejs.org/dist/latest-v10.x/docs/api/modules.html#modules_all_together
* @param string - the fully resolved file path to the module
* @return bool
* @return string
*/
function _canLoadAsFile(modulePath: string): boolean {
return fs.existsSync(modulePath) || fs.existsSync(modulePath + ".js");
function _getLoadAsFilePath(modulePath: string): string {
const paths = [modulePath + ".cjs", modulePath + ".js", modulePath];
for (const path of paths) {
if (fs.existsSync(path)) {
return path;
}
}
}

/**
Expand All @@ -74,8 +79,9 @@ function _canLoadAsFile(modulePath: string): boolean {
*/
function _tryRequire(appRoot: string, moduleRoot: string, module: string): any {
const lambdaStylePath = path.resolve(appRoot, moduleRoot, module);
if (_canLoadAsFile(lambdaStylePath)) {
return require(lambdaStylePath);
const filePath = _getLoadAsFilePath(lambdaStylePath);
if (filePath) {
return require(filePath);
} else {
// Why not just require(module)?
// Because require() is relative to __dirname, not process.cwd()
Expand Down