Skip to content

Commit

Permalink
Add Handlebars dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
tanem committed Dec 20, 2014
1 parent 6e6a5d2 commit 997e345
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
12 changes: 9 additions & 3 deletions lib/write_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@ var fs = require('fs');
var through = require('through2');
var mkdirp = require('mkdirp');

var headerWritten = false;

module.exports = function(helperFile, svgPath){
return through(function(chunk, enc, cb){
mkdirp(path.dirname(helperFile), function(err){
if (err) throw err;
var helper = '' +
'Handlebars.registerHelper(\'' + svgPath + '\', function(){\n' +
var str = '';
if (!headerWritten) {
str = 'var Handlebars = require(\'handlebars\');\n\n';
headerWritten = true;
}
str += 'Handlebars.registerHelper(\'' + svgPath + '\', function(){\n' +
' return \'' + chunk.toString() + '\';\n' +
'});\n\n';
fs.appendFile(helperFile, helper, function(err){
fs.appendFile(helperFile, str, function(err){
if (err) throw err;
cb(null, chunk);
});
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"bin": "bin/cmd.js",
"dependencies": {
"bufferstreams": "^0.0.2",
"handlebars": "^2.0.0",
"mkdirp": "^0.5.0",
"readdirp": "^1.1.0",
"svgo": "^0.5.0",
Expand Down Expand Up @@ -37,4 +38,4 @@
"url": "http://github.com/tanem"
},
"license": "MIT"
}
}
1 change: 1 addition & 0 deletions test/hbs-svg.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ test('should write the specified Handlebars helper file', function(t){
if (err) t.fail(err);
t.equal(
data.toString(),
'var Handlebars = require(\'handlebars\');\n\n' +
'Handlebars.registerHelper(\'newspaper.svg\', function(){\n' +
' return \'<svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 32 32"><path d="M28 8V4H0v22c0 1.105.895 2 2 2h27c1.657 0 3-1.343 3-3V8h-4zm-2 18H2V6h24v20zM4 10h20v2H4zm12 4h8v2h-8zm0 4h8v2h-8zm0 4h6v2h-6zM4 14h10v10H4z"/></svg>\';\n' +
'});\n\n' +
Expand Down

0 comments on commit 997e345

Please sign in to comment.