Created
October 13, 2011 20:43
-
-
Save adamloving/1285463 to your computer and use it in GitHub Desktop.
Node.js CoffeeScript, Jade, and Stylus compiler (watches filesystem for changes)
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
var OUTPUT_JS_FILENAME = '../media/partner/js/widgets.js' | |
var OUTPUT_CSS_FILENAME = '../media/partner/css/widgets.css' | |
var OUTPUT_HTML_PATH = 'templates' | |
var fs = require('fs') | |
var jade = require('jade') | |
var stylus = require('stylus'); | |
var cs = require('coffee-script'); | |
var watch = require('watch'); | |
var isJade = function(fileName) { return fileName.match(/.jade$/); } | |
var isCoffeeScript = function(fileName) { return fileName.match(/.coffee$/) } | |
var isStylus = function(fileName) { return fileName.match(/.styl$/)} | |
var isCompilable = function(fileName) { | |
return isJade(fileName) || isCoffeeScript(fileName) || isStylus(fileName); | |
} | |
var allJadeFileNames = []; | |
var allCoffeeScriptFileNames = []; | |
var allStylusFileNames = []; | |
watch.createMonitor('.', { filter: isCompilable }, function (monitor) { | |
// build list of all the files we care about | |
for (fileName in monitor.files) { | |
if (isJade(fileName)) { | |
allJadeFileNames.push(fileName); | |
} else if (isCoffeeScript(fileName)) { | |
allCoffeeScriptFileNames.push(fileName); | |
} else if (isStylus(fileName)) { | |
allStylusFileNames.push(fileName); | |
} | |
} | |
// build all on startup | |
compileCoffeeScript(); | |
compileStylus(); | |
for (var i = 0; i < allJadeFileNames.length; i++) { | |
compileJade(allJadeFileNames[i]); | |
} | |
monitor.on("created", function (f, stat) { | |
console.log('created file', f); | |
if (isCompilable(f)) compile(f); | |
}) | |
monitor.on("changed", function (f, curr, prev) { | |
console.log('changed file', f); | |
if (isCompilable(f)) compile(f); | |
}); | |
monitor.on("removed", function (f, stat) { | |
console.log('removed file', f); | |
if (isCompilable(f)) compile(f); | |
}); | |
}); | |
compile = function(fileName) { | |
if (isCoffeeScript(fileName)) { | |
compileCoffeeScript(); | |
} else if (isJade(fileName)) { | |
compileJade(fileName); | |
} else if (isStylus(fileName)) { | |
compileStylus(); | |
} | |
} | |
outputLines = function(e, allCoffeeScript) { | |
var line = e.message.match(/line ([0-9]+):/); | |
if (line) { | |
line = parseInt(line[1]); | |
var lines = allCoffeeScript.split('\n'); | |
for (var i = Math.max(line - 10, 0); i < Math.min(line + 10, lines.length); i++) { | |
console.log(i + ": " + lines[i]); | |
} | |
} | |
} | |
compileCoffeeScript = function() { | |
console.log("Compiling CoffeeScript"); | |
var allCoffeeScript = ''; | |
for (var i = 0; i < allCoffeeScriptFileNames.length; i++) { | |
var fileName = allCoffeeScriptFileNames[i] | |
console.log("Reading: " + fileName); | |
allCoffeeScript += '\n' + fs.readFileSync(fileName, 'utf8'); | |
} | |
try { | |
var js = cs.compile(allCoffeeScript); | |
fs.writeFile(OUTPUT_JS_FILENAME, js); | |
console.log(" -> " + OUTPUT_JS_FILENAME); | |
} catch (e) { | |
console.log(e); | |
outputLines(e, allCoffeeScript); | |
} | |
} | |
// bug: no need to compile child templates...? | |
compileJade = function(fileName) { | |
console.log("Compiling Jade: " + fileName); | |
var outputFileName = OUTPUT_HTML_PATH + '/' + fileName.replace('.jade', '.html'); | |
j = fs.readFileSync(fileName, 'utf8'); | |
var html = jade.compile(j, { | |
pretty:true, | |
filename: fileName | |
})(); | |
fs.writeFile(outputFileName, html); | |
console.log(" -> " + outputFileName); | |
} | |
compileStylus = function() { | |
console.log("Compiling Stylus"); | |
var allStylus = ''; | |
for (var i = 0; i < allStylusFileNames.length; i++) { | |
allStylus += fs.readFileSync(allStylusFileNames[i], 'utf8'); | |
} | |
stylus.render(allStylus, {}, | |
function(err, css) { | |
fs.writeFile(OUTPUT_CSS_FILENAME, css); | |
console.log(" -> " + OUTPUT_CSS_FILENAME); | |
} | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment