Skip to content

Commit

Permalink
Merge pull request #5678 from karol-bujacek/bugfix/deploy-ecosystem-f…
Browse files Browse the repository at this point in the history
…ilename-extension

Bugfix/deploy ecosystem filename extension
  • Loading branch information
Unitech committed Oct 6, 2023
2 parents 7912239 + dd19e3f commit 0530799
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 15 deletions.
3 changes: 2 additions & 1 deletion lib/API/Deploy.js
Expand Up @@ -67,7 +67,8 @@ module.exports = function(CLI) {
// Find ecosystem file by default
if (!Common.isConfigFile(file)) {
env = args[0];
var defaultConfigNames = ['ecosystem.config.js', 'ecosystem.config.cjs', 'ecosystem.config.mjs', 'ecosystem.json', 'ecosystem.json5', 'package.json'];
var defaultConfigNames = [ ...Common.knonwConfigFileExtensions('ecosystem'), 'ecosystem.json5', 'package.json'];

file = Utility.whichFileExists(defaultConfigNames);

if (!file) {
Expand Down
42 changes: 28 additions & 14 deletions lib/Common.js
Expand Up @@ -263,6 +263,18 @@ Common.prepareAppConf = function(opts, app) {
return app;
};

/**
* Definition of known config file extensions with their type
*/
Common.knonwConfigFileExtensions = {
'.json': 'json',
'.yml': 'yaml',
'.yaml': 'yaml',
'.config.js': 'js',
'.config.cjs': 'js',
'.config.mjs': 'mjs'
}

/**
* Check if filename is a configuration file
* @param {string} filename
Expand All @@ -271,19 +283,20 @@ Common.prepareAppConf = function(opts, app) {
Common.isConfigFile = function (filename) {
if (typeof (filename) !== 'string')
return null;
if (filename.indexOf('.json') !== -1)
return 'json';
if (filename.indexOf('.yml') > -1 || filename.indexOf('.yaml') > -1)
return 'yaml';
if (filename.indexOf('.config.js') !== -1)
return 'js';
if (filename.indexOf('.config.cjs') !== -1)
return 'js';
if (filename.indexOf('.config.mjs') !== -1)
return 'mjs';

for (let extension in Common.knonwConfigFileExtensions) {
if (filename.indexOf(extension) !== -1) {
return Common.knonwConfigFileExtensions[extension];
}
}

return null;
};

Common.getConfigFileCandidates = function (name) {
return Object.keys(Common.knonwConfigFileExtensions).map((extension) => name + extension);
}

/**
* Parses a config file like ecosystem.config.js. Supported formats: JS, JSON, JSON5, YAML.
* @param {string} confString contents of the config file
Expand All @@ -294,10 +307,12 @@ Common.parseConfig = function(confObj, filename) {
var yamljs = require('yamljs');
var vm = require('vm');

var isConfigFile = Common.isConfigFile(filename);

if (!filename ||
filename == 'pipe' ||
filename == 'none' ||
filename.indexOf('.json') > -1) {
isConfigFile == 'json') {
var code = '(' + confObj + ')';
var sandbox = {};

Expand All @@ -307,11 +322,10 @@ Common.parseConfig = function(confObj, filename) {
timeout: 1000
});
}
else if (filename.indexOf('.yml') > -1 ||
filename.indexOf('.yaml') > -1) {
else if (isConfigFile == 'yaml') {
return yamljs.parse(confObj.toString());
}
else if (filename.indexOf('.config.js') > -1 || filename.indexOf('.config.cjs') > -1 || filename.indexOf('.config.mjs') > -1) {
else if (isConfigFile == 'js' || isConfigFile == 'mjs') {
var confPath = require.resolve(path.resolve(filename));
delete require.cache[confPath];
return require(confPath);
Expand Down
43 changes: 43 additions & 0 deletions test/programmatic/common.mocha.js
@@ -0,0 +1,43 @@
var Common = require('../../lib/Common');
var should = require('should');

process.chdir(__dirname);

describe('Common utilities', function () {
describe('Config file detection', function () {
var tests = [
{ arg: "ecosystem.json", expected: "json" },
{ arg: "ecosystem.yml", expected: "yaml" },
{ arg: "ecosystem.yaml", expected: "yaml" },
{ arg: "ecosystem.config.js", expected: "js" },
{ arg: "ecosystem.config.cjs", expected: "js" },
{ arg: "ecosystem.config.mjs", expected: "mjs" },
]

tests.forEach(function (test) {
it('should accept configuration file ' + test.arg , function () {
var result = Common.isConfigFile(test.arg);
should(result).eql(test.expected);
})
});

it('should not accept unknown filename', function () {
should(Common.isConfigFile('lorem-ipsum.js')).be.null();
})
})

describe('Config file candidates', function () {
it('should return an array with well-known file extensions', function () {
var result = Common.getConfigFileCandidates('ecosystem');
should(result).eql([
'ecosystem.json',
'ecosystem.yml',
'ecosystem.yaml',
'ecosystem.config.js',
'ecosystem.config.cjs',
'ecosystem.config.mjs'
]);
});
});

})
1 change: 1 addition & 0 deletions test/unit.sh
Expand Up @@ -82,6 +82,7 @@ runUnitTest $D/configuration.mocha.js
runUnitTest $D/id.mocha.js
runUnitTest $D/god.mocha.js
runUnitTest $D/dump.mocha.js
runUnitTest $D/common.mocha.js

runUnitTest $D/issues/json_env_passing_4080.mocha.js

Expand Down

0 comments on commit 0530799

Please sign in to comment.