Created
March 3, 2012 02:28
-
-
Save codejoust/1963846 to your computer and use it in GitHub Desktop.
simple static site compiler for node
This file contains hidden or 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
var styluscomp = require('stylus'), | |
jadecomp = require('jade'), | |
fs = require('fs'); | |
function jade(data, cb, filename){ | |
cb(false, jadecomp.compile(data, { pretty: true, filename: filename })()); | |
} | |
function styl(data, cb, filename){ | |
styluscomp.render(data, { pretty: true, filename: filename }, cb); | |
} | |
function compiler(compfn, file_in, file_out, err_cb){ | |
return function(){ | |
var cb = function(err, data){ | |
if (!data || err){ | |
err_cb(err); | |
} else { | |
fs.writeFileSync(file_out, data); | |
console.log('Compiled: ' + file_in); | |
} } | |
fs.readFile(file_in, 'utf8', {filename: file_in, pretty: true}, function(err, data){ | |
if (!err){ | |
compfn(data, cb, file_in); | |
} else { err_cb(err); } | |
}); } } | |
function wrap_cb(cbfn){ | |
return function(curr, prev){ | |
if (curr.mtime.getTime() != prev.mtime.getTime()){ | |
console.log('Found Change.'); | |
cbfn(curr); | |
} } } | |
function watch(file, fileout, compfn){ | |
var err = function(err){ | |
console.log('Compilation Error: ' + file); | |
console.log(' ' + err); } | |
var comp_runner = compiler(compfn, file, fileout, err); | |
fs.watchFile(file, function(cur_stat, prev_stat){ | |
if (cur_stat.mtime.getTime() != prev_stat.mtime.getTime()){ | |
comp_runner(); | |
} }); | |
console.log('Starting compiler for: ' + file); | |
comp_runner() } | |
// watch these files. | |
watch('index.jade', 'pub/index.html', jade); | |
watch('contact.jade', 'pub/contact/index.html', jade); | |
watch('contact-base.jade', 'pub/contact/base.html', jade); | |
watch('site.styl', 'pub/css/site.css', styl); | |
watch('contact.styl', 'pub/css/contact.css', styl); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment