Skip to content

Commit

Permalink
refactor: delete all "if" condition when installing new module, creat…
Browse files Browse the repository at this point in the history
…e an object with all modules and a generic installation process
  • Loading branch information
wallet77 committed Feb 8, 2018
1 parent 0d2b717 commit 1b92a9c
Showing 1 changed file with 17 additions and 46 deletions.
63 changes: 17 additions & 46 deletions lib/API/Modules/Modularizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,15 @@ var MODULE_CONF_PREFIX = 'module-db-v2';

var KNOWN_MODULES = {
'deep-monitoring': {
'dependencies': ['v8-profiler-node8', 'gc-stats', 'event-loop-inspector']
dependencies: [{name: 'v8-profiler-node8'}, {name: 'gc-stats'}, {name: 'event-loop-inspector'}]
},
'gc-stats': {name: 'gc-stats'},
'event-loop-inspector': {name: 'event-loop-inspector'}
'event-loop-inspector': {name: 'event-loop-inspector'},
'v8-profiler': {name: 'v8-profiler-node8'},
'profiler': {name: 'v8-profiler-node8'},
'typescript': {dependencies: [{name: 'typescript'}, {name: 'ts-node@latest'}]},
'coffee-script': {name: 'coffee-script', message: 'Coffeescript v1 support'},
'coffeescript': {name: 'coffeescript', message: 'Coffeescript v2 support'}
};

/**
Expand Down Expand Up @@ -62,59 +67,24 @@ Modularizer.install = function (CLI, moduleName, opts, cb) {

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(results[i].module + ' installation has FAILED (checkout previous logs)'));
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(results[i].module + ' ENABLED'));
Common.printOut(cst.PREFIX_MSG + chalk.bold.green(display + ' ENABLED'));
}
}

cb(err);
});
} else {
installModuleByName(currentModule.name, cb);
installModuleByName(currentModule, cb);
}

return false;
}

if (moduleName === 'v8-profiler' || moduleName === 'profiler') {
installModuleByName('v8-profiler-node8', cb);
return false;
}

if (moduleName.indexOf('typescript') > -1) {
// Special dependency install
return installLangModule(moduleName, function (e) {
installLangModule('ts-node@latest', function (e) {
Common.printOut(cst.PREFIX_MSG + chalk.bold.green('Typescript support enabled'));
return cb(e);
});
});
}

if (moduleName === 'livescript') {
return installLangModule('livescript', function (e) {
Common.printOut(cst.PREFIX_MSG + chalk.bold.green('Livescript support enabled'));
return cb(e);
});
}

if (moduleName.indexOf('coffee-script') > -1) {
return installLangModule(moduleName, function (e) {
Common.printOut(cst.PREFIX_MSG + chalk.bold.green('Coffeescript v1 support enabled'));
return cb(e);
});
}

if (moduleName.indexOf('coffeescript') > -1) {
return installLangModule(moduleName, function (e) {
Common.printOut(cst.PREFIX_MSG + chalk.bold.green('Coffeescript v2 support enabled'));
return cb(e);
});
}

moduleExist(CLI, canonicModuleName, function (exists) {
if (exists) {
// Update
Expand Down Expand Up @@ -577,22 +547,23 @@ Modularizer.generateSample = function(app_name, cb) {
});
};

function installModuleByName (moduleName, cb, verbose) {
if (!moduleName || moduleName.length === 0) {
function installModuleByName (module, cb, verbose) {
if (!module || !module.name || module.name.length === 0) {
return cb(new Error('No module name !'));
}

if (typeof verbose === 'undefined') {
verbose = true;
}

installLangModule(moduleName, function (err) {
installLangModule(module.name, function (err) {
var display = module.message || module.name;
if (err) {
if (verbose) { Common.printError(cst.PREFIX_MSG_MOD_ERR + chalk.bold.green(moduleName + ' installation has FAILED (checkout previous logs)')); }
if (verbose) { Common.printError(cst.PREFIX_MSG_MOD_ERR + chalk.bold.green(display + ' installation has FAILED (checkout previous logs)')); }
return cb(err);
}

if (verbose) { Common.printOut(cst.PREFIX_MSG + chalk.bold.green(moduleName + ' ENABLED')); }
if (verbose) { Common.printOut(cst.PREFIX_MSG + chalk.bold.green(display + ' ENABLED')); }
return cb();
});
}
Expand Down

0 comments on commit 1b92a9c

Please sign in to comment.