Skip to content

Commit

Permalink
Deploy: use common file extensions during config file detection
Browse files Browse the repository at this point in the history
This fixes #5451, relates to #5337, #4652
  • Loading branch information
karol-bujacek committed Sep 27, 2023
1 parent e0a5add commit c810b9f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/API/Deploy.js
Expand Up @@ -67,7 +67,7 @@ module.exports = function(CLI) {
// Find ecosystem file by default
if (!Common.isConfigFile(file)) {
env = args[0];
var defaultConfigNames = ['ecosystem.config.js', 'ecosystem.json', 'ecosystem.json5', 'package.json'];
var defaultConfigNames = [ ...Common.knonwConfigFileExtensions('ecosystem'), 'ecosystem.json5', 'package.json'];
file = Utility.whichFileExists(defaultConfigNames);

if (!file) {
Expand Down
29 changes: 18 additions & 11 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 @@ -272,24 +284,19 @@ Common.isConfigFile = function (filename) {
if (typeof (filename) !== 'string')
return null;

const knownExtensions = {
'.json': 'json',
'.yml': 'yaml',
'.yaml': 'yaml',
'.config.js': 'js',
'.config.cjs': 'js',
'.config.mjs': 'mjs'
}

for (let extension in knownExtensions) {
for (let extension in Common.knonwConfigFileExtensions) {
if (filename.indexOf(extension) !== -1) {
return knownExtensions[extension];
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 Down
15 changes: 15 additions & 0 deletions test/programmatic/common.mocha.js
Expand Up @@ -25,4 +25,19 @@ describe('Common utilities', 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'
]);
});
});

})

0 comments on commit c810b9f

Please sign in to comment.