keithamus / hbs-cli

A CLI for handlebars
43 stars 26 forks source link

add parameters to accept third party helpers #55

Closed jhcao23 closed 5 years ago

jhcao23 commented 5 years ago

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?

keithamus commented 5 years ago

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?

jhcao23 commented 5 years ago

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

keithamus commented 5 years ago

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

https://github.com/keithamus/hbs-cli/blob/feacd1c0d6a0ee5c7ea0465c8356ca1789af71c5/src/index.js#L46-L52

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.

jhcao23 commented 5 years ago

beautiful!