From 59b9f161dd1ba25adc9bf5f109bfacf3d1a5222c Mon Sep 17 00:00:00 2001 From: Hannah Wolfe Date: Wed, 8 Apr 2020 19:02:31 +0100 Subject: [PATCH] Moved non-helper code out of helpers - the helper dir also contained some code used with helpers - utils and helper-helpers? - the goal here was for helpers to be the only thing in their folder so we can look at moving them out - all other code has been moved to services/themes for now, which is not the right place either - services/themes is a catch-all for theme storage, loading, validation, rendering and more, needs to be broken down --- core/frontend/helpers/index.js | 51 ------------------ core/frontend/helpers/link.js | 4 +- core/frontend/helpers/link_class.js | 4 +- core/frontend/services/apps/proxy.js | 2 +- core/frontend/services/proxy.js | 4 +- .../services/themes/handlebars/helpers.js | 53 +++++++++++++++++++ .../themes/handlebars}/register.js | 4 +- .../themes/handlebars}/template.js | 4 +- .../themes/handlebars}/utils.js | 0 core/frontend/services/themes/index.js | 3 +- core/server/web/site/app.js | 2 +- test/unit/services/apps/proxy_spec.js | 2 +- .../themes/handlebars/helpers_spec.js} | 10 ++-- .../themes/handlebars}/template_spec.js | 6 +-- 14 files changed, 77 insertions(+), 72 deletions(-) create mode 100644 core/frontend/services/themes/handlebars/helpers.js rename core/frontend/{helpers => services/themes/handlebars}/register.js (90%) rename core/frontend/{helpers => services/themes/handlebars}/template.js (91%) rename core/frontend/{helpers => services/themes/handlebars}/utils.js (100%) rename test/unit/{helpers/index_spec.js => services/themes/handlebars/helpers_spec.js} (86%) rename test/unit/{helpers => services/themes/handlebars}/template_spec.js (63%) diff --git a/core/frontend/helpers/index.js b/core/frontend/helpers/index.js index f6b4185d7c..a949aa6761 100644 --- a/core/frontend/helpers/index.js +++ b/core/frontend/helpers/index.js @@ -1,10 +1,4 @@ -const register = require('./register'); - const coreHelpers = {}; -const registerThemeHelper = register.registerThemeHelper; -const registerAsyncThemeHelper = register.registerAsyncThemeHelper; - -let registerAllCoreHelpers; coreHelpers.asset = require('./asset'); coreHelpers.author = require('./author'); @@ -45,49 +39,4 @@ coreHelpers.title = require('./title'); coreHelpers.twitter_url = require('./twitter_url'); coreHelpers.url = require('./url'); -registerAllCoreHelpers = function registerAllCoreHelpers() { - // Register theme helpers - registerThemeHelper('asset', coreHelpers.asset); - registerThemeHelper('author', coreHelpers.author); - registerThemeHelper('authors', coreHelpers.authors); - registerThemeHelper('body_class', coreHelpers.body_class); - registerThemeHelper('cancel_link', coreHelpers.cancel_link); - registerThemeHelper('concat', coreHelpers.concat); - registerThemeHelper('content', coreHelpers.content); - registerThemeHelper('date', coreHelpers.date); - registerThemeHelper('encode', coreHelpers.encode); - registerThemeHelper('excerpt', coreHelpers.excerpt); - registerThemeHelper('foreach', coreHelpers.foreach); - registerThemeHelper('ghost_foot', coreHelpers.ghost_foot); - registerThemeHelper('has', coreHelpers.has); - registerThemeHelper('is', coreHelpers.is); - registerThemeHelper('img_url', coreHelpers.img_url); - registerThemeHelper('lang', coreHelpers.lang); - registerThemeHelper('link', coreHelpers.link); - registerThemeHelper('link_class', coreHelpers.link_class); - registerThemeHelper('meta_description', coreHelpers.meta_description); - registerThemeHelper('meta_title', coreHelpers.meta_title); - registerThemeHelper('navigation', coreHelpers.navigation); - registerThemeHelper('page_url', coreHelpers.page_url); - registerThemeHelper('pagination', coreHelpers.pagination); - registerThemeHelper('plural', coreHelpers.plural); - registerThemeHelper('post_class', coreHelpers.post_class); - registerThemeHelper('price', coreHelpers.price); - registerThemeHelper('raw', coreHelpers.raw); - registerThemeHelper('reading_time', coreHelpers.reading_time); - registerThemeHelper('t', coreHelpers.t); - registerThemeHelper('tags', coreHelpers.tags); - registerThemeHelper('title', coreHelpers.title); - registerThemeHelper('twitter_url', coreHelpers.twitter_url); - registerThemeHelper('facebook_url', coreHelpers.facebook_url); - registerThemeHelper('url', coreHelpers.url); - - // Async theme helpers - registerAsyncThemeHelper('ghost_head', coreHelpers.ghost_head); - registerAsyncThemeHelper('next_post', coreHelpers.next_post); - registerAsyncThemeHelper('prev_post', coreHelpers.prev_post); - registerAsyncThemeHelper('get', coreHelpers.get); -}; - module.exports = coreHelpers; -module.exports.loadCoreHelpers = registerAllCoreHelpers; diff --git a/core/frontend/helpers/link.js b/core/frontend/helpers/link.js index 54a2fcd684..b1da160f7c 100644 --- a/core/frontend/helpers/link.js +++ b/core/frontend/helpers/link.js @@ -1,7 +1,7 @@ // # link helper const _ = require('lodash'); -const {config, SafeString, errors, i18n} = require('../services/proxy'); -const {buildLinkClasses} = require('./utils'); +const {config, SafeString, errors, i18n, localUtils} = require('../services/proxy'); +const {buildLinkClasses} = localUtils; const managedAttributes = ['href', 'class', 'activeClass', 'parentActiveClass']; diff --git a/core/frontend/helpers/link_class.js b/core/frontend/helpers/link_class.js index 2ba6d2aedf..a9f30846d3 100644 --- a/core/frontend/helpers/link_class.js +++ b/core/frontend/helpers/link_class.js @@ -1,7 +1,7 @@ // # link_class helper const _ = require('lodash'); -const {config, SafeString, errors, i18n} = require('../services/proxy'); -const {buildLinkClasses} = require('./utils'); +const {config, SafeString, errors, i18n, localUtils} = require('../services/proxy'); +const {buildLinkClasses} = localUtils; module.exports = function link_class(options) { // eslint-disable-line camelcase options = options || {}; diff --git a/core/frontend/services/apps/proxy.js b/core/frontend/services/apps/proxy.js index 52a2e0cdc0..ca4d6a2e12 100644 --- a/core/frontend/services/apps/proxy.js +++ b/core/frontend/services/apps/proxy.js @@ -1,4 +1,4 @@ -const helpers = require('../../helpers/register'); +const helpers = require('../../services/themes/handlebars/register'); const routingService = require('../../services/routing'); module.exports.getInstance = function getInstance() { diff --git a/core/frontend/services/proxy.js b/core/frontend/services/proxy.js index f6cff22527..d4d46ea29c 100644 --- a/core/frontend/services/proxy.js +++ b/core/frontend/services/proxy.js @@ -57,12 +57,12 @@ module.exports = { }, // The local template thing, should this be merged with the channels one? - templates: require('../helpers/template'), + templates: require('./themes/handlebars/template'), // Various utils, needs cleaning up / simplifying socialUrls: require('@tryghost/social-urls'), blogIcon: require('../../server/lib/image/blog-icon'), urlService: require('./url'), urlUtils: require('../../server/lib/url-utils'), - localUtils: require('../helpers/utils') + localUtils: require('./themes/handlebars/utils') }; diff --git a/core/frontend/services/themes/handlebars/helpers.js b/core/frontend/services/themes/handlebars/helpers.js new file mode 100644 index 0000000000..f738506a5c --- /dev/null +++ b/core/frontend/services/themes/handlebars/helpers.js @@ -0,0 +1,53 @@ +const register = require('./register'); +const coreHelpers = require('../../../helpers'); +const registerThemeHelper = register.registerThemeHelper; +const registerAsyncThemeHelper = register.registerAsyncThemeHelper; + +let registerAllCoreHelpers; + +registerAllCoreHelpers = function registerAllCoreHelpers() { + // Register theme helpers + registerThemeHelper('asset', coreHelpers.asset); + registerThemeHelper('author', coreHelpers.author); + registerThemeHelper('authors', coreHelpers.authors); + registerThemeHelper('body_class', coreHelpers.body_class); + registerThemeHelper('cancel_link', coreHelpers.cancel_link); + registerThemeHelper('concat', coreHelpers.concat); + registerThemeHelper('content', coreHelpers.content); + registerThemeHelper('date', coreHelpers.date); + registerThemeHelper('encode', coreHelpers.encode); + registerThemeHelper('excerpt', coreHelpers.excerpt); + registerThemeHelper('foreach', coreHelpers.foreach); + registerThemeHelper('ghost_foot', coreHelpers.ghost_foot); + registerThemeHelper('has', coreHelpers.has); + registerThemeHelper('is', coreHelpers.is); + registerThemeHelper('img_url', coreHelpers.img_url); + registerThemeHelper('lang', coreHelpers.lang); + registerThemeHelper('link', coreHelpers.link); + registerThemeHelper('link_class', coreHelpers.link_class); + registerThemeHelper('meta_description', coreHelpers.meta_description); + registerThemeHelper('meta_title', coreHelpers.meta_title); + registerThemeHelper('navigation', coreHelpers.navigation); + registerThemeHelper('page_url', coreHelpers.page_url); + registerThemeHelper('pagination', coreHelpers.pagination); + registerThemeHelper('plural', coreHelpers.plural); + registerThemeHelper('post_class', coreHelpers.post_class); + registerThemeHelper('price', coreHelpers.price); + registerThemeHelper('raw', coreHelpers.raw); + registerThemeHelper('reading_time', coreHelpers.reading_time); + registerThemeHelper('t', coreHelpers.t); + registerThemeHelper('tags', coreHelpers.tags); + registerThemeHelper('title', coreHelpers.title); + registerThemeHelper('twitter_url', coreHelpers.twitter_url); + registerThemeHelper('facebook_url', coreHelpers.facebook_url); + registerThemeHelper('url', coreHelpers.url); + + // Async theme helpers + registerAsyncThemeHelper('ghost_head', coreHelpers.ghost_head); + registerAsyncThemeHelper('next_post', coreHelpers.prev_post); + registerAsyncThemeHelper('prev_post', coreHelpers.prev_post); + registerAsyncThemeHelper('get', coreHelpers.get); +}; + +module.exports = coreHelpers; +module.exports.loadCoreHelpers = registerAllCoreHelpers; diff --git a/core/frontend/helpers/register.js b/core/frontend/services/themes/handlebars/register.js similarity index 90% rename from core/frontend/helpers/register.js rename to core/frontend/services/themes/handlebars/register.js index b401caf835..67e67fc4a7 100644 --- a/core/frontend/helpers/register.js +++ b/core/frontend/services/themes/handlebars/register.js @@ -1,5 +1,7 @@ const Promise = require('bluebird'); -const {config, hbs, errors, logging} = require('../services/proxy'); +const hbs = require('../engine'); +const config = require('../../../../server/config'); +const {errors, logging} = require('../../../../server/lib/common'); // Register an async handlebars helper for a given handlebars instance function asyncHelperWrapper(hbs, name, fn) { diff --git a/core/frontend/helpers/template.js b/core/frontend/services/themes/handlebars/template.js similarity index 91% rename from core/frontend/helpers/template.js rename to core/frontend/services/themes/handlebars/template.js index 0c9d5476ac..9dd922f882 100644 --- a/core/frontend/helpers/template.js +++ b/core/frontend/services/themes/handlebars/template.js @@ -2,8 +2,8 @@ const templates = {}; const _ = require('lodash'); const errors = require('@tryghost/errors'); -const hbs = require('../services/themes/engine'); -const i18n = require('../../server/lib/common/i18n'); +const hbs = require('../engine'); +const {i18n} = require('../../../../server/lib/common'); // Execute a template helper // All template helpers are register as partial view. diff --git a/core/frontend/helpers/utils.js b/core/frontend/services/themes/handlebars/utils.js similarity index 100% rename from core/frontend/helpers/utils.js rename to core/frontend/services/themes/handlebars/utils.js diff --git a/core/frontend/services/themes/index.js b/core/frontend/services/themes/index.js index 3bea4bde91..c21084a3fa 100644 --- a/core/frontend/services/themes/index.js +++ b/core/frontend/services/themes/index.js @@ -105,5 +105,6 @@ module.exports = { }); }, storage: require('./storage'), - middleware: require('./middleware') + middleware: require('./middleware'), + loadCoreHelpers: require('./handlebars/helpers').loadCoreHelpers }; diff --git a/core/server/web/site/app.js b/core/server/web/site/app.js index ca43a8e668..581038335d 100644 --- a/core/server/web/site/app.js +++ b/core/server/web/site/app.js @@ -130,7 +130,7 @@ module.exports = function setupSiteApp(options = {}) { // We do this here, at the top level, because helpers require so much stuff. // Moving this to being inside themes, where it probably should be requires the proxy to be refactored // Else we end up with circular dependencies - require('../../../frontend/helpers').loadCoreHelpers(); + themeService.loadCoreHelpers(); debug('Helpers done'); // Members middleware diff --git a/test/unit/services/apps/proxy_spec.js b/test/unit/services/apps/proxy_spec.js index cea8e495ee..ff95c3aaac 100644 --- a/test/unit/services/apps/proxy_spec.js +++ b/test/unit/services/apps/proxy_spec.js @@ -1,6 +1,6 @@ const should = require('should'), sinon = require('sinon'), - helpers = require('../../../../core/frontend/helpers/register'), + helpers = require('../../../../core/frontend/services/themes/handlebars/register'), AppProxy = require('../../../../core/frontend/services/apps/proxy'), routing = require('../../../../core/frontend/services/routing'); diff --git a/test/unit/helpers/index_spec.js b/test/unit/services/themes/handlebars/helpers_spec.js similarity index 86% rename from test/unit/helpers/index_spec.js rename to test/unit/services/themes/handlebars/helpers_spec.js index 40e2f2c18c..015b18f810 100644 --- a/test/unit/helpers/index_spec.js +++ b/test/unit/services/themes/handlebars/helpers_spec.js @@ -1,9 +1,9 @@ -var should = require('should'), - _ = require('lodash'), - hbs = require('../../../core/frontend/services/themes/engine'), +const should = require('should'); +const _ = require('lodash'); +const hbs = require('../../../../../core/frontend/services/themes/engine'); - // Stuff we are testing - helpers = require('../../../core/frontend/helpers'); +// Stuff we are testing +const helpers = require('../../../../../core/frontend/helpers'); describe('Helpers', function () { var hbsHelpers = ['each', 'if', 'unless', 'with', 'helperMissing', 'blockHelperMissing', 'log', 'lookup', 'block', 'contentFor'], diff --git a/test/unit/helpers/template_spec.js b/test/unit/services/themes/handlebars/template_spec.js similarity index 63% rename from test/unit/helpers/template_spec.js rename to test/unit/services/themes/handlebars/template_spec.js index dbd1fff477..ece2189e62 100644 --- a/test/unit/helpers/template_spec.js +++ b/test/unit/services/themes/handlebars/template_spec.js @@ -1,6 +1,6 @@ -var should = require('should'), - hbs = require('../../../core/frontend/services/themes/engine'), - template = require('../../../core/frontend/helpers/template'); +const should = require('should'); +const hbs = require('../../../../../core/frontend/services/themes/engine'); +const template = require('../../../../../core/frontend/services/themes/handlebars/template'); describe('Helpers Template', function () { it('can execute a template', function () {