* Convert all JSON configurations (*.json) to HJSON (*.hjson) which is much more flexible for a human readable and editable configuration format

* WIP "next" vs "action" changes
* options.cls is now defaulted in config.js/config.hjson (default = true)
* Notes/etc.
This commit is contained in:
Bryan Ashby 2015-09-08 22:08:45 -06:00
parent 052cf5c490
commit ca4b99a83e
10 changed files with 163 additions and 108 deletions

View file

@ -8,6 +8,7 @@ var _ = require('lodash');
var assert = require('assert');
exports.parseAsset = parseAsset;
exports.getAssetWithShorthand = getAssetWithShorthand;
exports.getArtAsset = getArtAsset;
exports.getModuleAsset = getModuleAsset;
exports.resolveConfigAsset = resolveConfigAsset;
@ -42,6 +43,25 @@ function parseAsset(s) {
}
}
function getAssetWithShorthand(spec, defaultType) {
if(!_.isString(spec)) {
return null;
}
if('@' === spec[0]) {
var asset = parseAsset(spec);
assert(_.isString(asset.type));
return asset;
} else {
return {
type : defaultType,
asset : spec,
}
}
}
// :TODO: Convert these to getAssetWithShorthand()
function getArtAsset(art) {
if(!_.isString(art)) {
return null;

View file

@ -5,9 +5,9 @@ var miscUtil = require('./misc_util.js');
var fs = require('fs');
var paths = require('path');
var stripJsonComments = require('strip-json-comments');
var async = require('async');
var _ = require('lodash');
var hjson = require('hjson');
exports.init = init;
exports.getDefaultPath = getDefaultPath;
@ -22,9 +22,10 @@ function init(configPath, cb) {
callback(null, { } );
} else {
try {
var configJson = JSON.parse(stripJsonComments(data));
var configJson = hjson.parse(data);
callback(null, configJson);
} catch(e) {
console.log(e)
callback(e);
}
}
@ -53,7 +54,7 @@ function init(configPath, cb) {
function getDefaultPath() {
var base = miscUtil.resolvePath('~/');
if(base) {
return paths.join(base, '.enigma-bbs', 'config.json');
return paths.join(base, '.enigma-bbs', 'config.hjson');
}
}
@ -80,6 +81,16 @@ function getDefaultConfig() {
defaultGroups : [ 'users' ] // default groups new users belong to
},
// :TODO: better name for "defaults"... which is redundant here!
/*
Concept
"theme" : {
"default" : "defaultThemeName", // or "*"
"preLogin" : "*",
"passwordChar" : "*",
...
}
*/
defaults : {
theme : 'NU-MAYA', // :TODO: allow "*" here
passwordChar : '*', // TODO: move to user ?
@ -94,14 +105,9 @@ function getDefaultConfig() {
}
},
/*
Concept
"theme" : {
"default" : "defaultThemeName", // or "*"
"passwordChar" : "*",
...
}
*/
menus : {
cls : true, // Clear screen before each menu by default?
},
paths : {
mods : paths.join(__dirname, './../mods/'),

View file

@ -9,6 +9,7 @@ var fs = require('fs');
var Gaze = require('gaze').Gaze;
var stripJsonComments = require('strip-json-comments');
var assert = require('assert');
var hjson = require('hjson');
module.exports = exports = new JSONCache();
@ -21,9 +22,11 @@ function JSONCache() {
this.reCacheJSONFromFile = function(filePath, cb) {
fs.readFile(filePath, { encoding : 'utf-8' }, function fileRead(err, data) {
try {
self.cache[filePath] = JSON.parse(stripJsonComments(data));
//self.cache[filePath] = JSON.parse(stripJsonComments(data));
self.cache[filePath] = hjson.parse(data);
cb(null, self.cache[filePath]);
} catch(e) {
console.log(e)
cb(e);
}
});

View file

@ -9,6 +9,7 @@ var ansi = require('./ansi_term.js');
var asset = require('./asset.js');
var ViewController = require('./view_controller.js').ViewController;
var menuUtil = require('./menu_util.js');
var Config = require('./config.js').config;
var async = require('async');
var assert = require('assert');
@ -27,6 +28,10 @@ function MenuModule(options) {
this.menuConfig.options = options.menuConfig.options || {};
this.menuMethods = {}; // methods called from @method's
this.cls = _.isBoolean(this.menuConfig.options.cls) ?
this.menuConfig.options.cls :
Config.menus.cls;
this.initViewControllers();
this.initSequence = function() {
@ -133,6 +138,7 @@ function MenuModule(options) {
return 'end' === self.menuConfig.options.pause || true === self.menuConfig.options.pause;
};
// :TODO: Convert this to process "next" instead of "action"
this.nextAction = function() {
if(!_.isObject(self.menuConfig.form) && !_.isString(self.menuConfig.prompt) &&
_.isString(self.menuConfig.action))
@ -164,7 +170,7 @@ MenuModule.prototype.leave = function() {
};
MenuModule.prototype.beforeArt = function() {
if(this.menuConfig.options.cls) {
if(this.cls) {
this.client.term.write(ansi.resetScreen());
}
@ -240,7 +246,8 @@ MenuModule.prototype.finishedLoading = function() {
_.isString(this.menuConfig.next))
{
setTimeout(function nextTimeout() {
self.client.gotoMenuModule( { name : self.menuConfig.next } );
menuUtil.handleNext(self.client, self.menuConfig.next);
//self.client.gotoMenuModule( { name : self.menuConfig.next } );
}, this.menuConfig.options.nextTimeout);
}
};

View file

@ -17,11 +17,10 @@ var async = require('async');
var assert = require('assert');
var _ = require('lodash');
var stripJsonComments = require('strip-json-comments');
exports.loadMenu = loadMenu;
exports.getFormConfigByIDAndMap = getFormConfigByIDAndMap;
exports.handleAction = handleAction;
exports.handleNext = handleNext;
exports.applyThemeCustomization = applyThemeCustomization;
function getMenuConfig(name, cb) {
@ -30,7 +29,7 @@ function getMenuConfig(name, cb) {
async.waterfall(
[
function loadMenuJSON(callback) {
jsonCache.getJSON('menu.json', function loaded(err, menuJson) {
jsonCache.getJSON('menu.hjson', function loaded(err, menuJson) {
callback(err, menuJson);
});
},
@ -44,7 +43,7 @@ function getMenuConfig(name, cb) {
},
function loadPromptJSON(callback) {
if(_.isString(menuConfig.prompt)) {
jsonCache.getJSON('prompt.json', function loaded(err, promptJson, reCached) {
jsonCache.getJSON('prompt.hjson', function loaded(err, promptJson, reCached) {
callback(err, promptJson);
});
} else {
@ -166,6 +165,20 @@ function getFormConfigByIDAndMap(menuConfig, formId, mciMap, cb) {
cb(new Error('No matching form configuration found for key \'' + mciReqKey + '\''));
}
// :TODO: Most of this should be moved elsewhere .... DRY...
function callModuleMenuMethod(client, asset, path, formData) {
if('' === paths.extname(path)) {
path += '.js';
}
try {
var methodMod = require(path);
methodMod[asset.asset](client.currentMenuModule, formData || { }, conf.extraArgs);
} catch(e) {
client.log.error( { error : e.toString(), methodName : asset.asset }, 'Failed to execute asset method');
}
}
function handleAction(client, formData, conf) {
assert(_.isObject(conf));
assert(_.isString(conf.action));
@ -173,30 +186,16 @@ function handleAction(client, formData, conf) {
var actionAsset = asset.parseAsset(conf.action);
assert(_.isObject(actionAsset));
// :TODO: Most of this should be moved elsewhere .... DRY...
function callModuleMenuMethod(path) {
if('' === paths.extname(path)) {
path += '.js';
}
try {
var methodMod = require(path);
methodMod[actionAsset.asset](client.currentMenuModule, formData, conf.extraArgs);
} catch(e) {
Log.error( { error : e.toString(), methodName : actionAsset.asset }, 'Failed to execute asset method');
}
}
switch(actionAsset.type) {
case 'method' :
case 'systemMethod' :
if(_.isString(actionAsset.location)) {
callModuleMenuMethod(paths.join(Config.paths.mods, actionAsset.location));
callModuleMenuMethod(client, actionAsset, paths.join(Config.paths.mods, actionAsset.location, formData));
} 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
callModuleMenuMethod(paths.join(__dirname, 'system_menu_method.js'));
callModuleMenuMethod(client, actionAsset, paths.join(__dirname, 'system_menu_method.js'), formData);
} else {
// local to current module
var currentModule = client.currentMenuModule;
@ -213,6 +212,40 @@ function handleAction(client, formData, conf) {
}
}
function handleNext(client, nextSpec) {
assert(_.isString(nextSpec));
var nextAsset = asset.getAssetWithShorthand(nextSpec, 'menu');
switch(nextAsset.type) {
case 'method' :
case 'systemMethod' :
if(_.isString(nextAsset.location)) {
callModuleMenuMethod(client, nextAsset, paths.join(Config.paths.mods, actionAsset.location));
} else {
if('systemMethod' === nextAsset.type) {
// :TODO: see other notes about system_menu_method.js here
callModuleMenuMethod(client, nextAsset, paths.join(__dirname, 'system_menu_method.js'));
} else {
// local to current module
var currentModule = client.currentMenuModule;
if(_.isFunction(currentModule.menuMethods[actionAsset.asset])) {
currentModule.menuMethods[actionAsset.asset]( { }, { } );
}
}
}
break;
case 'menu' :
client.gotoMenuModule( { name : nextAsset.asset } );
break;
default :
client.log.error( { nextSpec : nextSpec }, 'Invalid asset type for "next"');
break;
}
}
// :TODO: This should be in theme.js
// :TODO: Need to take (optional) form ID to search for (e.g. for multi-form menus)

View file

@ -29,4 +29,5 @@ function getUserLoginHistory(numRequested, cb) {
cb(err, loginHistory);
}
);
}
}

View file

@ -227,7 +227,7 @@ function displayThemedPause(options, cb) {
async.series(
[
function loadPromptJSON(callback) {
jsonCache.getJSON('prompt.json', function loaded(err, promptJson) {
jsonCache.getJSON('prompt.hjson', function loaded(err, promptJson) {
if(err) {
callback(err);
} else {