2014-10-17 02:21:06 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2016-02-17 05:11:55 +00:00
|
|
|
// ENiGMA½
|
2016-04-13 04:38:32 +00:00
|
|
|
const Config = require('./config.js').config;
|
2015-04-21 04:50:58 +00:00
|
|
|
|
2016-04-13 04:38:32 +00:00
|
|
|
// deps
|
2017-05-20 03:20:19 +00:00
|
|
|
const fs = require('graceful-fs');
|
2016-04-13 04:38:32 +00:00
|
|
|
const paths = require('path');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const assert = require('assert');
|
|
|
|
const 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) {
|
2016-07-16 19:05:32 +00:00
|
|
|
const err = new Error(`Module "${options.name}" is disabled`);
|
|
|
|
err.code = 'EENIGMODDISABLED';
|
|
|
|
return cb(err);
|
2014-10-17 02:21:06 +00:00
|
|
|
}
|
|
|
|
|
2016-07-03 16:58:57 +00:00
|
|
|
//
|
|
|
|
// Modules are allowed to live in /path/to/<moduleName>/<moduleName>.js or
|
|
|
|
// simply in /path/to/<moduleName>.js. This allows for more advanced modules
|
|
|
|
// to have their own containing folder, package.json & dependencies, etc.
|
|
|
|
//
|
2016-04-13 04:38:32 +00:00
|
|
|
let mod;
|
2016-07-03 16:58:57 +00:00
|
|
|
let modPath = paths.join(options.path, `${options.name}.js`); // general case first
|
2016-04-13 04:38:32 +00:00
|
|
|
try {
|
2016-07-03 16:58:57 +00:00
|
|
|
mod = require(modPath);
|
2016-02-03 04:35:59 +00:00
|
|
|
} catch(e) {
|
2016-07-03 16:58:57 +00:00
|
|
|
if('MODULE_NOT_FOUND' === e.code) {
|
|
|
|
modPath = paths.join(options.path, options.name, `${options.name}.js`);
|
|
|
|
try {
|
|
|
|
mod = require(modPath);
|
|
|
|
} catch(e) {
|
|
|
|
return cb(e);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return cb(e);
|
|
|
|
}
|
2016-02-03 04:35:59 +00:00
|
|
|
}
|
2015-03-19 05:08:23 +00:00
|
|
|
|
2016-02-03 04:35:59 +00:00
|
|
|
if(!_.isObject(mod.moduleInfo)) {
|
2016-04-13 04:38:32 +00:00
|
|
|
return cb(new Error('Module is missing "moduleInfo" section'));
|
2016-02-03 04:35:59 +00:00
|
|
|
}
|
2015-03-19 05:08:23 +00:00
|
|
|
|
2016-02-03 04:35:59 +00:00
|
|
|
if(!_.isFunction(mod.getModule)) {
|
2016-04-13 04:38:32 +00:00
|
|
|
return cb(new Error('Invalid or missing "getModule" method for module!'));
|
2016-02-03 04:35:59 +00:00
|
|
|
}
|
2015-03-19 05:08:23 +00:00
|
|
|
|
2016-07-03 16:58:57 +00:00
|
|
|
return 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) {
|
2016-04-13 04:38:32 +00:00
|
|
|
const path = Config.paths[category];
|
2015-04-21 04:50:58 +00:00
|
|
|
|
|
|
|
if(!_.isString(path)) {
|
2016-04-13 04:38:32 +00:00
|
|
|
return cb(new Error(`Not sure where to look for "${name}" of category "${category}"`));
|
2015-04-21 04:50:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
loadModuleEx( { name : name, path : path, category : category }, function loaded(err, mod) {
|
2016-07-03 16:58:57 +00:00
|
|
|
return cb(err, mod);
|
2015-04-21 04:50:58 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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-03-01 05:32:51 +00:00
|
|
|
return iterator(err);
|
2014-10-17 02:21:06 +00:00
|
|
|
}
|
|
|
|
|
2016-02-17 05:11:55 +00:00
|
|
|
const jsModules = files.filter(file => {
|
|
|
|
return '.js' === paths.extname(file);
|
|
|
|
});
|
|
|
|
|
|
|
|
async.each(jsModules, (file, next) => {
|
2017-01-28 19:33:06 +00:00
|
|
|
loadModule(paths.basename(file, '.js'), category, (err, mod) => {
|
2016-02-17 05:11:55 +00:00
|
|
|
iterator(err, mod);
|
2016-07-03 16:58:57 +00:00
|
|
|
return next();
|
2016-02-17 05:11:55 +00:00
|
|
|
});
|
|
|
|
}, err => {
|
|
|
|
if(complete) {
|
2016-07-03 16:58:57 +00:00
|
|
|
return complete(err);
|
2016-02-17 05:11:55 +00:00
|
|
|
}
|
2014-10-17 02:21:06 +00:00
|
|
|
});
|
|
|
|
});
|
2015-03-19 05:08:23 +00:00
|
|
|
}
|