@@ -12,6 +12,10 @@ function escapeRegExp(str) {
12
12
return str . replace ( / [ \- \[ \] \/ \{ \} \( \) \* \+ \? \. \\ \^ \$ \| ] / g, '\\$&' ) ;
13
13
}
14
14
15
+ function isJsFile ( str ) {
16
+ return ! ! str . match ( / \. c ? j s $ / ) ;
17
+ }
18
+
15
19
function defaultLogger ( params ) {
16
20
console . log (
17
21
`${ chalk . bgYellow . black ( 'api-mocker' ) } ${
@@ -164,7 +168,7 @@ module.exports = function (urlRoot, pathRoot) {
164
168
} ;
165
169
166
170
const returnForPath = function ( filePath , requestParams ) {
167
- if ( filePath . endsWith ( '.js' ) ) {
171
+ if ( isJsFile ( filePath ) ) {
168
172
logger ( {
169
173
req, filePath, fileType : 'js' , config
170
174
} ) ;
@@ -217,12 +221,11 @@ module.exports = function (urlRoot, pathRoot) {
217
221
if ( methodFileExtension === 'auto' ) {
218
222
methodFileExtension = req . accepts ( [ 'json' , 'xml' ] ) ;
219
223
}
220
- const jsMockFile = `${ req . method } .js` ;
221
- const staticMockFile = `${ req . method } .${ methodFileExtension } ` ;
222
- const wildcardJsMockFile = 'ANY.js' ;
223
- const wildcardStaticMockFile = `ANY.${ methodFileExtension } ` ;
224
224
225
- const methodFiles = [ jsMockFile , staticMockFile , wildcardJsMockFile , wildcardStaticMockFile ] ;
225
+ const fileExtensions = [ methodFileExtension , 'cjs' , 'js' ] ;
226
+ const jsMockFiles = fileExtensions . map ( ( ext ) => `${ req . method } .${ ext } ` ) ;
227
+ const wildcardJsMockFiles = fileExtensions . map ( ( ext ) => `ANY.${ ext } ` ) ;
228
+ const methodFiles = [ ...jsMockFiles , ...wildcardJsMockFiles ] ;
226
229
227
230
const matchedMethodFile = methodFiles . find ( ( methodFile ) => fs . existsSync ( path . join ( targetFullPath , methodFile ) ) ) ;
228
231
0 commit comments