Skip to content

Instantly share code, notes, and snippets.

@tomas-stefano
Last active December 15, 2015 08:28
Show Gist options
  • Save tomas-stefano/5230680 to your computer and use it in GitHub Desktop.
Save tomas-stefano/5230680 to your computer and use it in GitHub Desktop.
Simple Handlebars Compiler in command line.
module.exports = function(grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
handlebars: {
all: {
src: ["app/assets/javascripts/templates"],
dest: "app/assets/javascripts/templates/all.js"
}
}
});
grunt.registerMultiTask('handlebars', 'Precompile Handlebars template', function() {
var done = this.async();
var compiler = new HandlebarsCompiler({
sources: this.data['src'],
destination: this.data['dest'],
verbose: this.data['verbose']
});
compiler.compile();
done();
});
};
var exec = require('child_process').exec;
HandlebarsCompiler = function(options) {
this.sources = options.sources || [];
this.destination = options.destination;
this.extension = options.extension || '*.hbs';
this.verbose = options.verbose || false;
this.compile = function() {
var compiler = this;
if (this.sources == null || this.destination == null) {
if (this.verbose) {
console.log('Need to pass the sources and destination to compiler.');
}
return false;
}
this.sources.forEach(function(source) {
var filePattern = __dirname + '/' + source + '/' + compiler.extension;
var command = 'handlebars ' + filePattern + ' -f ' + compiler.destination;
if (compiler.verbose) {
console.log('Running command: ' + command);
}
exec(command, function(err, stdout, stderr) {
if (err) {
compiler.error = true;
compiler.stderr = stderr;
return false;
}
});
});
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment