Skip to content

Instantly share code, notes, and snippets.

@dweldon
Last active December 11, 2015 18:49
Show Gist options
  • Save dweldon/4644690 to your computer and use it in GitHub Desktop.
Save dweldon/4644690 to your computer and use it in GitHub Desktop.
This is a Cakefile for automatically compiling jade templates to html files in an arbitrarily deep directory tree. It was intended for use with meteor, however you can use it with any framework by changing the start task and the DIR. NOTE: this requires that chokidar and jade be installed via npm. NOTE: this assumes that the only html files unde…
fs = require 'fs'
path = require 'path'
chokidar = require 'chokidar'
{spawn} = require 'child_process'
DIR = path.join __dirname, 'client'
isType = (file, type) ->
path.extname(file) is '.' + type
findFiles = (dir, type, files = {}) ->
for filename in fs.readdirSync dir
file = path.join dir, filename
stats = fs.statSync file
if stats.isDirectory()
findFiles file, type, files
else if isType filename, type
files[file] = true
Object.keys files
runJade = (files, cb = ->) ->
stdio = ['ignore', 'ignore', process.stderr]
(spawn 'jade', files, {stdio}).on 'exit', cb
jadeDir = (dir, cb = ->) ->
runJade findFiles(dir, 'jade'), cb
watchDir = (dir) ->
watcher = chokidar.watch dir
watcher.on 'add', (file) ->
runJade [file] if isType file, 'jade'
watcher.on 'change', (file) ->
runJade [file] if isType file, 'jade'
watcher.on 'unlink', (file) ->
if isType file, 'jade'
htmlFile = file[0...-4] + 'html'
if fs.existsSync htmlFile
fs.unlinkSync htmlFile
task 'start', 'start the server', ->
jadeDir DIR, ->
watchDir DIR
spawn 'meteor', [], stdio: 'inherit'
task 'jade', 'compile jade files', ->
jadeDir DIR
task 'clean', 'remove html files', ->
for file in findFiles DIR, 'html'
fs.unlinkSync file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment