2017-03-13 14:44:44 +03:00
|
|
|
var _ = require('lodash'),
|
|
|
|
themeList = require('./list'),
|
2017-10-10 15:36:35 +03:00
|
|
|
active = require('./active'),
|
2017-03-13 14:44:44 +03:00
|
|
|
packages = require('../utils/packages'),
|
|
|
|
settingsCache = require('../settings/cache');
|
|
|
|
|
|
|
|
/**
|
2017-10-10 15:36:35 +03:00
|
|
|
*
|
|
|
|
* Provides a JSON object which can be returned via the API.
|
|
|
|
* You can either request all themes or a specific theme if you pass the `name` argument.
|
|
|
|
* Furthermore, you can pass a gscan result to filter warnings/errors.
|
|
|
|
*
|
|
|
|
* @TODO: settingsCache.get('active_theme') vs. active.get().name
|
2017-03-13 14:44:44 +03:00
|
|
|
*
|
|
|
|
* @param {string} [name] - the theme to output
|
|
|
|
* @param {object} [checkedTheme] - a theme result from gscan
|
|
|
|
* @return {*}
|
|
|
|
*/
|
|
|
|
module.exports = function toJSON(name, checkedTheme) {
|
|
|
|
var themeResult, toFilter;
|
|
|
|
|
|
|
|
if (!name) {
|
|
|
|
toFilter = themeList.getAll();
|
2017-04-24 20:41:00 +03:00
|
|
|
themeResult = packages.filterPackages(toFilter, settingsCache.get('active_theme'));
|
2017-03-13 14:44:44 +03:00
|
|
|
} else {
|
2017-06-06 14:33:18 +03:00
|
|
|
toFilter = {
|
|
|
|
[name]: themeList.get(name)
|
|
|
|
};
|
2017-03-13 14:44:44 +03:00
|
|
|
|
2017-04-24 20:41:00 +03:00
|
|
|
themeResult = packages.filterPackages(toFilter, settingsCache.get('active_theme'));
|
2017-03-13 14:44:44 +03:00
|
|
|
|
|
|
|
if (checkedTheme && checkedTheme.results.warning.length > 0) {
|
|
|
|
themeResult[0].warnings = _.cloneDeep(checkedTheme.results.warning);
|
|
|
|
}
|
2017-05-31 19:42:42 +03:00
|
|
|
|
|
|
|
if (checkedTheme && checkedTheme.results.error.length > 0) {
|
|
|
|
themeResult[0].errors = _.cloneDeep(checkedTheme.results.error);
|
|
|
|
}
|
2017-03-13 14:44:44 +03:00
|
|
|
}
|
|
|
|
|
2017-10-10 15:36:35 +03:00
|
|
|
// CASE: if you want a JSON response for a single theme, which is not active.
|
|
|
|
if (_.find(themeResult, {active: true}) && active.get()) {
|
|
|
|
_.find(themeResult, {active: true}).templates = active.get().customTemplates;
|
|
|
|
}
|
|
|
|
|
2017-03-13 14:44:44 +03:00
|
|
|
return {themes: themeResult};
|
|
|
|
};
|