@@ -12,6 +12,10 @@ function escapeRegExp(str) {
1212 return str . replace ( / [ \- \[ \] \/ \{ \} \( \) \* \+ \? \. \\ \^ \$ \| ] / g, '\\$&' ) ;
1313}
1414
15+ function isJsFile ( str ) {
16+ return ! ! str . match ( / \. c ? j s $ / ) ;
17+ }
18+
1519function defaultLogger ( params ) {
1620 console . log (
1721 `${ chalk . bgYellow . black ( 'api-mocker' ) } ${
@@ -164,7 +168,7 @@ module.exports = function (urlRoot, pathRoot) {
164168 } ;
165169
166170 const returnForPath = function ( filePath , requestParams ) {
167- if ( filePath . endsWith ( '.js' ) ) {
171+ if ( isJsFile ( filePath ) ) {
168172 logger ( {
169173 req, filePath, fileType : 'js' , config
170174 } ) ;
@@ -217,12 +221,11 @@ module.exports = function (urlRoot, pathRoot) {
217221 if ( methodFileExtension === 'auto' ) {
218222 methodFileExtension = req . accepts ( [ 'json' , 'xml' ] ) ;
219223 }
220- const jsMockFile = `${ req . method } .js` ;
221- const staticMockFile = `${ req . method } .${ methodFileExtension } ` ;
222- const wildcardJsMockFile = 'ANY.js' ;
223- const wildcardStaticMockFile = `ANY.${ methodFileExtension } ` ;
224224
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 ] ;
226229
227230 const matchedMethodFile = methodFiles . find ( ( methodFile ) => fs . existsSync ( path . join ( targetFullPath , methodFile ) ) ) ;
228231
0 commit comments