From 828a30d0ccc88b3f6e2b66d517ccf5f2394bd08b Mon Sep 17 00:00:00 2001 From: vince Date: Thu, 8 Feb 2018 15:57:58 +0100 Subject: [PATCH] feat: add dependencies section into ecosystem.json file. --- bin/pm2 | 2 +- constants.js | 1 + lib/API/Modules/Modularizer.js | 83 ++++++++++++++++++++++++---------- test/bash/interpreter.sh | 2 +- 4 files changed, 62 insertions(+), 26 deletions(-) diff --git a/bin/pm2 b/bin/pm2 index e43b1cc64..2202a7650 100755 --- a/bin/pm2 +++ b/bin/pm2 @@ -464,7 +464,7 @@ commander.command('update') /** * Module specifics */ -commander.command('install ') +commander.command('install [module|git:// url]') .alias('module:install') .option('--v1', 'install module in v1 manner (do not use it)') .option('--safe [time]', 'keep module backup, if new module fail = restore with previous') diff --git a/constants.js b/constants.js index b569670bc..c86f87720 100644 --- a/constants.js +++ b/constants.js @@ -28,6 +28,7 @@ var csts = { TEMPLATE_FOLDER : p.join(__dirname, 'lib/templates'), + APP_CONF_DEFAULT_FILE : 'ecosystem.json', APP_CONF_TPL : 'ecosystem.tpl', APP_CONF_TPL_SIMPLE : 'ecosystem-simple.tpl', SAMPLE_CONF_FILE : 'sample-conf.js', diff --git a/lib/API/Modules/Modularizer.js b/lib/API/Modules/Modularizer.js index 6a233329e..5b792dab4 100644 --- a/lib/API/Modules/Modularizer.js +++ b/lib/API/Modules/Modularizer.js @@ -46,6 +46,32 @@ var KNOWN_MODULES = { * - Generate sample module via pm2 module:generate */ Modularizer.install = function (CLI, moduleName, opts, cb) { + // if user want to install module from ecosystem.json file + // it can also be a custom json file's name + if (!moduleName || moduleName.length === 0 || moduleName.indexOf('.json') > 0) { + var file = moduleName || cst.APP_CONF_DEFAULT_FILE; + var isAbsolute = require('../../tools/IsAbsolute.js')(file); + var filePath = isAbsolute ? file : path.join(CLI.cwd, file); + + try { + var data = fs.readFileSync(filePath); + } catch (e) { + Common.printError(cst.PREFIX_MSG_ERR + 'File ' + file + ' not found'); + return cb(Common.retErr(e)); + } + + try { + var config = Common.parseConfig(data, file); + } catch (e) { + Common.printError(cst.PREFIX_MSG_ERR + 'File ' + file + ' malformated'); + console.error(e); + return cb(Common.retErr(e)); + } + + Modularizer.installMultipleModules(config.dependencies, cb); + return; + } + Common.printOut(cst.PREFIX_MSG_MOD + 'Installing module ' + moduleName); var canonicModuleName = Utility.getCanonicModuleName(moduleName); @@ -54,30 +80,7 @@ Modularizer.install = function (CLI, moduleName, opts, cb) { var currentModule = KNOWN_MODULES[moduleName]; if (currentModule && currentModule.hasOwnProperty('dependencies')) { - var functionList = []; - for (var i = 0; i < currentModule.dependencies.length; i++) { - functionList.push((function (index) { - return function (callback) { - installModuleByName(currentModule.dependencies[index], function (err) { - callback(null, {module: currentModule.dependencies[index], err: err}); - }, false); - }; - })(i)); - } - - async.parallel(functionList, function (err, results) { - for (var i = 0; i < results.length; i++) { - var display = results[i].module.message || results[i].module.name; - if (results[i].err) { - err = results[i].err; - Common.printError(cst.PREFIX_MSG_MOD_ERR + chalk.bold.green(display + ' installation has FAILED (checkout previous logs)')); - } else { - Common.printOut(cst.PREFIX_MSG + chalk.bold.green(display + ' ENABLED')); - } - } - - cb(err); - }); + Modularizer.installMultipleModules(currentModule.dependencies, cb); } else { installModuleByName(currentModule, cb); } @@ -106,6 +109,38 @@ Modularizer.install = function (CLI, moduleName, opts, cb) { }); }; +Modularizer.installMultipleModules = function (modules, cb) { + var functionList = []; + for (var i = 0; i < modules.length; i++) { + functionList.push((function (index) { + return function (callback) { + var module = modules[index]; + if (typeof modules[index] === 'string') { + module = {name: modules[index]}; + } + + installModuleByName(module, function (err) { + callback(null, {module: module, err: err}); + }, false); + }; + })(i)); + } + + async.parallel(functionList, function (err, results) { + for (var i = 0; i < results.length; i++) { + var display = results[i].module.message || results[i].module.name; + if (results[i].err) { + err = results[i].err; + Common.printError(cst.PREFIX_MSG_MOD_ERR + chalk.bold.green(display + ' installation has FAILED (checkout previous logs)')); + } else { + Common.printOut(cst.PREFIX_MSG + chalk.bold.green(display + ' ENABLED')); + } + } + + cb(err); + }); +}; + Modularizer.installModule = function(CLI, module_name, opts, cb) { var proc_path = '', cmd = '', diff --git a/test/bash/interpreter.sh b/test/bash/interpreter.sh index e497c8146..3257e85c0 100644 --- a/test/bash/interpreter.sh +++ b/test/bash/interpreter.sh @@ -77,7 +77,7 @@ spec "Should work on Livescript files in cluster mode" $pm2 delete all $pm2 start echo.ts sleep 1 -should 'process should be errored without coffee installed' "status: 'errored'" 1 +should 'process should be errored without typescript installed' "status: 'errored'" 1 ########### Install