Skip to content

Instantly share code, notes, and snippets.

@ialpert
Created January 10, 2012 13:46
Show Gist options
  • Save ialpert/1589154 to your computer and use it in GitHub Desktop.
Save ialpert/1589154 to your computer and use it in GitHub Desktop.
DustJS templates compiler into single file
var
path = require("path"),
fs = require("fs"),
dust_path = path.join(__dirname, "/lib/dust"),
tpls_path = path.join(__dirname, "../js/app"),
parser = require(path.join(dust_path, "/parser")),
compiler = require(path.join(dust_path, "/compiler")),
tpls = [],
compiled = "",
compiled_file = path.join(__dirname, "../js/templates.build.js");
compiler.parse = parser.parse;
function walk(filename, callback)
{
fs.stat(filename, function(err, stats)
{
if (stats.isFile() && filename.match(/\.js$/)) {
// Filename - do callback
callback(filename);
} else if (stats.isDirectory()) {
// Directory - walk recursive
fs.readdir(filename, function(err, files)
{
for (var i = 0; i < files.length; i++) {
walk(filename + '/' + files[i], callback);
}
});
}
});
}
var traverseFileSystem = function(currentPath)
{
var files = fs.readdirSync(currentPath);
for (var i in files) {
var currentFile = currentPath + '/' + files[i];
var stats = fs.statSync(currentFile);
if (stats.isFile()) {
if (path.extname(currentFile) === ".html") {
tpls.push(currentFile);
}
}
else if (stats.isDirectory()) {
traverseFileSystem(currentFile);
}
}
};
traverseFileSystem(tpls_path);
tpls.forEach(function(file)
{
var name = path.basename(file, ".html");
tpl = fs.readFileSync(file, "utf-8");
compiled += compiler.compile(tpl, name) + "\n";
console.log("processed " + name);
});
fs.writeFileSync(compiled_file, compiled);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment