mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-22 02:11:44 +03:00
9f11140ec4
- one big file full of stuff is never good for clarity - separating it out helps us see what requires what - it also highlights the awful naming and opaque behaviour we have in themes - much to do, but this helps us start
35 lines
1.3 KiB
JavaScript
35 lines
1.3 KiB
JavaScript
const errors = require('@tryghost/errors');
|
|
const tpl = require('@tryghost/tpl');
|
|
|
|
const activeTheme = require('../active');
|
|
const settingsCache = require('../../../../shared/settings-cache');
|
|
|
|
const messages = {
|
|
missingTheme: 'The currently active theme "{theme}" is missing.'
|
|
};
|
|
|
|
// ### Ensure Active Theme
|
|
// Ensure there's a properly set & mounted active theme before attempting to serve a site request
|
|
// If there is no active theme, throw an error
|
|
// Else, ensure the active theme is mounted
|
|
function ensureActiveTheme(req, res, next) {
|
|
// CASE: this means that the theme hasn't been loaded yet i.e. there is no active theme
|
|
if (!activeTheme.get()) {
|
|
// This is the one place we ACTUALLY throw an error for a missing theme as it's a request we cannot serve
|
|
return next(new errors.InternalServerError({
|
|
// We use the settingsCache here, because the setting will be set,
|
|
// even if the theme itself is not usable because it is invalid or missing.
|
|
message: tpl(messages.missingTheme, {theme: settingsCache.get('active_theme')})
|
|
}));
|
|
}
|
|
|
|
// If the active theme has not yet been mounted, mount it into express
|
|
if (!activeTheme.get().mounted) {
|
|
activeTheme.get().mount(req.app);
|
|
}
|
|
|
|
next();
|
|
}
|
|
|
|
module.exports = ensureActiveTheme;
|