enigma-bbs/core/module_util.js

71 lines
1.5 KiB
JavaScript
Raw Normal View History

2014-10-17 02:21:06 +00:00
/* jslint node: true */
'use strict';
var fs = require('fs');
var paths = require('path');
2014-10-17 02:21:06 +00:00
var conf = require('./config.js');
var miscUtil = require('./misc_util.js');
var _ = require('lodash');
2014-10-17 02:21:06 +00:00
// exports
exports.loadModule = loadModule;
exports.loadModulesForCategory = loadModulesForCategory;
function loadModule(name, category, cb) {
var config = conf.config;
var path = config.paths[category];
if(!path) {
cb(new Error('not sure where to look for "' + name + '" of category "' + category + '"'));
return;
}
// update conf to point at this module's section, if any
config = config[category] ? config[category][name] : null;
if(config && false === config.enabled) {
cb(new Error('module "' + name + '" is disabled'));
return;
}
try {
var mod = require(paths.join(path, name + '.js'));
2014-10-17 02:21:06 +00:00
if(!mod.moduleInfo) {
cb(new Error('module is missing \'moduleInfo\' section'));
return;
}
if(!_.isFunction(mod.getModule)) {
cb(new Error('Invalid or missing missing \'getModule\' method'));
return;
}
2014-10-17 02:21:06 +00:00
mod.runtime = {
config : config
};
cb(null, mod);
} catch(e) {
cb(e);
}
}
2014-10-17 02:21:06 +00:00
function loadModulesForCategory(category, cb) {
var path = conf.config.paths[category];
fs.readdir(path, function onFiles(err, files) {
if(err) {
cb(err);
return;
}
var filtered = files.filter(function onFilter(file) { return '.js' === paths.extname(file); });
filtered.forEach(function onFile(file) {
var modName = paths.basename(file, '.js');
loadModule(paths.basename(file, '.js'), category, cb);
});
});
}