Skip to content

Instantly share code, notes, and snippets.

@gperetin
Created July 7, 2012 10:47
Show Gist options
  • Save gperetin/3065819 to your computer and use it in GitHub Desktop.
Save gperetin/3065819 to your computer and use it in GitHub Desktop.
Node.js script to compile Jade templates
/*
* This script takes 2 arguments, first is path to templates dir,
* and second is path to folder where to put compiled templates.js
*
* It scans templates dir, takes all .jade files and compiles them
* as Jade templates. Then it writes all template functions to
* single output file templates.js
*/
var fs = require('fs');
var jade = require('jade');
var templatesDir = process.argv[2];
var files = fs.readdirSync(templatesDir);
function isJadeTemplate(file) {
var extension = file.split('.').pop();
if (extension == "jade" ) {
return true;
}
return false;
}
function getFilename(file) {
return file.split(".")[0];
}
/*
* Main
*/
var functions = {};
var buffer = "";
for (var f in files) {
var file = files[f];
if (!isJadeTemplate(file)) continue;
contents = fs.readFileSync(templatesDir + file);
options = {
client:true,
compileDebug:false,
filename: templatesDir + file
}
compiledFunction = jade.compile(contents, options);
buffer += "var " + getFilename(file) + " = " + compiledFunction + "\n";
}
outputDir = process.argv[3];
fs.writeFileSync(outputDir + 'templates.js', buffer);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment