diff --git a/core/events.js b/core/events.js index 73c9739b..79b41429 100644 --- a/core/events.js +++ b/core/events.js @@ -23,25 +23,28 @@ var self = module.exports = { eventEmitter.removeListener(eventName, listener); }, registerModules: function() { - var mods = fs.readdirSync(Config.config.paths.mods); + const moduleUtil = require('./module_util.js'); - mods.forEach(function(item) { - var modPath = Config.config.paths.mods+item; - if (item.substr(item.length-3) != '.js') { - modPath += path.sep+item+'.js'; - } - if (fs.existsSync(modPath)) { - var module = require(modPath); - - if (module.registerEvents !== undefined) { - logger.log.debug(modPath+" calling registerEvents function"); - module.registerEvents(); - } else { - logger.log.debug(modPath+" has no registerEvents function"); + moduleUtil.getModulePaths().forEach(function(modulePath) { + var mods = fs.readdirSync(modulePath); + mods.forEach(function(item) { + var modPath = modulePath+item; + if (item.substr(item.length-3) != '.js') { + modPath += path.sep+item+'.js'; } - } else { - logger.log.debug(modPath+" - file not found"); - } + if (fs.existsSync(modPath)) { + var module = require(modPath); + + if (module.registerEvents !== undefined) { + logger.log.debug(modPath+" calling registerEvents function"); + module.registerEvents(); + } else { + logger.log.debug(modPath+" has no registerEvents function"); + } + } else { + logger.log.debug(modPath+" - file not found"); + } + }); }); } } diff --git a/core/module_util.js b/core/module_util.js index 1cd4bed3..194efca5 100644 --- a/core/module_util.js +++ b/core/module_util.js @@ -15,6 +15,7 @@ const async = require('async'); exports.loadModuleEx = loadModuleEx; exports.loadModule = loadModule; exports.loadModulesForCategory = loadModulesForCategory; +exports.getModulePaths = getModulePaths; function loadModuleEx(options, cb) { assert(_.isObject(options)); @@ -25,7 +26,7 @@ function loadModuleEx(options, cb) { if(_.isObject(modConfig) && false === modConfig.enabled) { const err = new Error(`Module "${options.name}" is disabled`); - err.code = 'EENIGMODDISABLED'; + err.code = 'EENIGMODDISABLED'; return cb(err); } @@ -36,7 +37,7 @@ function loadModuleEx(options, cb) { // let mod; let modPath = paths.join(options.path, `${options.name}.js`); // general case first - try { + try { mod = require(modPath); } catch(e) { if('MODULE_NOT_FOUND' === e.code) { @@ -48,7 +49,7 @@ function loadModuleEx(options, cb) { } } else { return cb(e); - } + } } if(!_.isObject(mod.moduleInfo)) { @@ -75,7 +76,7 @@ function loadModule(name, category, cb) { } function loadModulesForCategory(category, iterator, complete) { - + fs.readdir(Config.paths[category], (err, files) => { if(err) { return iterator(err); @@ -97,3 +98,9 @@ function loadModulesForCategory(category, iterator, complete) { }); }); } + +function getModulePaths() { + return [ + Config.paths.mods + ]; +}