7
7
*/
8
8
'use strict'
9
9
10
- const fs = require ( 'fs' )
11
10
const path = require ( 'path' )
12
11
13
12
const log = require ( '../../log' )
@@ -60,23 +59,18 @@ function _extractModuleNameAndHandlerPath (handler) {
60
59
}
61
60
62
61
/**
63
- * Returns the correct path of the file to be patched
64
- * when required.
62
+ * Returns all possible paths of the files to be patched when required.
65
63
*
66
64
* @param {* } lambdaStylePath the path comprised of the `LAMBDA_TASK_ROOT`,
67
65
* the root of the module of the Lambda handler, and the module name.
68
- * @returns the lambdaStylePath with the appropiate extension for the hook.
66
+ * @returns the lambdaStylePath with appropiate extensions for the hook.
69
67
*/
70
- function _getLambdaFilePath ( lambdaStylePath ) {
71
- let lambdaFilePath = lambdaStylePath
72
- if ( fs . existsSync ( lambdaStylePath + '.js' ) ) {
73
- lambdaFilePath += '.js'
74
- } else if ( fs . existsSync ( lambdaStylePath + '.mjs' ) ) {
75
- lambdaFilePath += '.mjs'
76
- } else if ( fs . existsSync ( lambdaStylePath + '.cjs' ) ) {
77
- lambdaFilePath += '.cjs'
78
- }
79
- return lambdaFilePath
68
+ function _getLambdaFilePaths ( lambdaStylePath ) {
69
+ return [
70
+ `${ lambdaStylePath } .js` ,
71
+ `${ lambdaStylePath } .mjs` ,
72
+ `${ lambdaStylePath } .cjs`
73
+ ]
80
74
}
81
75
82
76
/**
@@ -92,12 +86,13 @@ const registerLambdaHook = () => {
92
86
const [ _module ] = _extractModuleNameAndHandlerPath ( moduleAndHandler )
93
87
94
88
const lambdaStylePath = path . resolve ( lambdaTaskRoot , moduleRoot , _module )
95
- const lambdaFilePath = _getLambdaFilePath ( lambdaStylePath )
89
+ const lambdaFilePaths = _getLambdaFilePaths ( lambdaStylePath )
96
90
97
- Hook ( [ lambdaFilePath ] , ( moduleExports ) => {
91
+ // TODO: Redo this like any other instrumentation.
92
+ Hook ( lambdaFilePaths , ( moduleExports , name ) => {
98
93
require ( './patch' )
99
94
100
- for ( const { hook } of instrumentations [ lambdaFilePath ] ) {
95
+ for ( const { hook } of instrumentations [ name ] ) {
101
96
try {
102
97
moduleExports = hook ( moduleExports )
103
98
} catch ( e ) {
@@ -133,6 +128,6 @@ const registerLambdaHook = () => {
133
128
module . exports = {
134
129
_extractModuleRootAndHandler,
135
130
_extractModuleNameAndHandlerPath,
136
- _getLambdaFilePath ,
131
+ _getLambdaFilePaths ,
137
132
registerLambdaHook
138
133
}
0 commit comments