Skip to content

Commit

Permalink
Merge pull request #3 from SpiderStrategies/cache-jade-templates
Browse files Browse the repository at this point in the history
Caches the template files when running in production mode.
  • Loading branch information
Nathan Bowser committed Aug 24, 2012
2 parents 321fee1 + 929fabe commit 2ae86da
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 22 deletions.
32 changes: 23 additions & 9 deletions lib/index.js
Expand Up @@ -7,20 +7,22 @@ var jade = require('jade')
module.exports = Balloon

function Balloon(config) {
var cache = {}
this.transport = nodemailer.createTransport("SES", config.auth)
_.defaults(config, {
templateDirectory: path.join(__dirname, 'templates')
})

this.config = config
this.config.cache = config.cache || process.NODE_ENV === 'production'
this.cache = config.cache && {}
}

Balloon.prototype.send = function (template, model, params, callback) {
var opts = {
locals: model
}
var self = this
this.compile(template, opts, function (err, result) {
this.renderFile(template, opts, function (err, result) {
if (err) {
// don't know yet
} else {
Expand All @@ -34,12 +36,24 @@ Balloon.prototype.send = function (template, model, params, callback) {
})
}

Balloon.prototype.compile = function (template, opts, callback) {
Balloon.prototype.compile = function (tpl, model, callback) {
var template = jade.compile(tpl)
callback(null, template(model))
}

Balloon.prototype.renderFile = function (template, opts, callback) {
var ext = path.extname(template) || '.jade'
var t = path.join(this.config.templateDirectory, path.basename(template, ext) + ext)
fs.readFile(t, 'utf8', function (err, data) {
// TODO: handle error
var template = jade.compile(data)
callback(err, template(opts.locals))
})
var name = path.join(this.config.templateDirectory, path.basename(template, ext) + ext)
var self = this

if (self.cache && self.cache[name]) {
self.compile(self.cache[name], opts.locals, callback)
} else {
fs.readFile(name, 'utf8', function (err, data) {
self.cache[name] = data
// TODO: handle error
if (err) callback(err, null)
else self.compile(data, opts.locals, callback)
})
}
}
64 changes: 51 additions & 13 deletions test/index.js
Expand Up @@ -3,22 +3,13 @@ var mocha = require('mocha')
, Balloon = require('../lib')
, assert = require('assert')
, path = require('path')

balloon = new Balloon({
templateDirectory: path.join(__dirname, 'templates'),
auth: {
AWSAccessKeyID: argv.key,
AWSSecretKey: argv.secret
}
})
, fs = require('fs')

describe('balloon', function () {
var balloon = undefined
var model = { users: { Linus: { type: 'Boxer' }}}
it('should send a templated email', function (done) {
balloon.send('hello', {
users: {
Linus: { type: 'Boxer' }
}
}, {
balloon.send('hello', model, {
from: argv.from,
to: argv.to,
subject: 'Balloon templated test'
Expand All @@ -29,4 +20,51 @@ describe('balloon', function () {
done()
})
})

it('should cache templates in production', function (done) {
process.NODE_ENV = 'production'
balloon = new Balloon({
templateDirectory: path.join(__dirname, 'templates')
})

balloon.renderFile('hello', { locals: model }, function (err, result) {
assert(result)
assert(balloon.cache)
assert(balloon.cache[path.join(__dirname, 'templates', 'hello.jade')])

// Make sure it doesn't read again
var read = false
var readFile = fs.readFile
fs.readFile = function () {
read = true
}
balloon.renderFile('hello', { locals: model }, function (err, second) {
assert(!read)
assert.equal(result, second)

process.NODE_ENV = 'test'

fs.readFile = readFile
done()
})
})
})

it('should read a fresh template when not in production', function (done) {
balloon.renderFile('hello', { locals: { users: { Linus: {type: 'Boxer' }}}}, function (err, result) {
assert(result)
assert(!balloon.cache)
done()
})
})

beforeEach(function () {
balloon = new Balloon({
templateDirectory: path.join(__dirname, 'templates'),
auth: {
AWSAccessKeyID: argv.key,
AWSSecretKey: argv.secret
}
})
})
})

0 comments on commit 2ae86da

Please sign in to comment.