mirror of
https://github.com/Unitech/pm2.git
synced 2026-02-01 16:57:09 +00:00
Merge pull request #5678 from karol-bujacek/bugfix/deploy-ecosystem-filename-extension
Bugfix/deploy ecosystem filename extension
This commit is contained in:
commit
0530799d72
@ -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) {
|
||||
|
||||
@ -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);
|
||||
|
||||
43
test/programmatic/common.mocha.js
Normal file
43
test/programmatic/common.mocha.js
Normal 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'
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
})
|
||||
@ -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
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user