mirror of
https://github.com/bower/bower.git
synced 2026-01-15 01:08:07 -05:00
39 lines
1.2 KiB
JavaScript
39 lines
1.2 KiB
JavaScript
// ==========================================
|
|
// BOWER: Hogan Renderer w/ template cache
|
|
// ==========================================
|
|
// Copyright 2012 Twitter, Inc
|
|
// Licensed under The MIT License
|
|
// http://opensource.org/licenses/MIT
|
|
// ==========================================
|
|
|
|
var events = require('events');
|
|
var hogan = require('hogan.js');
|
|
var path = require('path');
|
|
var fs = require('fs');
|
|
|
|
var colors = require('../util/hogan-colors');
|
|
|
|
var templates = {};
|
|
|
|
module.exports = function (name, context, sync) {
|
|
var emitter = new events.EventEmitter;
|
|
|
|
var templateName = name + '.mustache';
|
|
var templatePath = path.join(__dirname, '../../templates/', templateName);
|
|
|
|
if (sync) {
|
|
if (!templates[templatePath]) templates[templatePath] = fs.readFileSync(templatePath, 'utf-8');
|
|
return hogan.compile(templates[templatePath]).renderWithColors(context);
|
|
} else if (templates[templatePath]) {
|
|
process.nextTick(function () {
|
|
emitter.emit('data', hogan.compile(templates[templatePath]).renderWithColors(context));
|
|
});
|
|
} else {
|
|
fs.readFile(templatePath, 'utf-8', function (err, file) {
|
|
templates[templatePath] = file;
|
|
emitter.emit('data', hogan.compile(file).renderWithColors(context));
|
|
});
|
|
}
|
|
|
|
return emitter;
|
|
}; |