diff --git a/core/server/services/themes/activation-bridge.js b/core/server/services/themes/activation-bridge.js new file mode 100644 index 0000000000..0d0ad12553 --- /dev/null +++ b/core/server/services/themes/activation-bridge.js @@ -0,0 +1,21 @@ +const debug = require('@tryghost/debug')('themes'); +const bridge = require('../../../bridge'); + +/** + * These helper methods mean that the bridge is only required in one place + * And also adds a little debug statement, which is very handy when debugging theme logic + */ +module.exports = { + activateFromBoot: (themeName, theme, checkedTheme) => { + debug('Activating theme (method A on boot)', themeName); + bridge.activateTheme(theme, checkedTheme); + }, + activateFromAPI: (themeName, theme, checkedTheme) => { + debug('Activating theme (method B on API "activate")', themeName); + bridge.activateTheme(theme, checkedTheme); + }, + activateFromAPIOverride: (themeName, theme, checkedTheme) => { + debug('Activating theme (method C on API "override")', themeName); + bridge.activateTheme(theme, checkedTheme); + } +}; diff --git a/core/server/services/themes/index.js b/core/server/services/themes/index.js index 9c85caa960..d954df4b94 100644 --- a/core/server/services/themes/index.js +++ b/core/server/services/themes/index.js @@ -3,9 +3,9 @@ const logging = require('@tryghost/logging'); const errors = require('@tryghost/errors'); const tpl = require('@tryghost/tpl'); const themeLoader = require('./loader'); -const bridge = require('../../../bridge'); const validate = require('./validate'); const list = require('./list'); +const activator = require('./activation-bridge'); const settingsCache = require('../../../shared/settings-cache'); const messages = { @@ -34,8 +34,7 @@ module.exports = { logging.warn(validate.getThemeValidationError('activeThemeHasErrors', activeThemeName, checkedTheme)); } - debug('Activating theme (method A on boot)', activeThemeName); - bridge.activateTheme(theme, checkedTheme); + activator.activateFromBoot(activeThemeName, theme, 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 @@ -60,8 +59,7 @@ module.exports = { const checkedTheme = await validate.checkSafe(themeName, loadedTheme); - debug('Activating theme (method B on API "activate")', themeName); - bridge.activateTheme(loadedTheme, checkedTheme); + activator.activateFromAPI(themeName, loadedTheme, checkedTheme); return checkedTheme; }, diff --git a/core/server/services/themes/storage.js b/core/server/services/themes/storage.js index b15f943cef..fc4ff8ed5a 100644 --- a/core/server/services/themes/storage.js +++ b/core/server/services/themes/storage.js @@ -10,6 +10,7 @@ const validate = require('./validate'); const list = require('./list'); const ThemeStorage = require('./ThemeStorage'); const themeLoader = require('./loader'); +const activator = require('./activation-bridge'); const toJSON = require('./to-json'); const settingsCache = require('../../../shared/settings-cache'); @@ -79,8 +80,7 @@ module.exports = { overrideTheme = (shortName === settingsCache.get('active_theme')); // CASE: if this is the active theme, we are overriding if (overrideTheme) { - debug('Activating theme (method C, on API "override")', shortName); - bridge.activateTheme(loadedTheme, checkedTheme); + activator.activateFromOverride(shortName, loadedTheme, checkedTheme); } // @TODO: unify the name across gscan and Ghost!