First pass formatting with Prettier
* Added .prettierrc.json * Added .prettierignore * Formatted
This commit is contained in:
@@ -2,29 +2,29 @@
|
||||
'use strict';
|
||||
|
||||
// ENiGMA½
|
||||
const moduleUtil = require('./module_util.js');
|
||||
const Log = require('./logger.js').log;
|
||||
const Config = require('./config.js').get;
|
||||
const asset = require('./asset.js');
|
||||
const { MCIViewFactory } = require('./mci_view_factory.js');
|
||||
const { Errors } = require('./enig_error.js');
|
||||
const moduleUtil = require('./module_util.js');
|
||||
const Log = require('./logger.js').log;
|
||||
const Config = require('./config.js').get;
|
||||
const asset = require('./asset.js');
|
||||
const { MCIViewFactory } = require('./mci_view_factory.js');
|
||||
const { Errors } = require('./enig_error.js');
|
||||
|
||||
// deps
|
||||
const paths = require('path');
|
||||
const async = require('async');
|
||||
const _ = require('lodash');
|
||||
const paths = require('path');
|
||||
const async = require('async');
|
||||
const _ = require('lodash');
|
||||
|
||||
exports.loadMenu = loadMenu;
|
||||
exports.getFormConfigByIDAndMap = getFormConfigByIDAndMap;
|
||||
exports.handleAction = handleAction;
|
||||
exports.getResolvedSpec = getResolvedSpec;
|
||||
exports.handleNext = handleNext;
|
||||
exports.loadMenu = loadMenu;
|
||||
exports.getFormConfigByIDAndMap = getFormConfigByIDAndMap;
|
||||
exports.handleAction = handleAction;
|
||||
exports.getResolvedSpec = getResolvedSpec;
|
||||
exports.handleNext = handleNext;
|
||||
|
||||
function getMenuConfig(client, name, cb) {
|
||||
async.waterfall(
|
||||
[
|
||||
function locateMenuConfig(callback) {
|
||||
const menuConfig = _.get(client.currentTheme, [ 'menus', name ]);
|
||||
const menuConfig = _.get(client.currentTheme, ['menus', name]);
|
||||
if (menuConfig) {
|
||||
return callback(null, menuConfig);
|
||||
}
|
||||
@@ -32,15 +32,18 @@ function getMenuConfig(client, name, cb) {
|
||||
return callback(Errors.DoesNotExist(`No menu entry for "${name}"`));
|
||||
},
|
||||
function locatePromptConfig(menuConfig, callback) {
|
||||
if(_.isString(menuConfig.prompt)) {
|
||||
if(_.has(client.currentTheme, [ 'prompts', menuConfig.prompt ])) {
|
||||
menuConfig.promptConfig = client.currentTheme.prompts[menuConfig.prompt];
|
||||
if (_.isString(menuConfig.prompt)) {
|
||||
if (_.has(client.currentTheme, ['prompts', menuConfig.prompt])) {
|
||||
menuConfig.promptConfig =
|
||||
client.currentTheme.prompts[menuConfig.prompt];
|
||||
return callback(null, menuConfig);
|
||||
}
|
||||
return callback(Errors.DoesNotExist(`No prompt entry for "${menuConfig.prompt}"`));
|
||||
return callback(
|
||||
Errors.DoesNotExist(`No prompt entry for "${menuConfig.prompt}"`)
|
||||
);
|
||||
}
|
||||
return callback(null, menuConfig);
|
||||
}
|
||||
},
|
||||
],
|
||||
(err, menuConfig) => {
|
||||
return cb(err, menuConfig);
|
||||
@@ -50,7 +53,7 @@ function getMenuConfig(client, name, cb) {
|
||||
|
||||
// :TODO: name/client should not be part of options - they are required always
|
||||
function loadMenu(options, cb) {
|
||||
if(!_.isString(options.name) || !_.isObject(options.client)) {
|
||||
if (!_.isString(options.name) || !_.isObject(options.client)) {
|
||||
return cb(Errors.MissingParam('Missing required options'));
|
||||
}
|
||||
|
||||
@@ -62,27 +65,30 @@ function loadMenu(options, cb) {
|
||||
});
|
||||
},
|
||||
function loadMenuModule(menuConfig, callback) {
|
||||
|
||||
menuConfig.config = menuConfig.config || {};
|
||||
menuConfig.config.menuFlags = menuConfig.config.menuFlags || [];
|
||||
if(!Array.isArray(menuConfig.config.menuFlags)) {
|
||||
menuConfig.config.menuFlags = [ menuConfig.config.menuFlags ];
|
||||
if (!Array.isArray(menuConfig.config.menuFlags)) {
|
||||
menuConfig.config.menuFlags = [menuConfig.config.menuFlags];
|
||||
}
|
||||
|
||||
const modAsset = asset.getModuleAsset(menuConfig.module);
|
||||
const modSupplied = null !== modAsset;
|
||||
const modAsset = asset.getModuleAsset(menuConfig.module);
|
||||
const modSupplied = null !== modAsset;
|
||||
|
||||
const modLoadOpts = {
|
||||
name : modSupplied ? modAsset.asset : 'standard_menu',
|
||||
path : (!modSupplied || 'systemModule' === modAsset.type) ? __dirname : Config().paths.mods,
|
||||
category : (!modSupplied || 'systemModule' === modAsset.type) ? null : 'mods',
|
||||
name: modSupplied ? modAsset.asset : 'standard_menu',
|
||||
path:
|
||||
!modSupplied || 'systemModule' === modAsset.type
|
||||
? __dirname
|
||||
: Config().paths.mods,
|
||||
category:
|
||||
!modSupplied || 'systemModule' === modAsset.type ? null : 'mods',
|
||||
};
|
||||
|
||||
moduleUtil.loadModuleEx(modLoadOpts, (err, mod) => {
|
||||
const modData = {
|
||||
name : modLoadOpts.name,
|
||||
config : menuConfig,
|
||||
mod : mod,
|
||||
name: modLoadOpts.name,
|
||||
config: menuConfig,
|
||||
mod: mod,
|
||||
};
|
||||
|
||||
return callback(err, modData);
|
||||
@@ -90,24 +96,30 @@ function loadMenu(options, cb) {
|
||||
},
|
||||
function createModuleInstance(modData, callback) {
|
||||
Log.trace(
|
||||
{ moduleName : modData.name, extraArgs : options.extraArgs, config : modData.config, info : modData.mod.modInfo },
|
||||
'Creating menu module instance');
|
||||
{
|
||||
moduleName: modData.name,
|
||||
extraArgs: options.extraArgs,
|
||||
config: modData.config,
|
||||
info: modData.mod.modInfo,
|
||||
},
|
||||
'Creating menu module instance'
|
||||
);
|
||||
|
||||
let moduleInstance;
|
||||
try {
|
||||
moduleInstance = new modData.mod.getModule({
|
||||
menuName : options.name,
|
||||
menuConfig : modData.config,
|
||||
extraArgs : options.extraArgs,
|
||||
client : options.client,
|
||||
lastMenuResult : options.lastMenuResult,
|
||||
menuName: options.name,
|
||||
menuConfig: modData.config,
|
||||
extraArgs: options.extraArgs,
|
||||
client: options.client,
|
||||
lastMenuResult: options.lastMenuResult,
|
||||
});
|
||||
} catch(e) {
|
||||
} catch (e) {
|
||||
return callback(e);
|
||||
}
|
||||
|
||||
return callback(null, moduleInstance);
|
||||
}
|
||||
},
|
||||
],
|
||||
(err, modInst) => {
|
||||
return cb(err, modInst);
|
||||
@@ -116,82 +128,99 @@ function loadMenu(options, cb) {
|
||||
}
|
||||
|
||||
function getFormConfigByIDAndMap(menuConfig, formId, mciMap, cb) {
|
||||
if(!_.isObject(menuConfig.form)) {
|
||||
if (!_.isObject(menuConfig.form)) {
|
||||
return cb(Errors.MissingParam('Invalid or missing "form" member for menu'));
|
||||
}
|
||||
|
||||
if(!_.isObject(menuConfig.form[formId])) {
|
||||
if (!_.isObject(menuConfig.form[formId])) {
|
||||
return cb(Errors.DoesNotExist(`No form found for formId ${formId}`));
|
||||
}
|
||||
|
||||
const formForId = menuConfig.form[formId];
|
||||
const mciReqKey = _.filter(_.map(_.sortBy(mciMap, 'code'), 'code'), (mci) => {
|
||||
const mciReqKey = _.filter(_.map(_.sortBy(mciMap, 'code'), 'code'), mci => {
|
||||
return MCIViewFactory.UserViewCodes.indexOf(mci) > -1;
|
||||
}).join('');
|
||||
|
||||
Log.trace( { mciKey : mciReqKey }, 'Looking for MCI configuration key');
|
||||
Log.trace({ mciKey: mciReqKey }, 'Looking for MCI configuration key');
|
||||
|
||||
//
|
||||
// Exact, explicit match?
|
||||
//
|
||||
if(_.isObject(formForId[mciReqKey])) {
|
||||
Log.trace( { mciKey : mciReqKey }, 'Using exact configuration key match');
|
||||
if (_.isObject(formForId[mciReqKey])) {
|
||||
Log.trace({ mciKey: mciReqKey }, 'Using exact configuration key match');
|
||||
return cb(null, formForId[mciReqKey]);
|
||||
}
|
||||
|
||||
//
|
||||
// Generic match
|
||||
//
|
||||
if(_.has(formForId, 'mci') || _.has(formForId, 'submit')) {
|
||||
if (_.has(formForId, 'mci') || _.has(formForId, 'submit')) {
|
||||
Log.trace('Using generic configuration');
|
||||
return cb(null, formForId);
|
||||
}
|
||||
|
||||
return cb(Errors.DoesNotExist(`No matching form configuration found for key "${mciReqKey}"`));
|
||||
return cb(
|
||||
Errors.DoesNotExist(`No matching form configuration found for key "${mciReqKey}"`)
|
||||
);
|
||||
}
|
||||
|
||||
// :TODO: Most of this should be moved elsewhere .... DRY...
|
||||
function callModuleMenuMethod(client, asset, path, formData, extraArgs, cb) {
|
||||
if('' === paths.extname(path)) {
|
||||
if ('' === paths.extname(path)) {
|
||||
path += '.js';
|
||||
}
|
||||
|
||||
try {
|
||||
client.log.trace(
|
||||
{ path : path, methodName : asset.asset, formData : formData, extraArgs : extraArgs },
|
||||
'Calling menu method');
|
||||
{
|
||||
path: path,
|
||||
methodName: asset.asset,
|
||||
formData: formData,
|
||||
extraArgs: extraArgs,
|
||||
},
|
||||
'Calling menu method'
|
||||
);
|
||||
|
||||
const methodMod = require(path);
|
||||
return methodMod[asset.asset](client.currentMenuModule, formData || { }, extraArgs, cb);
|
||||
} catch(e) {
|
||||
client.log.error( { error : e.toString(), methodName : asset.asset }, 'Failed to execute asset method');
|
||||
return methodMod[asset.asset](
|
||||
client.currentMenuModule,
|
||||
formData || {},
|
||||
extraArgs,
|
||||
cb
|
||||
);
|
||||
} catch (e) {
|
||||
client.log.error(
|
||||
{ error: e.toString(), methodName: asset.asset },
|
||||
'Failed to execute asset method'
|
||||
);
|
||||
return cb(e);
|
||||
}
|
||||
}
|
||||
|
||||
function handleAction(client, formData, conf, cb) {
|
||||
if(!_.isObject(conf)) {
|
||||
if (!_.isObject(conf)) {
|
||||
return cb(Errors.MissingParam('Missing config'));
|
||||
}
|
||||
|
||||
const action = getResolvedSpec(client, conf.action, 'action'); // random/conditionals/etc.
|
||||
const action = getResolvedSpec(client, conf.action, 'action'); // random/conditionals/etc.
|
||||
const actionAsset = asset.parseAsset(action);
|
||||
if(!_.isObject(actionAsset)) {
|
||||
if (!_.isObject(actionAsset)) {
|
||||
return cb(Errors.Invalid('Unable to parse "conf.action"'));
|
||||
}
|
||||
|
||||
switch(actionAsset.type) {
|
||||
case 'method' :
|
||||
case 'systemMethod' :
|
||||
if(_.isString(actionAsset.location)) {
|
||||
switch (actionAsset.type) {
|
||||
case 'method':
|
||||
case 'systemMethod':
|
||||
if (_.isString(actionAsset.location)) {
|
||||
return callModuleMenuMethod(
|
||||
client,
|
||||
actionAsset,
|
||||
paths.join(Config().paths.mods, actionAsset.location),
|
||||
formData,
|
||||
conf.extraArgs,
|
||||
cb);
|
||||
} else if('systemMethod' === actionAsset.type) {
|
||||
cb
|
||||
);
|
||||
} else if ('systemMethod' === actionAsset.type) {
|
||||
// :TODO: Need to pass optional args here -- conf.extraArgs and args between e.g. ()
|
||||
// :TODO: Probably better as system_method.js
|
||||
return callModuleMenuMethod(
|
||||
@@ -200,21 +229,30 @@ function handleAction(client, formData, conf, cb) {
|
||||
paths.join(__dirname, 'system_menu_method.js'),
|
||||
formData,
|
||||
conf.extraArgs,
|
||||
cb);
|
||||
cb
|
||||
);
|
||||
} else {
|
||||
// local to current module
|
||||
const currentModule = client.currentMenuModule;
|
||||
if(_.isFunction(currentModule.menuMethods[actionAsset.asset])) {
|
||||
return currentModule.menuMethods[actionAsset.asset](formData, conf.extraArgs, cb);
|
||||
if (_.isFunction(currentModule.menuMethods[actionAsset.asset])) {
|
||||
return currentModule.menuMethods[actionAsset.asset](
|
||||
formData,
|
||||
conf.extraArgs,
|
||||
cb
|
||||
);
|
||||
}
|
||||
|
||||
const err = Errors.DoesNotExist('Method does not exist');
|
||||
client.log.warn( { method : actionAsset.asset }, err.message);
|
||||
client.log.warn({ method: actionAsset.asset }, err.message);
|
||||
return cb(err);
|
||||
}
|
||||
|
||||
case 'menu' :
|
||||
return client.currentMenuModule.gotoMenu(actionAsset.asset, { formData : formData, extraArgs : conf.extraArgs }, cb );
|
||||
case 'menu':
|
||||
return client.currentMenuModule.gotoMenu(
|
||||
actionAsset.asset,
|
||||
{ formData: formData, extraArgs: conf.extraArgs },
|
||||
cb
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -237,15 +275,15 @@ function getResolvedSpec(client, spec, memberName) {
|
||||
// (3) Simple array of strings. A random selection will be made:
|
||||
// next: [ "foo", "baz", "fizzbang" ]
|
||||
//
|
||||
if(!Array.isArray(spec)) {
|
||||
return spec; // (1) simple string, as-is
|
||||
if (!Array.isArray(spec)) {
|
||||
return spec; // (1) simple string, as-is
|
||||
}
|
||||
|
||||
if(_.isObject(spec[0])) {
|
||||
return client.acs.getConditionalValue(spec, memberName); // (2) ACS conditionals
|
||||
if (_.isObject(spec[0])) {
|
||||
return client.acs.getConditionalValue(spec, memberName); // (2) ACS conditionals
|
||||
}
|
||||
|
||||
return spec[Math.floor(Math.random() * spec.length)]; // (3) random
|
||||
return spec[Math.floor(Math.random() * spec.length)]; // (3) random
|
||||
}
|
||||
|
||||
function handleNext(client, nextSpec, conf, cb) {
|
||||
@@ -257,32 +295,54 @@ function handleNext(client, nextSpec, conf, cb) {
|
||||
const extraArgs = conf.extraArgs || {};
|
||||
|
||||
// :TODO: DRY this with handleAction()
|
||||
switch(nextAsset.type) {
|
||||
case 'method' :
|
||||
case 'systemMethod' :
|
||||
if(_.isString(nextAsset.location)) {
|
||||
return callModuleMenuMethod(client, nextAsset, paths.join(Config().paths.mods, nextAsset.location), {}, extraArgs, cb);
|
||||
} else if('systemMethod' === nextAsset.type) {
|
||||
switch (nextAsset.type) {
|
||||
case 'method':
|
||||
case 'systemMethod':
|
||||
if (_.isString(nextAsset.location)) {
|
||||
return callModuleMenuMethod(
|
||||
client,
|
||||
nextAsset,
|
||||
paths.join(Config().paths.mods, nextAsset.location),
|
||||
{},
|
||||
extraArgs,
|
||||
cb
|
||||
);
|
||||
} else if ('systemMethod' === nextAsset.type) {
|
||||
// :TODO: see other notes about system_menu_method.js here
|
||||
return callModuleMenuMethod(client, nextAsset, paths.join(__dirname, 'system_menu_method.js'), {}, extraArgs, cb);
|
||||
return callModuleMenuMethod(
|
||||
client,
|
||||
nextAsset,
|
||||
paths.join(__dirname, 'system_menu_method.js'),
|
||||
{},
|
||||
extraArgs,
|
||||
cb
|
||||
);
|
||||
} else {
|
||||
// local to current module
|
||||
const currentModule = client.currentMenuModule;
|
||||
if(_.isFunction(currentModule.menuMethods[nextAsset.asset])) {
|
||||
const formData = {}; // we don't have any
|
||||
return currentModule.menuMethods[nextAsset.asset]( formData, extraArgs, cb );
|
||||
if (_.isFunction(currentModule.menuMethods[nextAsset.asset])) {
|
||||
const formData = {}; // we don't have any
|
||||
return currentModule.menuMethods[nextAsset.asset](
|
||||
formData,
|
||||
extraArgs,
|
||||
cb
|
||||
);
|
||||
}
|
||||
|
||||
const err = Errors.DoesNotExist('Method does not exist');
|
||||
client.log.warn( { method : nextAsset.asset }, err.message);
|
||||
client.log.warn({ method: nextAsset.asset }, err.message);
|
||||
return cb(err);
|
||||
}
|
||||
|
||||
case 'menu' :
|
||||
return client.currentMenuModule.gotoMenu(nextAsset.asset, { extraArgs : extraArgs }, cb );
|
||||
case 'menu':
|
||||
return client.currentMenuModule.gotoMenu(
|
||||
nextAsset.asset,
|
||||
{ extraArgs: extraArgs },
|
||||
cb
|
||||
);
|
||||
}
|
||||
|
||||
const err = Errors.Invalid('Invalid asset type for "next"');
|
||||
client.log.error( { nextSpec : nextSpec }, err.message);
|
||||
client.log.error({ nextSpec: nextSpec }, err.message);
|
||||
return cb(err);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user