Skip to content

Instantly share code, notes, and snippets.

@enriclluelles
Created February 14, 2012 00:59
Show Gist options
  • Select an option

  • Save enriclluelles/1822163 to your computer and use it in GitHub Desktop.

Select an option

Save enriclluelles/1822163 to your computer and use it in GitHub Desktop.
var jade = require('jade')
, srcDir = __dirname + '/public/templates'
, dstPath = __dirname + '/public/js/templates.js'
, util = require('util')
, fs = require('fs');
function writeTemplates (templates) {
var out = '(function () {\n'
out += 'this.JST = this.JST || {};\n';
out += '' + templates;
out += '\n})()';
fs.writeFile(dstPath, out, 'utf-8', function (err) {
if (err)
return console.log('Error compiling: ' + err);
return console.log('Done!');
});
}
(function () {
var out, srcPath, rootName, templates = [];
fs.readdir(srcDir, function (err, files) {
files.forEach(function (file) {
srcPath = srcDir + '/' + file;
fs.readFile(srcPath, 'utf-8', function (err, content) {
rootName = file.replace('.jade', '');
out = 'JST["' + rootName + '"] = ';
out += jade.compile(content, {client: true, self: true, compileDebug: false});
templates.push(out);
if (Object.keys(templates).length === files.length)
writeTemplates(templates.join('; \n'));
});
});
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment