@@ -263,6 +263,18 @@ Common.prepareAppConf = function(opts, app) {
263
263
return app ;
264
264
} ;
265
265
266
+ /**
267
+ * Definition of known config file extensions with their type
268
+ */
269
+ Common . knonwConfigFileExtensions = {
270
+ '.json' : 'json' ,
271
+ '.yml' : 'yaml' ,
272
+ '.yaml' : 'yaml' ,
273
+ '.config.js' : 'js' ,
274
+ '.config.cjs' : 'js' ,
275
+ '.config.mjs' : 'mjs'
276
+ }
277
+
266
278
/**
267
279
* Check if filename is a configuration file
268
280
* @param {string } filename
@@ -271,19 +283,20 @@ Common.prepareAppConf = function(opts, app) {
271
283
Common . isConfigFile = function ( filename ) {
272
284
if ( typeof ( filename ) !== 'string' )
273
285
return null ;
274
- if ( filename . indexOf ( '.json' ) !== - 1 )
275
- return 'json' ;
276
- if ( filename . indexOf ( '.yml' ) > - 1 || filename . indexOf ( '.yaml' ) > - 1 )
277
- return 'yaml' ;
278
- if ( filename . indexOf ( '.config.js' ) !== - 1 )
279
- return 'js' ;
280
- if ( filename . indexOf ( '.config.cjs' ) !== - 1 )
281
- return 'js' ;
282
- if ( filename . indexOf ( '.config.mjs' ) !== - 1 )
283
- return 'mjs' ;
286
+
287
+ for ( let extension in Common . knonwConfigFileExtensions ) {
288
+ if ( filename . indexOf ( extension ) !== - 1 ) {
289
+ return Common . knonwConfigFileExtensions [ extension ] ;
290
+ }
291
+ }
292
+
284
293
return null ;
285
294
} ;
286
295
296
+ Common . getConfigFileCandidates = function ( name ) {
297
+ return Object . keys ( Common . knonwConfigFileExtensions ) . map ( ( extension ) => name + extension ) ;
298
+ }
299
+
287
300
/**
288
301
* Parses a config file like ecosystem.config.js. Supported formats: JS, JSON, JSON5, YAML.
289
302
* @param {string } confString contents of the config file
@@ -294,10 +307,12 @@ Common.parseConfig = function(confObj, filename) {
294
307
var yamljs = require ( 'yamljs' ) ;
295
308
var vm = require ( 'vm' ) ;
296
309
310
+ var isConfigFile = Common . isConfigFile ( filename ) ;
311
+
297
312
if ( ! filename ||
298
313
filename == 'pipe' ||
299
314
filename == 'none' ||
300
- filename . indexOf ( '. json') > - 1 ) {
315
+ isConfigFile == ' json') {
301
316
var code = '(' + confObj + ')' ;
302
317
var sandbox = { } ;
303
318
@@ -307,11 +322,10 @@ Common.parseConfig = function(confObj, filename) {
307
322
timeout : 1000
308
323
} ) ;
309
324
}
310
- else if ( filename . indexOf ( '.yml' ) > - 1 ||
311
- filename . indexOf ( '.yaml' ) > - 1 ) {
325
+ else if ( isConfigFile == 'yaml' ) {
312
326
return yamljs . parse ( confObj . toString ( ) ) ;
313
327
}
314
- else if ( filename . indexOf ( '.config. js') > - 1 || filename . indexOf ( '.config.cjs' ) > - 1 || filename . indexOf ( '.config. mjs') > - 1 ) {
328
+ else if ( isConfigFile == ' js' || isConfigFile == ' mjs') {
315
329
var confPath = require . resolve ( path . resolve ( filename ) ) ;
316
330
delete require . cache [ confPath ] ;
317
331
return require ( confPath ) ;
0 commit comments