Skip to content

Instantly share code, notes, and snippets.

@civersen
Last active Aug 29, 2015
Embed
What would you like to do?
A simple node.js script that monitors changes to javascript files in my development directory tree. Once a change occurs, the script creates a minified version using the Closure compiler.
var watch = require("watch");
var cc = require("closurecompiler");
var fs = require("fs");
watch.watchTree("/Users/chris/Dropbox/Development",{"ignoreDotFiles":true},function(f,cstat,pstat){
if(typeof(f)=="string" && f.toLowerCase().indexOf(".js")>1 && f.toLowerCase().indexOf(".min.")==-1){
if(cstat.nlink===1){
var fname = f.substring(f.lastIndexOf("/")+1);
var fpath = f.substr(0,f.lastIndexOf("/"));
fname = [fname.slice(0,fname.lastIndexOf(".")),fname.slice(fname.lastIndexOf(".")+1)];
var nf = fpath+"/"+fname[0]+".min."+fname[1];
console.log("Compiling File: "+fname.join(".")+" ...")
var minfile = cc.compile([f],
{ compilation_level : "SIMPLE_OPTIMIZATIONS" },
function(error,result){
if(result){
fs.writeFile(nf,result,function(err){
if(err){
console.log("Error: Could not output to: "+nf);
throw err;
}
else { console.log("Completed: "+fname.join(".")); }
})
}
else { console.log("Error: "+error)}
}
);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment