diff --git a/dist/cli/error.js b/dist/cli/error.js index 431c051..d8d1c28 100644 --- a/dist/cli/error.js +++ b/dist/cli/error.js @@ -61,7 +61,7 @@ Error-handling routines for HackMyResume. stack = ex.stack || (ex.inner && ex.inner.stack); stack && o(chalk.gray(stack)); } - if (shouldExit) { + if (shouldExit || ex.exit) { if (this.debug) { o(chalk.cyan('Exiting with error code ' + ex.fluenterror.toString())); } @@ -262,6 +262,10 @@ Error-handling routines for HackMyResume. case HMSTATUS.unknownSchema: msg = M2C(this.msgs.unknownSchema.msg[0]); etype = 'error'; + break; + case HMSTATUS.themeHelperLoad: + msg = printf(M2C(this.msgs.themeHelperLoad.msg), ex.glob); + etype = 'error'; } return { msg: msg, diff --git a/dist/cli/msg.yml b/dist/cli/msg.yml index b2d9ca2..b7fe231 100644 --- a/dist/cli/msg.yml +++ b/dist/cli/msg.yml @@ -132,3 +132,8 @@ errors: "basics": { "name": "John Doe" } + themeHelperLoad: + msg: >- + An error occurred while attempting to load the '%s' theme helper. Is the + theme correctly installed? + dummy: dontcare diff --git a/dist/core/status-codes.js b/dist/core/status-codes.js index 4cd2be7..4eedca5 100644 --- a/dist/core/status-codes.js +++ b/dist/core/status-codes.js @@ -36,7 +36,8 @@ Status codes for HackMyResume. validateError: 26, invalidOptionsFile: 27, optionsFileNotFound: 28, - unknownSchema: 29 + unknownSchema: 29, + themeHelperLoad: 30 }; }).call(this); diff --git a/dist/helpers/handlebars-helpers.js b/dist/helpers/handlebars-helpers.js index ddca0c7..df0ac87 100644 --- a/dist/helpers/handlebars-helpers.js +++ b/dist/helpers/handlebars-helpers.js @@ -6,7 +6,7 @@ Template helper definitions for Handlebars. */ (function() { - var HANDLEBARS, _, blockHelpers, helpers; + var HANDLEBARS, HMS, _, blockHelpers, helpers, path; HANDLEBARS = require('handlebars'); @@ -14,8 +14,12 @@ Template helper definitions for Handlebars. helpers = require('./generic-helpers'); + path = require('path'); + blockHelpers = require('./block-helpers'); + HMS = require('../core/status-codes'); + /** Register useful Handlebars helpers. @@ -23,7 +27,7 @@ Template helper definitions for Handlebars. */ module.exports = function(theme, opts) { - var i, len, ref, themeHelpers, wrappedHelpers; + var curGlob, ex, glob, wrappedHelpers; helpers.theme = theme; helpers.opts = opts; helpers.type = 'handlebars'; @@ -41,10 +45,33 @@ Template helper definitions for Handlebars. }, this); HANDLEBARS.registerHelper(wrappedHelpers); HANDLEBARS.registerHelper(blockHelpers); - ref = theme.jsFiles; - for (i = 0, len = ref.length; i < len; i++) { - themeHelpers = ref[i]; - HANDLEBARS.registerHelper(require(themeHelpers)); + if (_.isString(theme.helpers)) { + theme.helpers = [theme.helpers]; + } + if (_.isArray(theme.helpers)) { + glob = require('glob'); + curGlob = null; + try { + _.each(theme.helpers, function(fGlob) { + 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)); + }); + } + }); + }); + } catch (_error) { + ex = _error; + throw { + fluenterror: HMS.themeHelperLoad, + inner: ex, + glob: curGlob, + exit: true + }; + } } }; diff --git a/src/cli/error.coffee b/src/cli/error.coffee index d130616..815902e 100644 --- a/src/cli/error.coffee +++ b/src/cli/error.coffee @@ -57,7 +57,7 @@ ErrorHandler = module.exports = stack && o( chalk.gray( stack ) ); # Quit if necessary - if shouldExit + if shouldExit or ex.exit if @debug o chalk.cyan('Exiting with error code ' + ex.fluenterror.toString()) if @assert @@ -253,6 +253,11 @@ assembleError = ( ex ) -> #msg += "\n" + M2C( @msgs.unknownSchema.msg[1], 'yellow' ) etype = 'error' + when HMSTATUS.themeHelperLoad + msg = printf M2C( @msgs.themeHelperLoad.msg ), ex.glob + etype = 'error' + + msg: msg # The error message to display withStack: withStack # Whether to include the stack quit: quit diff --git a/src/cli/msg.yml b/src/cli/msg.yml index b2d9ca2..b7fe231 100644 --- a/src/cli/msg.yml +++ b/src/cli/msg.yml @@ -132,3 +132,8 @@ errors: "basics": { "name": "John Doe" } + themeHelperLoad: + msg: >- + An error occurred while attempting to load the '%s' theme helper. Is the + theme correctly installed? + dummy: dontcare diff --git a/src/core/status-codes.coffee b/src/core/status-codes.coffee index 992d90b..8252796 100644 --- a/src/core/status-codes.coffee +++ b/src/core/status-codes.coffee @@ -36,3 +36,4 @@ module.exports = invalidOptionsFile: 27 optionsFileNotFound: 28 unknownSchema: 29 + themeHelperLoad: 30 diff --git a/src/helpers/handlebars-helpers.coffee b/src/helpers/handlebars-helpers.coffee index d8a13f0..1b77084 100644 --- a/src/helpers/handlebars-helpers.coffee +++ b/src/helpers/handlebars-helpers.coffee @@ -8,7 +8,9 @@ Template helper definitions for Handlebars. HANDLEBARS = require 'handlebars' _ = require 'underscore' helpers = require './generic-helpers' +path = require 'path' blockHelpers = require './block-helpers' +HMS = require '../core/status-codes' ###* Register useful Handlebars helpers. @@ -33,6 +35,32 @@ module.exports = ( theme, opts ) -> HANDLEBARS.registerHelper wrappedHelpers HANDLEBARS.registerHelper blockHelpers - for themeHelpers in theme.jsFiles - HANDLEBARS.registerHelper require themeHelpers - return + + # Register any theme-provided custom helpers... + + # Normalize "theme.helpers" (string or array) to an array + theme.helpers = [ theme.helpers ] if _.isString theme.helpers + + if _.isArray theme.helpers + glob = require 'glob' + 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 + 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 + return