Skip to content

Instantly share code, notes, and snippets.

@mattnull
Last active December 19, 2015 21:59
Show Gist options
  • Save mattnull/6024365 to your computer and use it in GitHub Desktop.
Save mattnull/6024365 to your computer and use it in GitHub Desktop.
Part of my main Cakefile that pre-compiles Handlebars templates. This script watches the specified directory for changes and writes to templates.js
fs = require 'fs'
{print} = require 'sys'
{log, error} = console; print = log
{spawn, exec} = require 'child_process'
run = (name, args...) ->
proc = spawn(name, args)
proc.stdout.on('data', (buffer) -> print buffer if buffer = buffer.toString().trim())
proc.stderr.on('data', (buffer) -> error buffer if buffer = buffer.toString().trim())
proc.on 'exit', (status) ->
process.exit(1) if status isnt 0
task 'watch', 'Watch template dir for changes and compile ', () ->
# pre-compile client-side templates
templatesDir = 'public/js/templates/src'
compileHandlebars = (template) ->
run 'handlebars', templatesDir, '-f', 'public/js/templates/templates.js'
run 'uglifyjs', 'public/js/templates/templates.js', '-o','public/js/templates/templates.js'
# watch client side templates
templates = fs.readdirSync templatesDir
for i in [0...templates.length]
template = templatesDir + '/' + templates[i]
do (template) ->
fs.watchFile template, (curr, prev) ->
if +curr.mtime isnt +prev.mtime
compileHandlebars(template)
compileHandlebars()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment