Merge pull request #5678 from karol-bujacek/bugfix/deploy-ecosystem-filename-extension

Bugfix/deploy ecosystem filename extension
This commit is contained in:
Alexandre Strzelewicz 2023-10-06 06:28:12 +02:00 committed by GitHub
commit 0530799d72
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 74 additions and 15 deletions

View File

@ -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) {

View File

@ -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
@ -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
@ -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 = {};
@ -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);

View File

@ -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'
]);
});
});
})

View File

@ -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