From f69b52c2a28e47ac4da3413f547735a1c9f50548 Mon Sep 17 00:00:00 2001 From: Christopher Martin Date: Sat, 23 Apr 2016 13:45:29 -0400 Subject: [PATCH] feat(): custom extensions --- .eslintrc.js | 4 ++-- README.md | 37 +++++++++++++++++++++++++++++++++---- bin/envhandlebars | 15 ++------------- index.js | 27 ++++++++++++++++++++++++++- package.json | 2 +- test/index.spec.js | 27 ++++++++++++++++++++++++++- 6 files changed, 90 insertions(+), 22 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 0ca8620..70f8122 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -128,7 +128,7 @@ module.exports = { "no-param-reassign": "off", "no-path-concat": "error", "no-plusplus": "error", - "no-process-env": "error", + "no-process-env": "off", "no-process-exit": "error", "no-proto": "error", "no-restricted-globals": "error", @@ -175,7 +175,7 @@ module.exports = { "prefer-reflect": "off", "prefer-rest-params": "off", "prefer-spread": "error", - "prefer-template": "error", + "prefer-template": "off", "quote-props": "off", "quotes": "off", "radix": "error", diff --git a/README.md b/README.md index e9ad42c..fd71675 100644 --- a/README.md +++ b/README.md @@ -9,8 +9,8 @@ A simple templating utility, akin to [`envsubst`](http://linuxcommand.org/man_pa Environment variables are used as the data input to a Handlebars template: ``` -$ export WORLD=world -$ echo "Hello {{WORLD}}" | envhandlebars +$ export NAME=world +$ echo "Hello {{NAME}}" | envhandlebars Hello world ``` @@ -50,7 +50,8 @@ Output: Hello world! ``` -See the [if block helper](http://handlebarsjs.com/builtin_helpers.html#conditionals) for more information. +See the [if block helper](http://handlebarsjs.com/builtin_helpers.html#conditionals) docs page for more information. + ### Iterators @@ -99,7 +100,8 @@ Output: Chris Martin, John Papa, Shayne Boyer! ``` -See the [each block helper](http://handlebarsjs.com/builtin_helpers.html#iteration) for more information. +See the [each block helper](http://handlebarsjs.com/builtin_helpers.html#iteration) docs page for more information. + ## Docker Usage @@ -136,6 +138,33 @@ For alpine-based images: RUN apk add --update nodejs && npm i -g envhandlebars ``` +## Custom Helpers or Partials + +**(New in v1.3.0+)** + +Custom Mustache helpers and partials can be implemented by extending the `envhandlebars` module with your own Node.js wrapper script: + +```javascript +#!/usr/bin/env node +// Script: `myenvhandlebars` +'use strict'; +var envhandlebars = require('envhandlebars'); + +// The Handlebars context is passed into this function +// for registering helpers, partials or other extensions. +function extendHandlebars(Handlebars) { + Handlebars.registerHelper('fullName', function(first, last) { + return last + ', ' + first; + }); +} + +envhandlebars({ + extendHandlebars: extendHandlebars +}); +``` + +See the [custom helpers](http://handlebarsjs.com/#helpers) docs page for more information. + ## License [MIT License](http://cgm.mit-license.org/) diff --git a/bin/envhandlebars b/bin/envhandlebars index cfc3738..6a5b5f1 100755 --- a/bin/envhandlebars +++ b/bin/envhandlebars @@ -1,20 +1,9 @@ #!/usr/bin/env node /* eslint no-process-env: 0, no-process-exit: 0, no-console: 0 */ 'use strict'; -var bin = require('../index.js'); +var envhandlebars = require('../index.js'); // Provide a title to the process in `ps` process.title = 'envhandlebars'; -bin({ - env: process.env, - stdin: process.stdin, - stdout: process.stdout, - stderr: process.stderr, - argv: process.argv -}, function(err) { - if (err) { - console.error(err); - process.exit(1); - } -}); +envhandlebars(); diff --git a/index.js b/index.js index 61b8d3b..e782e3f 100644 --- a/index.js +++ b/index.js @@ -3,8 +3,33 @@ var concat = require('concat-stream'); var Handlebars = require('handlebars'); var util = require('util'); -module.exports = function bin(opts, cb) { +module.exports = function envhandlebars(opts, cb) { + // Defaults + if (!cb && typeof opts === 'function') { + cb = opts; + opts = {}; + } + opts = opts || {}; + opts.env = opts.env || process.env; + opts.exit = opts.exit || process.exit; + opts.stdin = opts.stdin || process.stdin; + opts.stdout = opts.stdout || process.stdout; + opts.stderr = opts.stderr || process.stderr; + opts.argv = opts.argv || process.argv; + + cb = cb || function(err) { + if (err) { + opts.stderr.write(util.format.call(null, err)); + opts.exit(1); + } + }; + + // Allow helpers or other extensions to be applied + if (typeof opts.extendHandlebars === 'function') { + opts.extendHandlebars(Handlebars); + } + // Stream stdin through handlebars template procesing opts.stdin.setEncoding('utf8'); opts.stdin.on('error', handleError); opts.stdin.pipe(concat(applyTemplate)); diff --git a/package.json b/package.json index 714c2bd..b6dd8c2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "envhandlebars", - "version": "1.2.1", + "version": "1.3.0", "description": "Simple handlebars templating for configuration files using environment variables", "main": "index.js", "scripts": { diff --git a/test/index.spec.js b/test/index.spec.js index 6dff9ae..a1c8dca 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -16,7 +16,7 @@ describe('envhandlebars', function() { argv = [ 'node', path.join(__dirname, "../bin/envhandlebars") - ] + ]; }); describe('basic expressions', function () { @@ -128,4 +128,29 @@ describe('envhandlebars', function() { }); }); }); + + describe('handlebars extensions', function () { + it('should register helper', function (done) { + var stdin = new stream.ReadableStream( + "Hello {{fullName FIRST_NAME LAST_NAME}}" + ); + function registerHelpers(Handlebars) { + Handlebars.registerHelper('fullName', function(first, last) { + return last + ', ' + first; + }); + } + + fixture({ + env: { FIRST_NAME: 'first', LAST_NAME: 'last' }, + stdin: stdin, stdout: stdout, stderr: stderr, + argv: argv, + extendHandlebars: registerHelpers + }, function (err) { + assert.ifError(err); + assert.equal(stdout.toString(), 'Hello last, first'); + done(); + }); + }); + }); + });