Last active
August 29, 2015 14:23
-
-
Save jayfresh/fe8416ddce15b2c5038b to your computer and use it in GitHub Desktop.
hbs-utils using vinyl-fs for the watch method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// based on https://github.com/dpolivy/hbs-utils/blob/master/lib/hbs-utils.js | |
// modified to use vinyl-fs to watch files so it works with docker-mounted Windows volumes (possibly NFS) | |
var fs = require('fs'), | |
path = require('path'), | |
walk = require('../../node_modules/hbs-utils/node_modules/walk').walk, | |
watch = require('vinyl-fs').watch; // instead of node-watch | |
// Precompile a partial | |
var precompilePartialHelper = function(handlebars, partial) { | |
handlebars.partials[partial] = handlebars.compile(handlebars.partials[partial]); | |
}, | |
// Register the partial with handlebars directly | |
registerPartialHelper = function(handlebars, directory, filepath, opts, done) { | |
var regex = (typeof opts.match === 'string') ? new RegExp(opts.match) : opts.match || /\.(html|hbs)$/, | |
isValidTemplate = regex.test(filepath); | |
if (!isValidTemplate) { | |
return done(null); | |
} | |
fs.readFile(filepath, 'utf8', function(err, data) { | |
if (!err) { | |
var ext = path.extname(filepath); | |
var templateName = path.relative(directory, filepath) | |
.slice(0, -(ext.length)).replace(/[ -]/g, '_'); | |
if (typeof opts.name === 'function') templateName = opts.name(templateName); | |
handlebars.registerPartial(templateName, data); | |
return done(undefined, templateName); | |
} | |
done(err); | |
}); | |
}; | |
// Constructor | |
var Instance = function(hbs) { | |
this.hbs = hbs; | |
}; | |
// Precompile all partials | |
Instance.prototype.precompilePartials = function() { | |
var handlebars = this.hbs.handlebars, | |
partials = handlebars.partials; | |
for (var partial in partials) { | |
if (typeof partials[partial] === 'string') { | |
precompilePartialHelper(handlebars, partial); | |
} | |
} | |
}; | |
// Register all partials in a given directory | |
Instance.prototype.registerPartials = function(directory, opts, done) { | |
if (Object.prototype.toString.call(opts) === "[object Function]") { | |
done = opts; | |
opts = {}; | |
} | |
opts = opts || {}; | |
var handlebars = this.hbs.handlebars; | |
// Load all partials in the specified directory | |
walk(directory) | |
.on('file', function(root, stat, next) { | |
registerPartialHelper(handlebars, directory, path.join(root, stat.name), opts, function(err, template) { | |
if (opts.precompile && template) precompilePartialHelper(handlebars, template); | |
next(err); | |
}); | |
}) | |
.on('end', done || function() {}); | |
}; | |
// Register all partials in a given directory, and watch for changes to those | |
// files. When there is a change, register that updated file. | |
Instance.prototype.registerWatchedPartials = function(directory, opts, done) { | |
if (Object.prototype.toString.call(opts) === "[object Function]") { | |
done = opts; | |
opts = {}; | |
} | |
opts = opts || {}; | |
var handlebars = this.hbs.handlebars; | |
// Load all partials in a given directory | |
this.registerPartials(directory, opts, function() { | |
// Now, setup the watcher on the directory | |
// using glob as we are using vinyl-fs watcher | |
watch(directory+'/{*.hbs,*.html}', function(e) { | |
// filename comes from property of first argument | |
var filename = e.path; | |
console.log('changed', filename); | |
registerPartialHelper(handlebars, directory, filename, opts, function(err, template) { | |
if (opts.precompile && template) precompilePartialHelper(handlebars, template); | |
if (opts.onchange && template) opts.onchange(template); | |
}); | |
}); | |
if (done) done(); | |
}); | |
}; | |
// Require a reference to the hbs object to initialize the instance | |
module.exports = function(hbs) { | |
return new Instance(hbs); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment