Skip to content

Commit

Permalink
Merge pull request #5 from cgmartin/helpers
Browse files Browse the repository at this point in the history
feat(): Custom Handlebars extensions
  • Loading branch information
cgmartin committed Apr 23, 2016
2 parents e81157e + f69b52c commit 15eb0a3
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 22 deletions.
4 changes: 2 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
37 changes: 33 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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/)
15 changes: 2 additions & 13 deletions bin/envhandlebars
Original file line number Diff line number Diff line change
@@ -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();
27 changes: 26 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
27 changes: 26 additions & 1 deletion test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('envhandlebars', function() {
argv = [
'node',
path.join(__dirname, "../bin/envhandlebars")
]
];
});

describe('basic expressions', function () {
Expand Down Expand Up @@ -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();
});
});
});

});

0 comments on commit 15eb0a3

Please sign in to comment.