2014-10-17 02:21:06 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2016-02-17 05:11:55 +00:00
|
|
|
// ENiGMA½
|
|
|
|
let Config = require('./config.js').config;
|
|
|
|
let miscUtil = require('./misc_util.js');
|
2015-04-21 04:50:58 +00:00
|
|
|
|
2016-02-17 05:11:55 +00:00
|
|
|
// standard/deps
|
|
|
|
let fs = require('fs');
|
|
|
|
let paths = require('path');
|
|
|
|
let _ = require('lodash');
|
|
|
|
let assert = require('assert');
|
|
|
|
let async = require('async');
|
2014-10-17 02:21:06 +00:00
|
|
|
|
|
|
|
// exports
|
2015-04-21 04:50:58 +00:00
|
|
|
exports.loadModuleEx = loadModuleEx;
|
2014-10-17 02:21:06 +00:00
|
|
|
exports.loadModule = loadModule;
|
|
|
|
exports.loadModulesForCategory = loadModulesForCategory;
|
|
|
|
|
2015-04-21 04:50:58 +00:00
|
|
|
function loadModuleEx(options, cb) {
|
|
|
|
assert(_.isObject(options));
|
|
|
|
assert(_.isString(options.name));
|
|
|
|
assert(_.isString(options.path));
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2016-02-03 04:35:59 +00:00
|
|
|
const modConfig = _.isObject(Config[options.category]) ? Config[options.category][options.name] : null;
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2015-04-21 04:50:58 +00:00
|
|
|
if(_.isObject(modConfig) && false === modConfig.enabled) {
|
|
|
|
cb(new Error('Module "' + options.name + '" is disabled'));
|
2014-10-17 02:21:06 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-02-03 04:35:59 +00:00
|
|
|
var mod;
|
2014-10-17 02:21:06 +00:00
|
|
|
try {
|
2016-02-03 04:35:59 +00:00
|
|
|
mod = require(paths.join(options.path, options.name + '.js'));
|
|
|
|
} catch(e) {
|
|
|
|
cb(e);
|
|
|
|
}
|
2015-03-19 05:08:23 +00:00
|
|
|
|
2016-02-03 04:35:59 +00:00
|
|
|
if(!_.isObject(mod.moduleInfo)) {
|
|
|
|
cb(new Error('Module is missing "moduleInfo" section'));
|
|
|
|
return;
|
|
|
|
}
|
2015-03-19 05:08:23 +00:00
|
|
|
|
2016-02-03 04:35:59 +00:00
|
|
|
if(!_.isFunction(mod.getModule)) {
|
|
|
|
cb(new Error('Invalid or missing "getModule" method for module!'));
|
|
|
|
return;
|
|
|
|
}
|
2015-03-19 05:08:23 +00:00
|
|
|
|
2016-02-17 05:11:55 +00:00
|
|
|
// Ref configuration, if any, for convience to the module
|
2016-02-03 04:35:59 +00:00
|
|
|
mod.runtime = { config : modConfig };
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2016-02-03 04:35:59 +00:00
|
|
|
cb(null, mod);
|
2015-03-19 05:08:23 +00:00
|
|
|
}
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2015-04-21 04:50:58 +00:00
|
|
|
function loadModule(name, category, cb) {
|
|
|
|
var path = Config.paths[category];
|
|
|
|
|
|
|
|
if(!_.isString(path)) {
|
|
|
|
cb(new Error('Not sure where to look for "' + name + '" of category "' + category + '"'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
loadModuleEx( { name : name, path : path, category : category }, function loaded(err, mod) {
|
|
|
|
cb(err, mod);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-02-17 05:11:55 +00:00
|
|
|
function loadModulesForCategory(category, iterator, complete) {
|
|
|
|
|
|
|
|
fs.readdir(Config.paths[category], (err, files) => {
|
2014-10-17 02:21:06 +00:00
|
|
|
if(err) {
|
2016-02-17 05:11:55 +00:00
|
|
|
iterator(err);
|
2014-10-17 02:21:06 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-02-17 05:11:55 +00:00
|
|
|
const jsModules = files.filter(file => {
|
|
|
|
return '.js' === paths.extname(file);
|
|
|
|
});
|
|
|
|
|
|
|
|
async.each(jsModules, (file, next) => {
|
|
|
|
loadModule(paths.basename(file, '.js'), category, (err, mod) => {
|
|
|
|
iterator(err, mod);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
}, err => {
|
|
|
|
if(complete) {
|
|
|
|
complete(err);
|
|
|
|
}
|
2014-10-17 02:21:06 +00:00
|
|
|
});
|
|
|
|
});
|
2015-03-19 05:08:23 +00:00
|
|
|
}
|