Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

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 under DIR, are those produced by jade. This will not work if you are mixing jade and html. When the start task is run, all jade files under DIR are compiled to html files (in the same folder as the jade file). Then chokidar will look for any further jade file changes, while also starting meteor. If a file is added or updated, the corresponding html file will be generated. If a jade file is removed, the corresponding html file will be removed. Any syntax errors that jade encounters will be printed to the screen with line numbers, but will not halt execution of the server.

View Cakefile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
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
Something went wrong with that request. Please try again.