keithamus/hbs-cli

add parameters to accept third party helpers

Closed this issue · 4 comments

Can we add one more parameter to accept third party helpers such as Swag and the popular helpers, such as
hbs --extraHelper 'swag' --data data.json template.mustache ?

Hey @jhcao23 thanks for the issue!

IIRC --helper can be used multiple times, what is the issue with calling hbs --helper swag? Does this not do what you expect?

Hey @keithamus Can --helper support the whole library? I assume this option only supports one single helper function, doesn't it?

Looking at the code for hbs-cli, it duck-types to see if there is a ".register" function and simply calls that.

hbs-cli/src/index.js

Lines 46 to 52 in feacd1c

const handlebarsHelper = require(file); // eslint-disable-line global-require
if (handlebarsHelper && typeof handlebarsHelper.register === 'function') {
debug(`${file} has a register function, registering with handlebars`);
handlebarsHelper.register(Handlebars);
} else {
console.error(`WARNING: ${file} does not export a 'register' function, cannot import`);
}

Looking through the docs for Swag it expects you to call Swag.registerHelpers. Our code could be modified to handle this case but it seems non standard?

Looking through the docs for helpers, it seems that the module is a function that you call like so: require('handlebars-helpers')({ handlebars: handlebars }). Again this is non-standard.


To get this working today you could write a simple wrapper helper which looks something like this:

// myhelpers.js
const swag = require('swag')
const helpers = require('handlebars-helpers')

module.exports = {
  register(handlebars) {
    Swag.registerHelpers(handlebars)
    helpers({ handlebars })
  }
}

Then simply call hbs --helpers myhelpers --data data.json template.mustache


Another option would be to raise Issues/PRs against these project and ask them to support the defacto-standard of a register(handlebars) function.

beautiful!