2021-06-15 19:01:22 +03:00
|
|
|
const debug = require('@tryghost/debug')('themes');
|
2021-06-15 17:36:27 +03:00
|
|
|
const logging = require('@tryghost/logging');
|
2020-05-22 21:22:20 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
2021-07-06 16:54:02 +03:00
|
|
|
const tpl = require('@tryghost/tpl');
|
2019-01-28 20:06:47 +03:00
|
|
|
const themeLoader = require('./loader');
|
|
|
|
const validate = require('./validate');
|
2019-07-09 17:35:18 +03:00
|
|
|
const list = require('./list');
|
2021-07-07 15:58:47 +03:00
|
|
|
const activator = require('./activation-bridge');
|
2021-06-30 16:56:57 +03:00
|
|
|
const settingsCache = require('../../../shared/settings-cache');
|
2019-01-28 20:06:47 +03:00
|
|
|
|
2021-07-06 16:54:02 +03:00
|
|
|
const messages = {
|
2021-07-07 15:40:10 +03:00
|
|
|
activeThemeIsMissing: 'The currently active theme "{theme}" is missing.',
|
2021-07-07 15:49:40 +03:00
|
|
|
themeCannotBeActivated: '{themeName} cannot be activated because it was not found in the theme directory.'
|
2021-07-06 16:54:02 +03:00
|
|
|
};
|
|
|
|
|
2017-02-22 02:26:19 +03:00
|
|
|
module.exports = {
|
2017-03-13 19:30:35 +03:00
|
|
|
// Init themes module
|
|
|
|
// TODO: move this once we're clear what needs to happen here
|
2021-07-07 15:49:40 +03:00
|
|
|
init: async () => {
|
2020-04-29 18:44:27 +03:00
|
|
|
const activeThemeName = settingsCache.get('active_theme');
|
2017-03-13 23:13:17 +03:00
|
|
|
|
2017-03-13 19:30:35 +03:00
|
|
|
debug('init themes', activeThemeName);
|
2021-07-07 15:49:40 +03:00
|
|
|
try {
|
|
|
|
// Just read the active theme for now
|
|
|
|
const theme = await themeLoader.loadOneTheme(activeThemeName);
|
|
|
|
// Validate
|
|
|
|
// @NOTE: this is now the only usage of check, rather than checkSafe...
|
|
|
|
const checkedTheme = await validate.check(theme);
|
2021-07-07 15:09:00 +03:00
|
|
|
|
2021-07-07 15:49:40 +03:00
|
|
|
if (!validate.canActivate(checkedTheme)) {
|
|
|
|
logging.error(validate.getThemeValidationError('activeThemeHasFatalErrors', activeThemeName, 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', activeThemeName, checkedTheme));
|
|
|
|
}
|
2021-07-06 16:54:02 +03:00
|
|
|
|
2021-07-07 15:58:47 +03:00
|
|
|
activator.activateFromBoot(activeThemeName, theme, checkedTheme);
|
2021-07-07 15:49:40 +03:00
|
|
|
} 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: activeThemeName});
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
2017-03-13 19:30:35 +03:00
|
|
|
},
|
2019-07-09 17:35:18 +03:00
|
|
|
getJSON: require('./to-json'),
|
2021-07-07 15:49:40 +03:00
|
|
|
activate: async (themeName) => {
|
2019-07-09 17:35:18 +03:00
|
|
|
const loadedTheme = list.get(themeName);
|
2019-01-03 22:30:35 +03:00
|
|
|
|
2019-07-01 17:56:23 +03:00
|
|
|
if (!loadedTheme) {
|
2021-07-07 15:49:40 +03:00
|
|
|
throw new errors.ValidationError({
|
2021-07-06 16:54:02 +03:00
|
|
|
message: tpl(messages.themeCannotBeActivated, {themeName: themeName}),
|
2019-07-01 17:56:23 +03:00
|
|
|
errorDetails: themeName
|
2021-07-07 15:49:40 +03:00
|
|
|
});
|
2017-10-11 16:19:12 +03:00
|
|
|
}
|
2019-07-01 17:56:23 +03:00
|
|
|
|
2021-07-07 15:49:40 +03:00
|
|
|
const checkedTheme = await validate.checkSafe(themeName, loadedTheme);
|
2019-07-01 17:56:23 +03:00
|
|
|
|
2021-07-07 15:58:47 +03:00
|
|
|
activator.activateFromAPI(themeName, loadedTheme, checkedTheme);
|
2021-07-07 15:49:40 +03:00
|
|
|
|
|
|
|
return checkedTheme;
|
2017-03-21 12:03:09 +03:00
|
|
|
},
|
2019-07-09 17:35:18 +03:00
|
|
|
storage: require('./storage'),
|
2021-02-22 20:30:25 +03:00
|
|
|
/**
|
|
|
|
* Load all inactive themes
|
|
|
|
*/
|
|
|
|
loadInactiveThemes: async () => {
|
|
|
|
return await themeLoader.loadAllThemes();
|
|
|
|
}
|
2017-02-22 02:26:19 +03:00
|
|
|
};
|