First pass formatting with Prettier
* Added .prettierrc.json * Added .prettierignore * Formatted
This commit is contained in:
@@ -2,37 +2,41 @@
|
||||
'use strict';
|
||||
|
||||
// ENiGMA½
|
||||
const Config = require('./config.js').get;
|
||||
const Log = require('./logger.js').log;
|
||||
const {
|
||||
Errors,
|
||||
ErrorReasons
|
||||
} = require('./enig_error.js');
|
||||
const Config = require('./config.js').get;
|
||||
const Log = require('./logger.js').log;
|
||||
const { Errors, ErrorReasons } = require('./enig_error.js');
|
||||
|
||||
// deps
|
||||
const fs = require('graceful-fs');
|
||||
const paths = require('path');
|
||||
const _ = require('lodash');
|
||||
const assert = require('assert');
|
||||
const async = require('async');
|
||||
const glob = require('glob');
|
||||
const fs = require('graceful-fs');
|
||||
const paths = require('path');
|
||||
const _ = require('lodash');
|
||||
const assert = require('assert');
|
||||
const async = require('async');
|
||||
const glob = require('glob');
|
||||
|
||||
// exports
|
||||
exports.loadModuleEx = loadModuleEx;
|
||||
exports.loadModule = loadModule;
|
||||
exports.loadModulesForCategory = loadModulesForCategory;
|
||||
exports.getModulePaths = getModulePaths;
|
||||
exports.initializeModules = initializeModules;
|
||||
exports.loadModuleEx = loadModuleEx;
|
||||
exports.loadModule = loadModule;
|
||||
exports.loadModulesForCategory = loadModulesForCategory;
|
||||
exports.getModulePaths = getModulePaths;
|
||||
exports.initializeModules = initializeModules;
|
||||
|
||||
function loadModuleEx(options, cb) {
|
||||
assert(_.isObject(options));
|
||||
assert(_.isString(options.name));
|
||||
assert(_.isString(options.path));
|
||||
|
||||
const modConfig = _.isObject(Config[options.category]) ? Config[options.category][options.name] : null;
|
||||
const modConfig = _.isObject(Config[options.category])
|
||||
? Config[options.category][options.name]
|
||||
: null;
|
||||
|
||||
if(_.isObject(modConfig) && false === modConfig.enabled) {
|
||||
return cb(Errors.AccessDenied(`Module "${options.name}" is disabled`, ErrorReasons.Disabled));
|
||||
if (_.isObject(modConfig) && false === modConfig.enabled) {
|
||||
return cb(
|
||||
Errors.AccessDenied(
|
||||
`Module "${options.name}" is disabled`,
|
||||
ErrorReasons.Disabled
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
//
|
||||
@@ -41,15 +45,15 @@ function loadModuleEx(options, cb) {
|
||||
// to have their own containing folder, package.json & dependencies, etc.
|
||||
//
|
||||
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 {
|
||||
mod = require(modPath);
|
||||
} catch(e) {
|
||||
if('MODULE_NOT_FOUND' === e.code) {
|
||||
} catch (e) {
|
||||
if ('MODULE_NOT_FOUND' === e.code) {
|
||||
modPath = paths.join(options.path, options.name, `${options.name}.js`);
|
||||
try {
|
||||
mod = require(modPath);
|
||||
} catch(e) {
|
||||
} catch (e) {
|
||||
return cb(e);
|
||||
}
|
||||
} else {
|
||||
@@ -57,12 +61,16 @@ function loadModuleEx(options, cb) {
|
||||
}
|
||||
}
|
||||
|
||||
if(!_.isObject(mod.moduleInfo)) {
|
||||
return cb(Errors.Invalid(`No exported "moduleInfo" block for module ${modPath}!`));
|
||||
if (!_.isObject(mod.moduleInfo)) {
|
||||
return cb(
|
||||
Errors.Invalid(`No exported "moduleInfo" block for module ${modPath}!`)
|
||||
);
|
||||
}
|
||||
|
||||
if(!_.isFunction(mod.getModule)) {
|
||||
return cb(Errors.Invalid(`No exported "getModule" method for module ${modPath}!`));
|
||||
if (!_.isFunction(mod.getModule)) {
|
||||
return cb(
|
||||
Errors.Invalid(`No exported "getModule" method for module ${modPath}!`)
|
||||
);
|
||||
}
|
||||
|
||||
return cb(null, mod);
|
||||
@@ -71,19 +79,25 @@ function loadModuleEx(options, cb) {
|
||||
function loadModule(name, category, cb) {
|
||||
const path = Config().paths[category];
|
||||
|
||||
if(!_.isString(path)) {
|
||||
return cb(Errors.DoesNotExist(`Not sure where to look for module "${name}" of category "${category}"`));
|
||||
if (!_.isString(path)) {
|
||||
return cb(
|
||||
Errors.DoesNotExist(
|
||||
`Not sure where to look for module "${name}" of category "${category}"`
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
loadModuleEx( { name : name, path : path, category : category }, function loaded(err, mod) {
|
||||
return cb(err, mod);
|
||||
});
|
||||
loadModuleEx(
|
||||
{ name: name, path: path, category: category },
|
||||
function loaded(err, mod) {
|
||||
return cb(err, mod);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
function loadModulesForCategory(category, iterator, complete) {
|
||||
|
||||
fs.readdir(Config().paths[category], (err, files) => {
|
||||
if(err) {
|
||||
if (err) {
|
||||
return iterator(err);
|
||||
}
|
||||
|
||||
@@ -91,23 +105,27 @@ function loadModulesForCategory(category, iterator, complete) {
|
||||
return '.js' === paths.extname(file);
|
||||
});
|
||||
|
||||
async.each(jsModules, (file, next) => {
|
||||
loadModule(paths.basename(file, '.js'), category, (err, mod) => {
|
||||
if(err) {
|
||||
if(ErrorReasons.Disabled === err.reasonCode) {
|
||||
Log.debug(err.message);
|
||||
} else {
|
||||
Log.info( { err : err }, 'Failed loading module');
|
||||
async.each(
|
||||
jsModules,
|
||||
(file, next) => {
|
||||
loadModule(paths.basename(file, '.js'), category, (err, mod) => {
|
||||
if (err) {
|
||||
if (ErrorReasons.Disabled === err.reasonCode) {
|
||||
Log.debug(err.message);
|
||||
} else {
|
||||
Log.info({ err: err }, 'Failed loading module');
|
||||
}
|
||||
return next(null); // continue no matter what
|
||||
}
|
||||
return next(null); // continue no matter what
|
||||
return iterator(mod, next);
|
||||
});
|
||||
},
|
||||
err => {
|
||||
if (complete) {
|
||||
return complete(err);
|
||||
}
|
||||
return iterator(mod, next);
|
||||
});
|
||||
}, err => {
|
||||
if(complete) {
|
||||
return complete(err);
|
||||
}
|
||||
});
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -127,48 +145,63 @@ function initializeModules(cb) {
|
||||
|
||||
const modulePaths = getModulePaths().concat(__dirname);
|
||||
|
||||
async.each(modulePaths, (modulePath, nextPath) => {
|
||||
glob('*{.js,/*.js}', { cwd : modulePath }, (err, files) => {
|
||||
if(err) {
|
||||
return nextPath(err);
|
||||
}
|
||||
|
||||
const ourPath = paths.join(__dirname, __filename);
|
||||
|
||||
async.each(files, (moduleName, nextModule) => {
|
||||
const fullModulePath = paths.join(modulePath, moduleName);
|
||||
if(ourPath === fullModulePath) {
|
||||
return nextModule(null);
|
||||
async.each(
|
||||
modulePaths,
|
||||
(modulePath, nextPath) => {
|
||||
glob('*{.js,/*.js}', { cwd: modulePath }, (err, files) => {
|
||||
if (err) {
|
||||
return nextPath(err);
|
||||
}
|
||||
|
||||
try {
|
||||
const mod = require(fullModulePath);
|
||||
const ourPath = paths.join(__dirname, __filename);
|
||||
|
||||
if(_.isFunction(mod.moduleInitialize)) {
|
||||
const initInfo = {
|
||||
events : Events,
|
||||
};
|
||||
|
||||
mod.moduleInitialize(initInfo, err => {
|
||||
if(err) {
|
||||
Log.warn( { error : err.message, modulePath : fullModulePath }, 'Error during "moduleInitialize"');
|
||||
}
|
||||
async.each(
|
||||
files,
|
||||
(moduleName, nextModule) => {
|
||||
const fullModulePath = paths.join(modulePath, moduleName);
|
||||
if (ourPath === fullModulePath) {
|
||||
return nextModule(null);
|
||||
});
|
||||
} else {
|
||||
return nextModule(null);
|
||||
}
|
||||
|
||||
try {
|
||||
const mod = require(fullModulePath);
|
||||
|
||||
if (_.isFunction(mod.moduleInitialize)) {
|
||||
const initInfo = {
|
||||
events: Events,
|
||||
};
|
||||
|
||||
mod.moduleInitialize(initInfo, err => {
|
||||
if (err) {
|
||||
Log.warn(
|
||||
{
|
||||
error: err.message,
|
||||
modulePath: fullModulePath,
|
||||
},
|
||||
'Error during "moduleInitialize"'
|
||||
);
|
||||
}
|
||||
return nextModule(null);
|
||||
});
|
||||
} else {
|
||||
return nextModule(null);
|
||||
}
|
||||
} catch (e) {
|
||||
Log.warn(
|
||||
{ error: e.message, fullModulePath },
|
||||
'Exception during "moduleInitialize"'
|
||||
);
|
||||
return nextModule(null);
|
||||
}
|
||||
},
|
||||
err => {
|
||||
return nextPath(err);
|
||||
}
|
||||
} catch(e) {
|
||||
Log.warn( { error : e.message, fullModulePath }, 'Exception during "moduleInitialize"');
|
||||
return nextModule(null);
|
||||
}
|
||||
},
|
||||
err => {
|
||||
return nextPath(err);
|
||||
);
|
||||
});
|
||||
});
|
||||
},
|
||||
err => {
|
||||
return cb(err);
|
||||
});
|
||||
},
|
||||
err => {
|
||||
return cb(err);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user