Skip to content

Instantly share code, notes, and snippets.

@chaosprinz
Forked from gperetin/jade_compile.js
Last active April 19, 2016 08:34
Show Gist options
  • Save chaosprinz/41ed41c3831b443aa058ad6c584a64bf to your computer and use it in GitHub Desktop.
Save chaosprinz/41ed41c3831b443aa058ad6c584a64bf 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 = "var template={};";
for (var f in files) {
var file = files[f];
if (!isJadeTemplate(file)) continue;
contents = fs.readFileSync(templatesDir + "/" + file);
options = {
compileDebug:false,
filename: templatesDir + file
}
compiledFunction = jade.compileClient(contents, options);
buffer += "template['" + getFilename(file) + "'] = " + compiledFunction + "\n";
}
outputFile = process.argv[3];
fs.writeFileSync(outputFile, buffer);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment