From 9d7049cd3f07d98b727134d36e3302a7edc9a376 Mon Sep 17 00:00:00 2001 From: Hannah Wolfe Date: Mon, 4 Oct 2021 16:50:07 +0100 Subject: [PATCH] Refactored helper registration code into a service - The helper registration code is "framework" code and very specific - At the moment the "theme engine" is full of lots of disparate theme related stuff - I'm trying to make the frontend framework code clearer and also expand it to make it more useful - The helper system now also exposes 3 methods allowing you to register a directory, a helper or an alias - I've updated the codebase to use these both for our core helpers and for "apps" --- core/boot.js | 17 +++++- core/frontend/apps/amp/index.js | 8 +-- core/frontend/apps/amp/lib/helpers/index.js | 18 ------- core/frontend/apps/private-blogging/index.js | 5 +- .../private-blogging/lib/helpers/index.js | 3 -- core/frontend/helpers/get.js | 4 +- core/frontend/helpers/ghost_head.js | 2 + core/frontend/helpers/prev_post.js | 5 +- core/frontend/services/apps/proxy.js | 9 ++-- .../register.js => helpers/handlebars.js} | 8 +-- core/frontend/services/helpers/index.js | 18 +++++++ core/frontend/services/helpers/registry.js | 45 ++++++++++++++++ core/frontend/services/rendering.js | 2 +- .../theme-engine/handlebars/helpers.js | 54 ------------------- .../theme-engine/handlebars/loader.js | 19 ------- core/frontend/services/theme-engine/index.js | 1 - core/server/web/site/app.js | 5 +- test/unit/services/apps/proxy.test.js | 13 ++--- .../theme-engine/handlebars/helpers.test.js | 4 +- 19 files changed, 116 insertions(+), 124 deletions(-) delete mode 100644 core/frontend/apps/amp/lib/helpers/index.js delete mode 100644 core/frontend/apps/private-blogging/lib/helpers/index.js rename core/frontend/services/{theme-engine/handlebars/register.js => helpers/handlebars.js} (83%) create mode 100644 core/frontend/services/helpers/index.js create mode 100644 core/frontend/services/helpers/registry.js delete mode 100644 core/frontend/services/theme-engine/handlebars/helpers.js delete mode 100644 core/frontend/services/theme-engine/handlebars/loader.js diff --git a/core/boot.js b/core/boot.js index 5f5b51746c..017a198658 100644 --- a/core/boot.js +++ b/core/boot.js @@ -111,10 +111,10 @@ async function initCore({ghostServer, config}) { async function initServicesForFrontend() { debug('Begin: initServicesForFrontend'); - debug('Begin: Frontend Routing Settings'); + debug('Begin: Routing Settings'); const routeSettings = require('./server/services/route-settings'); await routeSettings.init(); - debug('End: Frontend Routing Settings'); + debug('End: Routing Settings'); debug('Begin: Themes'); const themeService = require('./server/services/themes'); @@ -124,6 +124,18 @@ async function initServicesForFrontend() { debug('End: initServicesForFrontend'); } +/** + * Frontend is intended to be just Ghost's frontend + */ +async function initFrontend() { + debug('Begin: initFrontend'); + + const helperService = require('./frontend/services/helpers'); + await helperService.init(); + + debug('End: initFrontend'); +} + /** * At the moment we load our express apps all in one go, they require themselves and are co-located * What we want is to be able to optionally load various components and mount them @@ -331,6 +343,7 @@ async function bootGhost() { debug('Begin: Load Ghost Services & Apps'); await initCore({ghostServer, config}); await initServicesForFrontend(); + await initFrontend(); const ghostApp = await initExpressApps(); await initDynamicRouting(); await initServices({config}); diff --git a/core/frontend/apps/amp/index.js b/core/frontend/apps/amp/index.js index f385e38f2c..e4c3c530b7 100644 --- a/core/frontend/apps/amp/index.js +++ b/core/frontend/apps/amp/index.js @@ -1,5 +1,6 @@ +const path = require('path'); + const router = require('./lib/router'); -const registerHelpers = require('./lib/helpers'); const urlUtils = require('../../../shared/url-utils'); // Dirty requires @@ -22,7 +23,8 @@ module.exports = { let ampRoute = '*/amp/'; ghost.routeService.registerRouter(ampRoute, ampRouter); - - registerHelpers(ghost); + ghost.helperService.registerDir(path.resolve(__dirname, './lib/helpers')); + // we use the {{ghost_head}} helper, but call it {{amp_ghost_head}}, so it's consistent + ghost.helperService.registerAlias('amp_ghost_head', 'ghost_head'); } }; diff --git a/core/frontend/apps/amp/lib/helpers/index.js b/core/frontend/apps/amp/lib/helpers/index.js deleted file mode 100644 index f2d24ef879..0000000000 --- a/core/frontend/apps/amp/lib/helpers/index.js +++ /dev/null @@ -1,18 +0,0 @@ -// Dirty require! -const ghostHead = require('../../../../helpers/ghost_head'); - -function registerAmpHelpers(ghost) { - ghost.helpers.registerAsync('amp_content', require('./amp_content')); - - ghost.helpers.register('amp_components', require('./amp_components')); - - ghost.helpers.register('amp_analytics', require('./amp_analytics')); - - // we use the {{ghost_head}} helper, but call it {{amp_ghost_head}}, so it's consistent - ghost.helpers.registerAsync('amp_ghost_head', ghostHead); - - // additional injected styles for use inside the single