From 1dbb78c53f63c7bb29520ea3547af3e5aaaa6ac9 Mon Sep 17 00:00:00 2001 From: hacksalot Date: Thu, 1 Feb 2018 07:00:59 -0500 Subject: [PATCH] feat: improve custom theme helper registration --- dist/helpers/handlebars-helpers.js | 23 ++++++++++++++-------- src/helpers/handlebars-helpers.coffee | 28 +++++++++++++-------------- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/dist/helpers/handlebars-helpers.js b/dist/helpers/handlebars-helpers.js index df0ac87..982c231 100644 --- a/dist/helpers/handlebars-helpers.js +++ b/dist/helpers/handlebars-helpers.js @@ -27,7 +27,7 @@ Template helper definitions for Handlebars. */ module.exports = function(theme, opts) { - var curGlob, ex, glob, wrappedHelpers; + var curGlob, ex, glob, slash, wrappedHelpers; helpers.theme = theme; helpers.opts = opts; helpers.type = 'handlebars'; @@ -50,18 +50,25 @@ Template helper definitions for Handlebars. } if (_.isArray(theme.helpers)) { glob = require('glob'); + slash = require('slash'); curGlob = null; try { _.each(theme.helpers, function(fGlob) { + var files; curGlob = fGlob; fGlob = path.join(theme.folder, fGlob); - glob(fGlob, {}, function(er, files) { - if (er === null && files.length > 0) { - _.each(files, function(f) { - HANDLEBARS.registerHelper(require(f)); - }); - } - }); + files = glob.sync(slash(fGlob)); + if (files.length > 0) { + _.each(files, function(f) { + HANDLEBARS.registerHelper(require(f)); + }); + } else { + throw { + fluenterror: HMS.themeHelperLoad, + inner: er, + glob: fGlob + }; + } }); } catch (_error) { ex = _error; diff --git a/src/helpers/handlebars-helpers.coffee b/src/helpers/handlebars-helpers.coffee index 1b77084..e4ea69d 100644 --- a/src/helpers/handlebars-helpers.coffee +++ b/src/helpers/handlebars-helpers.coffee @@ -43,24 +43,24 @@ module.exports = ( theme, opts ) -> if _.isArray theme.helpers glob = require 'glob' + slash = require 'slash' curGlob = null try _.each theme.helpers, (fGlob) -> # foreach theme.helpers entry - curGlob = fGlob # cache in case of exception - fGlob = path.join theme.folder, fGlob # make relative to theme - glob fGlob, { }, (er, files) -> # expand the glob to paths - if er is null and files.length > 0 # guard against the error - _.each files, (f) -> # loop over concrete paths - HANDLEBARS.registerHelper require f # register the path - return - # else if er # glob error occurred - # throw fluenterror: HMS.themeHelperLoad, inner: er, glob: fGlob - # else if files.length < 1 # glob returned no results - # throw fluenterror: HMS.themeHelperLoad - return + curGlob = fGlob # ..cache in case of exception + fGlob = path.join theme.folder, fGlob # ..make relative to theme + files = glob.sync slash fGlob # ..expand the glob + if files.length > 0 # ..guard against empty glob + _.each files, (f) -> # ..loop over concrete paths + HANDLEBARS.registerHelper require f # ..register the path + return + else + throw fluenterror: HMS.themeHelperLoad, inner: er, glob: fGlob return return catch ex - # If a non-path is passed to glob() it will throw an error - throw fluenterror: HMS.themeHelperLoad, inner: ex, glob: curGlob, exit: true + throw + fluenterror: HMS.themeHelperLoad + inner: ex + glob: curGlob, exit: true return