Skip to content

Commit 0530799

Browse files
authoredOct 6, 2023
Merge pull request #5678 from karol-bujacek/bugfix/deploy-ecosystem-filename-extension
Bugfix/deploy ecosystem filename extension
2 parents 7912239 + dd19e3f commit 0530799

File tree

4 files changed

+74
-15
lines changed

4 files changed

+74
-15
lines changed
 

‎lib/API/Deploy.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ module.exports = function(CLI) {
6767
// Find ecosystem file by default
6868
if (!Common.isConfigFile(file)) {
6969
env = args[0];
70-
var defaultConfigNames = ['ecosystem.config.js', 'ecosystem.config.cjs', 'ecosystem.config.mjs', 'ecosystem.json', 'ecosystem.json5', 'package.json'];
70+
var defaultConfigNames = [ ...Common.knonwConfigFileExtensions('ecosystem'), 'ecosystem.json5', 'package.json'];
71+
7172
file = Utility.whichFileExists(defaultConfigNames);
7273

7374
if (!file) {

‎lib/Common.js

+28-14
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,18 @@ Common.prepareAppConf = function(opts, app) {
263263
return app;
264264
};
265265

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+
266278
/**
267279
* Check if filename is a configuration file
268280
* @param {string} filename
@@ -271,19 +283,20 @@ Common.prepareAppConf = function(opts, app) {
271283
Common.isConfigFile = function (filename) {
272284
if (typeof (filename) !== 'string')
273285
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+
284293
return null;
285294
};
286295

296+
Common.getConfigFileCandidates = function (name) {
297+
return Object.keys(Common.knonwConfigFileExtensions).map((extension) => name + extension);
298+
}
299+
287300
/**
288301
* Parses a config file like ecosystem.config.js. Supported formats: JS, JSON, JSON5, YAML.
289302
* @param {string} confString contents of the config file
@@ -294,10 +307,12 @@ Common.parseConfig = function(confObj, filename) {
294307
var yamljs = require('yamljs');
295308
var vm = require('vm');
296309

310+
var isConfigFile = Common.isConfigFile(filename);
311+
297312
if (!filename ||
298313
filename == 'pipe' ||
299314
filename == 'none' ||
300-
filename.indexOf('.json') > -1) {
315+
isConfigFile == 'json') {
301316
var code = '(' + confObj + ')';
302317
var sandbox = {};
303318

@@ -307,11 +322,10 @@ Common.parseConfig = function(confObj, filename) {
307322
timeout: 1000
308323
});
309324
}
310-
else if (filename.indexOf('.yml') > -1 ||
311-
filename.indexOf('.yaml') > -1) {
325+
else if (isConfigFile == 'yaml') {
312326
return yamljs.parse(confObj.toString());
313327
}
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') {
315329
var confPath = require.resolve(path.resolve(filename));
316330
delete require.cache[confPath];
317331
return require(confPath);

‎test/programmatic/common.mocha.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
var Common = require('../../lib/Common');
2+
var should = require('should');
3+
4+
process.chdir(__dirname);
5+
6+
describe('Common utilities', function () {
7+
describe('Config file detection', function () {
8+
var tests = [
9+
{ arg: "ecosystem.json", expected: "json" },
10+
{ arg: "ecosystem.yml", expected: "yaml" },
11+
{ arg: "ecosystem.yaml", expected: "yaml" },
12+
{ arg: "ecosystem.config.js", expected: "js" },
13+
{ arg: "ecosystem.config.cjs", expected: "js" },
14+
{ arg: "ecosystem.config.mjs", expected: "mjs" },
15+
]
16+
17+
tests.forEach(function (test) {
18+
it('should accept configuration file ' + test.arg , function () {
19+
var result = Common.isConfigFile(test.arg);
20+
should(result).eql(test.expected);
21+
})
22+
});
23+
24+
it('should not accept unknown filename', function () {
25+
should(Common.isConfigFile('lorem-ipsum.js')).be.null();
26+
})
27+
})
28+
29+
describe('Config file candidates', function () {
30+
it('should return an array with well-known file extensions', function () {
31+
var result = Common.getConfigFileCandidates('ecosystem');
32+
should(result).eql([
33+
'ecosystem.json',
34+
'ecosystem.yml',
35+
'ecosystem.yaml',
36+
'ecosystem.config.js',
37+
'ecosystem.config.cjs',
38+
'ecosystem.config.mjs'
39+
]);
40+
});
41+
});
42+
43+
})

‎test/unit.sh

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ runUnitTest $D/configuration.mocha.js
8282
runUnitTest $D/id.mocha.js
8383
runUnitTest $D/god.mocha.js
8484
runUnitTest $D/dump.mocha.js
85+
runUnitTest $D/common.mocha.js
8586

8687
runUnitTest $D/issues/json_env_passing_4080.mocha.js
8788

0 commit comments

Comments
 (0)
Please sign in to comment.