Permalink
Browse files

Extract client CS compile to a helper function

  • Loading branch information...
1 parent 53a06ca commit 227b2b5a7b22a41a8021001ed7094a6fac14e614 Andrew Appleton committed Nov 25, 2011
Showing with 15 additions and 8 deletions.
  1. +3 −7 app.coffee
  2. +12 −1 helpers/helpers.coffee
View
@@ -87,13 +87,9 @@ ipWhitelist = (req, res, next) ->
# Routes
app.get('/javascripts/:resource.js', (req, res) ->
- # Compile and serve client side CoffeeScript on the fly
- filePath = "./public/javascripts/#{req.params.resource}.coffee"
- fs.readFile(filePath, 'utf-8', (err, data) ->
- res.writeHead(200, {'Content-Type': 'application/javascript'})
- res.write(coffee.compile(data))
- res.end()
- )
+ resource = req.params.resource
+ helpers.compile resource, (compiled) ->
+ res.contentType('application/javascript').send(compiled)
)
app.get('/', basicAuth, (req, res) ->
View
@@ -1,3 +1,5 @@
+fs = require('fs')
+
exports.discretify = (name, substitutes) ->
discreteName = name
if substitutes
@@ -6,4 +8,13 @@ exports.discretify = (name, substitutes) ->
discreteName = substitutes[sub].subs
return discreteName
- return discreteName
+ return discreteName
+
+exports.compile = (resource, callback) ->
+ # Takes a CoffeeScript resource name and runs a callback with compiled
+ # JavaScript as the first argument
+ filePath = "./public/javascripts/#{resource}.coffee"
+ fs.readFile filePath, 'utf-8', (err, data) ->
+ compiled = coffee.compile(data)
+ callback(compiled)
+

0 comments on commit 227b2b5

Please sign in to comment.