Last active
December 19, 2015 21:59
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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