mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-26 12:21:36 +03:00
4da7e7f0cb
refs: https://github.com/TryGhost/Team/issues/831 - This ultimately fixes the index.js file - It also makes it super clear what methods in the themeService are used by the API, and which are part of the service loading logic - It also moves the activate and init function into a single file in a way that highlights they are very similar - They are also very similar to what happens in storage.setFromZip but that code is mixed up with storage code at the moment
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));
|
|
}
|
|
|
|
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
|
|
activator.activateFromAPI(themeName, loadedTheme, checkedTheme);
|
|
// Return the checked theme
|
|
return checkedTheme;
|
|
};
|