Skip to content

Instantly share code, notes, and snippets.

@marlun
Created March 29, 2011 06:52
Show Gist options
  • Save marlun/891911 to your computer and use it in GitHub Desktop.
Save marlun/891911 to your computer and use it in GitHub Desktop.
var fs = require('fs'),
path = require('path'),
glob = require('glob').glob;
exports = module.exports = function jam(options) {
options = options || {};
var patterns = options.patterns || [];
var root = options.root || path.join(__dirname, 'public');
var namespace = options.namespace || 'JST';
var output = options.output || path.join(root, 'jst.js');
var func = options.func || '_.template';
if (!options.root) throw new Error('jam() root path required');
if (!options.patterns) throw new Error('jam() root path required');
var files = [];
var compiled = '';
var name = '';
patterns.forEach(function(pattern) {
glob(path.join(root, pattern), function(err, paths) {
runs -= 1;
files = files.concat(paths);
if (runs === 0) {
files.forEach(function(file) {
name = file.replace(root, '');
fs.readFile(file, 'utf8', function(err, data) {
if (err) throw err;
data = data.replace('\n', '');
compiled += namespace + "[" + name + "] = " + func + "(" + data + ");";
});
});
}
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment