2020-04-29 18:44:27 +03:00
|
|
|
const _ = require('lodash');
|
|
|
|
const themeList = require('./list');
|
2021-04-27 15:49:15 +03:00
|
|
|
const bridge = require('../../../bridge');
|
2021-05-06 14:46:37 +03:00
|
|
|
const packageJSON = require('../../lib/package-json');
|
2021-04-27 15:28:04 +03:00
|
|
|
const settingsCache = require('../settings/cache');
|
2017-03-13 14:44:44 +03:00
|
|
|
|
|
|
|
/**
|
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) {
|
2020-04-29 18:44:27 +03:00
|
|
|
let themeResult;
|
|
|
|
let toFilter;
|
2017-03-13 14:44:44 +03:00
|
|
|
|
|
|
|
if (!name) {
|
|
|
|
toFilter = themeList.getAll();
|
2017-12-15 00:07:53 +03:00
|
|
|
themeResult = packageJSON.filter(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-12-15 00:07:53 +03:00
|
|
|
themeResult = packageJSON.filter(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.
|
2021-04-27 15:49:15 +03:00
|
|
|
if (_.find(themeResult, {active: true}) && bridge.getActiveTheme()) {
|
|
|
|
_.find(themeResult, {active: true}).templates = bridge.getActiveTheme().customTemplates;
|
2017-10-10 15:36:35 +03:00
|
|
|
}
|
|
|
|
|
2017-03-13 14:44:44 +03:00
|
|
|
return {themes: themeResult};
|
|
|
|
};
|