From c810b9fa3b898c8dcfd5fa13b079e21a2afa77d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karol=20Buja=C4=8Dek?= Date: Wed, 27 Sep 2023 13:01:33 +0200 Subject: [PATCH] Deploy: use common file extensions during config file detection This fixes #5451, relates to #5337, #4652 --- lib/API/Deploy.js | 2 +- lib/Common.js | 29 ++++++++++++++++++----------- test/programmatic/common.mocha.js | 15 +++++++++++++++ 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/lib/API/Deploy.js b/lib/API/Deploy.js index 4c0de6db7..b43552d5d 100644 --- a/lib/API/Deploy.js +++ b/lib/API/Deploy.js @@ -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) { diff --git a/lib/Common.js b/lib/Common.js index 701df2be0..4426d98c7 100644 --- a/lib/Common.js +++ b/lib/Common.js @@ -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 @@ -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 diff --git a/test/programmatic/common.mocha.js b/test/programmatic/common.mocha.js index 4dd956bc4..bec2efadd 100644 --- a/test/programmatic/common.mocha.js +++ b/test/programmatic/common.mocha.js @@ -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' + ]); + }); + }); + })