mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-21 01:41:46 +03:00
1708f0c3a4
closes https://github.com/TryGhost/Team/issues/1172 Custom theme settings sync and cache population had been left to complete in the background as it wasn't essential for it to be complete for the front-end to start. However that was causing problems for the API where theme activation and custom theme settings list requests happen very close together, with the latter often not containing the theme settings data when it is expected to. - changed `activationBridge.*` methods to `async` so they can `await` the completion of custom theme settings sync before activating a theme
63 lines
2.4 KiB
JavaScript
63 lines
2.4 KiB
JavaScript
const debug = require('@tryghost/debug')('themes');
|
|
|
|
const errors = require('@tryghost/errors');
|
|
const logging = require('@tryghost/logging');
|
|
const tpl = require('@tryghost/tpl');
|
|
|
|
const activator = require('./activation-bridge');
|
|
const list = require('./list');
|
|
const themeLoader = require('./loader');
|
|
const validate = require('./validate');
|
|
|
|
const messages = {
|
|
activeThemeIsMissing: 'The currently active theme "{theme}" is missing.',
|
|
themeCannotBeActivated: '{themeName} cannot be activated because it was not found in the theme directory.'
|
|
};
|
|
|
|
module.exports.loadAndActivate = async (themeName) => {
|
|
debug('loadAndActivate', themeName);
|
|
try {
|
|
// Just read the active theme for now
|
|
const loadedTheme = await themeLoader.loadOneTheme(themeName);
|
|
// Validate
|
|
// @NOTE: this is now the only usage of check, rather than checkSafe...
|
|
const checkedTheme = await validate.check(loadedTheme);
|
|
|
|
if (!validate.canActivate(checkedTheme)) {
|
|
logging.error(validate.getThemeValidationError('activeThemeHasFatalErrors', themeName, checkedTheme));
|
|
} else if (checkedTheme.results.error.length) {
|
|
// CASE: inform that the theme has errors, but not fatal (theme still works)
|
|
logging.warn(validate.getThemeValidationError('activeThemeHasErrors', themeName, checkedTheme));
|
|
}
|
|
|
|
await activator.activateFromBoot(themeName, loadedTheme, checkedTheme);
|
|
} catch (err) {
|
|
if (err instanceof errors.NotFoundError) {
|
|
// CASE: active theme is missing, we don't want to exit because the admin panel will still work
|
|
err.message = tpl(messages.activeThemeIsMissing, {theme: themeName});
|
|
}
|
|
|
|
// 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);
|
|
}
|
|
};
|
|
|
|
module.exports.activate = async (themeName) => {
|
|
const loadedTheme = list.get(themeName);
|
|
|
|
if (!loadedTheme) {
|
|
throw new errors.ValidationError({
|
|
message: tpl(messages.themeCannotBeActivated, {themeName: themeName}),
|
|
errorDetails: themeName
|
|
});
|
|
}
|
|
|
|
// Validate
|
|
const checkedTheme = await validate.checkSafe(themeName, loadedTheme);
|
|
// Activate
|
|
await activator.activateFromAPI(themeName, loadedTheme, checkedTheme);
|
|
// Return the checked theme
|
|
return checkedTheme;
|
|
};
|