Skip to content

Commit

Permalink
feat: add dependencies section into ecosystem.json file.
Browse files Browse the repository at this point in the history
  • Loading branch information
wallet77 committed Feb 8, 2018
1 parent 1b92a9c commit 828a30d
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 26 deletions.
2 changes: 1 addition & 1 deletion bin/pm2
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ commander.command('update')
/**
* Module specifics
*/
commander.command('install <module|git:// url>')
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')
Expand Down
1 change: 1 addition & 0 deletions constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
83 changes: 59 additions & 24 deletions lib/API/Modules/Modularizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,32 @@ var KNOWN_MODULES = {
* - Generate sample module via pm2 module:generate <module_name>
*/
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);
Expand All @@ -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);
}
Expand Down Expand Up @@ -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 = '',
Expand Down
2 changes: 1 addition & 1 deletion test/bash/interpreter.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 828a30d

Please sign in to comment.