Ghost/core/frontend/services/themes/index.js
Hannah Wolfe af09d55c7a Moved activate from themes to the bridge
refs: bf0823c9a2

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

Theme activations are a trickier piece of the theme split puzzle because they are called from the API and theme service on boot in different ways.

Activations require a theme to have been validated at different levels. Validations are also tricky - do they belong to the theme engine, or the theme service?

There are then several different flows for activations:
- On Boot
- API "activate" call
- API override on upload or install via setFromZip, which is a method in the storage layer

These calls all have quite different logical flows at the moment, and need to be unified

For now, I've moved the existing "activate" function onto the bridge. This allows the theme service to be split from the frontend, and refactoring can start from there.
I hope to move this so there is less code in the actual bridge very soon, but my goal is not to require any server packages in the frontend part of this

I think ideally:
- all activation code, including validation, should probably be part of the theme engine
- the theme engine should offer 3 methods: getActive() canActivate() and activate()
- the theme service is then only responsible for loading themes in and out of storage, JSON responses for the API, and handing themes to the frontend via the bridge at the appropriate moment
2021-04-27 13:52:19 +01:00

98 lines
4.4 KiB
JavaScript

const _ = require('lodash');
const debug = require('ghost-ignition').debug('themes');
const {i18n: commonI18n} = require('../proxy');
const logging = require('../../../shared/logging');
const errors = require('@tryghost/errors');
const themeLoader = require('./loader');
const bridge = require('../../../bridge');
const validate = require('./validate');
const list = require('./list');
const settingsCache = require('../../../server/services/settings/cache');
module.exports = {
// Init themes module
// TODO: move this once we're clear what needs to happen here
init: function initThemes() {
const activeThemeName = settingsCache.get('active_theme');
debug('init themes', activeThemeName);
// Just read the active theme for now
return themeLoader
.loadOneTheme(activeThemeName)
.then(function activeThemeHasLoaded(theme) {
// Validate
return validate
.check(theme)
.then(function validationSuccess(checkedTheme) {
if (!validate.canActivate(checkedTheme)) {
const checkError = new errors.ThemeValidationError({
message: commonI18n.t('errors.middleware.themehandler.invalidTheme', {theme: activeThemeName}),
errorDetails: Object.assign(
_.pick(checkedTheme, ['checkedVersion', 'name', 'path', 'version']), {
errors: checkedTheme.results.error
}
)
});
logging.error(checkError);
bridge.activateTheme(theme, checkedTheme, checkError);
} else {
// CASE: inform that the theme has errors, but not fatal (theme still works)
if (checkedTheme.results.error.length) {
logging.warn(new errors.ThemeValidationError({
errorType: 'ThemeWorksButHasErrors',
message: commonI18n.t('errors.middleware.themehandler.themeHasErrors', {theme: activeThemeName}),
errorDetails: Object.assign(
_.pick(checkedTheme, ['checkedVersion', 'name', 'path', 'version']), {
errors: checkedTheme.results.error
}
)
}));
}
debug('Activating theme (method A on boot)', activeThemeName);
bridge.activateTheme(theme, checkedTheme);
}
});
})
.catch(errors.NotFoundError, function (err) {
// CASE: active theme is missing, we don't want to exit because the admin panel will still work
err.message = commonI18n.t('errors.middleware.themehandler.missingTheme', {theme: activeThemeName});
logging.error(err);
})
.catch(function (err) {
// CASE: theme threw an odd error, we don't want to exit because the admin panel will still work
// This is the absolute catch-all, at this point, we do not know what went wrong!
logging.error(err);
});
},
getJSON: require('./to-json'),
activate: function (themeName) {
const loadedTheme = list.get(themeName);
if (!loadedTheme) {
return Promise.reject(new errors.ValidationError({
message: commonI18n.t('notices.data.validation.index.themeCannotBeActivated', {themeName: themeName}),
errorDetails: themeName
}));
}
return validate.checkSafe(loadedTheme)
.then((checkedTheme) => {
debug('Activating theme (method B on API "activate")', themeName);
bridge.activateTheme(loadedTheme, checkedTheme);
return checkedTheme;
});
},
storage: require('./storage'),
/**
* Load all inactive themes
*/
loadInactiveThemes: async () => {
return await themeLoader.loadAllThemes();
}
};