Ghost/core/server/services/themes/to-json.js
Hannah Wolfe bc75fab663 Moved theme service to core/server
refs: bf0823c9a2
refs: ae86254972

- continuing the work of splitting up the theme service into logical components

Themes Service
- The serverside theme service now serves just the API and boot
- It loads the theme and passes it to the theme-engine via the bridge

This achieves the bare minimum goal of removing all the cross requires between server and frontend around themes
There is still a lot more to do to achieve an ideal architecture here as laid out in ae86254972
2021-04-27 15:14:49 +01:00

49 lines
1.6 KiB
JavaScript

const _ = require('lodash');
const themeList = require('./list');
const bridge = require('../../../bridge');
const packageJSON = require('../../lib/fs/package-json');
const settingsCache = require('../settings/cache');
/**
*
* 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
*
* @param {string} [name] - the theme to output
* @param {object} [checkedTheme] - a theme result from gscan
* @return {*}
*/
module.exports = function toJSON(name, checkedTheme) {
let themeResult;
let toFilter;
if (!name) {
toFilter = themeList.getAll();
themeResult = packageJSON.filter(toFilter, settingsCache.get('active_theme'));
} else {
toFilter = {
[name]: themeList.get(name)
};
themeResult = packageJSON.filter(toFilter, settingsCache.get('active_theme'));
if (checkedTheme && checkedTheme.results.warning.length > 0) {
themeResult[0].warnings = _.cloneDeep(checkedTheme.results.warning);
}
if (checkedTheme && checkedTheme.results.error.length > 0) {
themeResult[0].errors = _.cloneDeep(checkedTheme.results.error);
}
}
// CASE: if you want a JSON response for a single theme, which is not active.
if (_.find(themeResult, {active: true}) && bridge.getActiveTheme()) {
_.find(themeResult, {active: true}).templates = bridge.getActiveTheme().customTemplates;
}
return {themes: themeResult};
};