Created
July 10, 2012 12:54
-
-
Save jay3sh/3083080 to your computer and use it in GitHub Desktop.
Watch script to compile Coffeescript, Jade and Less.css
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
/* | |
* Tested on node 0.6, Ubuntu | |
*/ | |
#!/usr/bin/node | |
COLOR_GREEN="\033[01;32m" | |
COLOR_RED="\033[01;31m" | |
COLOR_YELLOW="\033[01;33m" | |
COLOR_BLUE="\033[01;34m" | |
COLOR_NORMAL="\033[00m" | |
var fs = require('fs'); | |
var coffeescript, jade, less; | |
try { | |
coffeescript = require('/usr/lib/node_modules/coffee-script'); // after npm -g install coffee-script | |
} catch(e) { | |
console.log(COLOR_RED,String(e),COLOR_NORMAL); | |
} | |
try { | |
jade = require('/usr/lib/node_modules/jade'); // after npm -g install jade | |
} catch(e) { | |
console.log(COLOR_RED,String(e),COLOR_NORMAL); | |
} | |
try { | |
less = require('/usr/lib/node_modules/less'); // after npm -g install less | |
} catch(e) { | |
console.log(COLOR_RED,String(e),COLOR_NORMAL); | |
} | |
console.log( | |
'Watch Support', | |
(coffeescript ? COLOR_GREEN : COLOR_RED),' coffeescript ',COLOR_NORMAL, | |
(jade ? COLOR_GREEN : COLOR_RED),' jade ',COLOR_NORMAL, | |
(less ? COLOR_GREEN : COLOR_RED),' less ',COLOR_NORMAL | |
); | |
var compile = function (fname) { | |
var source = fs.readFileSync(fname).toString(); | |
try { | |
if(/\.jade$/.test(fname)) { | |
var html = jade.compile(source,{pretty:true})(); | |
var htmlfname = fname.replace(/\.jade$/,'.html'); | |
fs.writeFileSync(htmlfname, html); | |
console.log(COLOR_GREEN,'Success ',fname,new Date(),COLOR_NORMAL) | |
} else if(/\.coffee$/.test(fname)) { | |
var js = coffeescript.compile(source); | |
var jsfname = fname.replace(/\.coffee$/,'.js'); | |
fs.writeFileSync(jsfname, js); | |
console.log(COLOR_GREEN,'Success ',fname,new Date(),COLOR_NORMAL) | |
} else if(/\.less$/.test(fname)) { | |
var lessparser = new(less.Parser); | |
var cssfname = fname.replace(/\.less$/,'.css'); | |
lessparser.parse(source, function (e, tree) { | |
if(e) { | |
console.log(COLOR_RED,'Failed ',fname,String(e),COLOR_NORMAL); | |
} else { | |
fs.writeFileSync(cssfname, tree.toCSS()); | |
console.log(COLOR_GREEN,'Success ',fname,new Date(),COLOR_NORMAL) | |
} | |
}); | |
} | |
} catch(e) { | |
console.log(COLOR_RED,'Failed ',fname,String(e),COLOR_NORMAL) | |
} | |
} | |
var walk = function(dir, done) { | |
var results = []; | |
fs.readdir(dir, function(err, list) { | |
if (err) return done(err); | |
var pending = list.length; | |
if (!pending) return done(null, results); | |
list.forEach(function(file) { | |
file = dir + '/' + file; | |
fs.stat(file, function(err, stat) { | |
if (stat && stat.isDirectory()) { | |
walk(file, function(err, res) { | |
results = results.concat(res); | |
if (!--pending) done(null, results); | |
}); | |
} else { | |
results.push(file); | |
if (!--pending) done(null, results); | |
} | |
}); | |
}); | |
}); | |
}; | |
function main() { | |
if(process.argv.length > 2) { | |
dirName = process.argv[2]; | |
} else { | |
dirName = '.'; | |
} | |
stats = fs.statSync(dirName); | |
if(stats.isDirectory()) { | |
walk(dirName,function (err, files) { | |
var watching = 0; | |
files.forEach(function (fname) { | |
if(/\.coffee$|\.less$|\.jade$/.test(fname)) { | |
fs.watchFile(fname,{persistent:true,interval:500}, | |
function (cur,prev) { | |
if(cur.mtime.getTime() != prev.mtime.getTime()) { | |
compile(fname); | |
} | |
} | |
); | |
watching++; | |
} | |
}); | |
if(watching) { | |
console.log('Watching '+watching+' file(s) in '+ | |
COLOR_YELLOW+dirName+COLOR_NORMAL); | |
} else { | |
console.log('No coffee/jade/less files found to watch'); | |
} | |
}); | |
} else { | |
// It's a file | |
var fname = dirName; | |
if(/\.coffee$|\.less$|\.jade$/.test(fname)) { | |
fs.watchFile(fname,{persistent:true,interval:500}, | |
function (cur,prev) { | |
if(cur.mtime.getTime() != prev.mtime.getTime()) { | |
compile(fname); | |
} | |
} | |
); | |
console.log('Watching '+COLOR_YELLOW+fname+COLOR_NORMAL); | |
} else { | |
console.log('No coffee/jade/less files found to watch'); | |
} | |
} | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment