From e0a5add354419907adc796969448af71578406df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karol=20Buja=C4=8Dek?= Date: Wed, 27 Sep 2023 12:37:19 +0200 Subject: [PATCH 1/3] Refactor conditions in config file detection --- lib/Common.js | 26 ++++++++++++++++---------- test/programmatic/common.mocha.js | 28 ++++++++++++++++++++++++++++ test/unit.sh | 1 + 3 files changed, 45 insertions(+), 10 deletions(-) create mode 100644 test/programmatic/common.mocha.js diff --git a/lib/Common.js b/lib/Common.js index 6945f568c..701df2be0 100644 --- a/lib/Common.js +++ b/lib/Common.js @@ -271,16 +271,22 @@ 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'; + + const knownExtensions = { + '.json': 'json', + '.yml': 'yaml', + '.yaml': 'yaml', + '.config.js': 'js', + '.config.cjs': 'js', + '.config.mjs': 'mjs' + } + + for (let extension in knownExtensions) { + if (filename.indexOf(extension) !== -1) { + return knownExtensions[extension]; + } + } + return null; }; diff --git a/test/programmatic/common.mocha.js b/test/programmatic/common.mocha.js new file mode 100644 index 000000000..4dd956bc4 --- /dev/null +++ b/test/programmatic/common.mocha.js @@ -0,0 +1,28 @@ +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(); + }) + }) +}) diff --git a/test/unit.sh b/test/unit.sh index 57d993fbc..e363644f6 100644 --- a/test/unit.sh +++ b/test/unit.sh @@ -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 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 2/3] 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' + ]); + }); + }); + }) From 79687f1b26d3ce6288aadf4bdacdb2afcdd2468c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karol=20Buja=C4=8Dek?= Date: Wed, 27 Sep 2023 13:06:55 +0200 Subject: [PATCH 3/3] Refactor config file type detection in parse config function --- lib/Common.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/Common.js b/lib/Common.js index 4426d98c7..dd38db570 100644 --- a/lib/Common.js +++ b/lib/Common.js @@ -307,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 = {}; @@ -320,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);