Add getModulePaths to module_util, call it from events.registerModules

This commit is contained in:
Josh M. McKee 2017-06-11 19:44:34 -07:00
parent 06e84eee94
commit b383950314
2 changed files with 31 additions and 21 deletions

View file

@ -23,25 +23,28 @@ var self = module.exports = {
eventEmitter.removeListener(eventName, listener); eventEmitter.removeListener(eventName, listener);
}, },
registerModules: function() { registerModules: function() {
var mods = fs.readdirSync(Config.config.paths.mods); const moduleUtil = require('./module_util.js');
mods.forEach(function(item) { moduleUtil.getModulePaths().forEach(function(modulePath) {
var modPath = Config.config.paths.mods+item; var mods = fs.readdirSync(modulePath);
if (item.substr(item.length-3) != '.js') { mods.forEach(function(item) {
modPath += path.sep+item+'.js'; var modPath = modulePath+item;
} if (item.substr(item.length-3) != '.js') {
if (fs.existsSync(modPath)) { modPath += path.sep+item+'.js';
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 { if (fs.existsSync(modPath)) {
logger.log.debug(modPath+" - file not found"); 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");
}
});
}); });
} }
} }

View file

@ -15,6 +15,7 @@ const async = require('async');
exports.loadModuleEx = loadModuleEx; exports.loadModuleEx = loadModuleEx;
exports.loadModule = loadModule; exports.loadModule = loadModule;
exports.loadModulesForCategory = loadModulesForCategory; exports.loadModulesForCategory = loadModulesForCategory;
exports.getModulePaths = getModulePaths;
function loadModuleEx(options, cb) { function loadModuleEx(options, cb) {
assert(_.isObject(options)); assert(_.isObject(options));
@ -25,7 +26,7 @@ function loadModuleEx(options, cb) {
if(_.isObject(modConfig) && false === modConfig.enabled) { if(_.isObject(modConfig) && false === modConfig.enabled) {
const err = new Error(`Module "${options.name}" is disabled`); const err = new Error(`Module "${options.name}" is disabled`);
err.code = 'EENIGMODDISABLED'; err.code = 'EENIGMODDISABLED';
return cb(err); return cb(err);
} }
@ -36,7 +37,7 @@ function loadModuleEx(options, cb) {
// //
let mod; let mod;
let modPath = paths.join(options.path, `${options.name}.js`); // general case first let modPath = paths.join(options.path, `${options.name}.js`); // general case first
try { try {
mod = require(modPath); mod = require(modPath);
} catch(e) { } catch(e) {
if('MODULE_NOT_FOUND' === e.code) { if('MODULE_NOT_FOUND' === e.code) {
@ -48,7 +49,7 @@ function loadModuleEx(options, cb) {
} }
} else { } else {
return cb(e); return cb(e);
} }
} }
if(!_.isObject(mod.moduleInfo)) { if(!_.isObject(mod.moduleInfo)) {
@ -75,7 +76,7 @@ function loadModule(name, category, cb) {
} }
function loadModulesForCategory(category, iterator, complete) { function loadModulesForCategory(category, iterator, complete) {
fs.readdir(Config.paths[category], (err, files) => { fs.readdir(Config.paths[category], (err, files) => {
if(err) { if(err) {
return iterator(err); return iterator(err);
@ -97,3 +98,9 @@ function loadModulesForCategory(category, iterator, complete) {
}); });
}); });
} }
function getModulePaths() {
return [
Config.paths.mods
];
}