Ability to disabled themes + clean up theme code slightly. Area needs work!
This commit is contained in:
parent
782cae6ba3
commit
b1b841674c
154
core/theme.js
154
core/theme.js
|
@ -10,6 +10,7 @@ const getFullConfig = require('./config_util.js').getFullConfig;
|
||||||
const asset = require('./asset.js');
|
const asset = require('./asset.js');
|
||||||
const ViewController = require('./view_controller.js').ViewController;
|
const ViewController = require('./view_controller.js').ViewController;
|
||||||
const Errors = require('./enig_error.js').Errors;
|
const Errors = require('./enig_error.js').Errors;
|
||||||
|
const ErrorReasons = require('./enig_error.js').ErrorReasons;
|
||||||
|
|
||||||
const fs = require('graceful-fs');
|
const fs = require('graceful-fs');
|
||||||
const paths = require('path');
|
const paths = require('path');
|
||||||
|
@ -80,24 +81,27 @@ function refreshThemeHelpers(theme) {
|
||||||
|
|
||||||
function loadTheme(themeID, cb) {
|
function loadTheme(themeID, cb) {
|
||||||
|
|
||||||
var path = paths.join(Config.paths.themes, themeID, 'theme.hjson');
|
const path = paths.join(Config.paths.themes, themeID, 'theme.hjson');
|
||||||
|
|
||||||
configCache.getConfigWithOptions( { filePath : path, forceReCache : true }, function loaded(err, theme) {
|
configCache.getConfigWithOptions( { filePath : path, forceReCache : true }, (err, theme) => {
|
||||||
if(err) {
|
if(err) {
|
||||||
cb(err);
|
return cb(err);
|
||||||
} else {
|
|
||||||
if(!_.isObject(theme.info) ||
|
|
||||||
!_.isString(theme.info.name) ||
|
|
||||||
!_.isString(theme.info.author))
|
|
||||||
{
|
|
||||||
cb(new Error('Invalid or missing "info" section!'));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
refreshThemeHelpers(theme);
|
|
||||||
|
|
||||||
cb(null, theme, path);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(!_.isObject(theme.info) ||
|
||||||
|
!_.isString(theme.info.name) ||
|
||||||
|
!_.isString(theme.info.author))
|
||||||
|
{
|
||||||
|
return cb(Errors.Invalid('Invalid or missing "info" section'));
|
||||||
|
}
|
||||||
|
|
||||||
|
if(false === _.get(theme, 'info.enabled')) {
|
||||||
|
return cb(Errors.General('Theme is not enalbed', ErrorReasons.ErrNotEnabled));
|
||||||
|
}
|
||||||
|
|
||||||
|
refreshThemeHelpers(theme);
|
||||||
|
|
||||||
|
return cb(null, theme, path);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -261,69 +265,72 @@ function getMergedTheme(menuConfig, promptConfig, theme) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function initAvailableThemes(cb) {
|
function initAvailableThemes(cb) {
|
||||||
var menuConfig;
|
|
||||||
var promptConfig;
|
|
||||||
|
|
||||||
async.waterfall(
|
async.waterfall(
|
||||||
[
|
[
|
||||||
function loadMenuConfig(callback) {
|
function loadMenuConfig(callback) {
|
||||||
getFullConfig(Config.general.menuFile, function gotConfig(err, mc) {
|
getFullConfig(Config.general.menuFile, (err, menuConfig) => {
|
||||||
menuConfig = mc;
|
return callback(err, menuConfig);
|
||||||
callback(err);
|
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
function loadPromptConfig(callback) {
|
function loadPromptConfig(menuConfig, callback) {
|
||||||
getFullConfig(Config.general.promptFile, function gotConfig(err, pc) {
|
getFullConfig(Config.general.promptFile, (err, promptConfig) => {
|
||||||
promptConfig = pc;
|
return callback(err, menuConfig, promptConfig);
|
||||||
callback(err);
|
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
function getDir(callback) {
|
function getThemeDirectories(menuConfig, promptConfig, callback) {
|
||||||
fs.readdir(Config.paths.themes, function dirRead(err, files) {
|
fs.readdir(Config.paths.themes, (err, files) => {
|
||||||
callback(err, files);
|
if(err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
return callback(
|
||||||
|
null,
|
||||||
|
menuConfig,
|
||||||
|
promptConfig,
|
||||||
|
files.filter( f => {
|
||||||
|
// sync normally not allowed -- initAvailableThemes() is a startup-only method, however
|
||||||
|
return fs.statSync(paths.join(Config.paths.themes, f)).isDirectory();
|
||||||
|
})
|
||||||
|
);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
function filterFiles(files, callback) {
|
function populateAvailable(menuConfig, promptConfig, themeDirectories, callback) {
|
||||||
var filtered = files.filter(function filter(file) {
|
async.each(themeDirectories, (themeId, nextThemeDir) => { // theme dir = theme ID
|
||||||
return fs.statSync(paths.join(Config.paths.themes, file)).isDirectory();
|
loadTheme(themeId, (err, theme, themePath) => {
|
||||||
});
|
if(err) {
|
||||||
callback(null, filtered);
|
if(ErrorReasons.NotEnabled !== err.reasonCode) {
|
||||||
},
|
Log.warn( { themeId : themeId, err : err.message }, 'Failed loading theme');
|
||||||
function populateAvailable(filtered, callback) {
|
}
|
||||||
// :TODO: this is a bit broken with callback placement and configCache.on() handler
|
|
||||||
|
|
||||||
filtered.forEach(function themeEntry(themeId) {
|
return nextThemeDir(null); // try next
|
||||||
loadTheme(themeId, function themeLoaded(err, theme, themePath) {
|
|
||||||
if(!err) {
|
|
||||||
availableThemes[themeId] = getMergedTheme(menuConfig, promptConfig, theme);
|
|
||||||
|
|
||||||
configCache.on('recached', function recached(path) {
|
|
||||||
if(themePath === path) {
|
|
||||||
loadTheme(themeId, function reloaded(err, reloadedTheme) {
|
|
||||||
Log.debug( { info : theme.info }, 'Theme recached' );
|
|
||||||
|
|
||||||
availableThemes[themeId] = reloadedTheme;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
Log.debug( { info : theme.info }, 'Theme loaded');
|
|
||||||
} else {
|
|
||||||
Log.warn( { themeId : themeId, error : err.toString() }, 'Failed to load theme');
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
|
availableThemes[themeId] = getMergedTheme(menuConfig, promptConfig, theme);
|
||||||
|
|
||||||
|
configCache.on('recached', recachedPath => {
|
||||||
|
if(themePath === recachedPath) {
|
||||||
|
loadTheme(themeId, (err, reloadedTheme) => {
|
||||||
|
if(!err) {
|
||||||
|
// :TODO: This is still broken - Need to reapply *latest* menu config and prompt configs to theme at very least
|
||||||
|
Log.debug( { info : theme.info }, 'Theme recached' );
|
||||||
|
availableThemes[themeId] = getMergedTheme(menuConfig, promptConfig, reloadedTheme);
|
||||||
|
} else if(ErrorReasons.NotEnabled === err.reasonCode) {
|
||||||
|
// :TODO: we need to disable this theme -- users may be using it! We'll need to re-assign them if so
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return nextThemeDir(null);
|
||||||
|
});
|
||||||
|
}, err => {
|
||||||
|
return callback(err);
|
||||||
});
|
});
|
||||||
callback(null);
|
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
function onComplete(err) {
|
err => {
|
||||||
if(err) {
|
return cb(err, availableThemes ? availableThemes.length : 0);
|
||||||
cb(err);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
cb(null, availableThemes.length);
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -340,17 +347,24 @@ function getRandomTheme() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function setClientTheme(client, themeId) {
|
function setClientTheme(client, themeId) {
|
||||||
var desc;
|
let logMsg;
|
||||||
|
|
||||||
try {
|
const availThemes = getAvailableThemes();
|
||||||
client.currentTheme = getAvailableThemes()[themeId];
|
|
||||||
desc = 'Set client theme';
|
client.currentTheme = availThemes[themeId];
|
||||||
} catch(e) {
|
if(client.currentTheme) {
|
||||||
client.currentTheme = getAvailableThemes()[Config.defaults.theme];
|
logMsg = 'Set client theme';
|
||||||
desc = 'Failed setting theme by supplied ID; Using default';
|
} else {
|
||||||
|
client.currentTheme = availThemes[Config.defaults.theme];
|
||||||
|
if(client.currentTheme) {
|
||||||
|
logMsg = 'Failed setting theme by supplied ID; Using default';
|
||||||
|
} else {
|
||||||
|
client.currentTheme = availThemes[Object.keys(availThemes)[0]];
|
||||||
|
logMsg = 'Failed setting theme by system default ID; Using the first one we can find';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
client.log.debug( { themeId : themeId, info : client.currentTheme.info }, desc);
|
client.log.debug( { themeId : themeId, info : client.currentTheme.info }, logMsg);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getThemeArt(options, cb) {
|
function getThemeArt(options, cb) {
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
name: Mystery Skull
|
name: Mystery Skull
|
||||||
author: Luciano Ayres
|
author: Luciano Ayres
|
||||||
group: blocktronics
|
group: blocktronics
|
||||||
|
enabled: true
|
||||||
}
|
}
|
||||||
|
|
||||||
customization: {
|
customization: {
|
||||||
|
|
Loading…
Reference in New Issue