Skip to content

Instantly share code, notes, and snippets.

@phiggins42
Created August 1, 2011 11:43
Show Gist options
  • Save phiggins42/1117991 to your computer and use it in GitHub Desktop.
Save phiggins42/1117991 to your computer and use it in GitHub Desktop.
/*
monitor.js - monitors a javascript file and re-executes when file is
saved. eg:
$ node monitor.js /home/me/sample.js
Now edit /home/me/sample.js and watch the console output. Like a "live
firebug", though gives you the comfort of your own editor for quick
testing and prototyping.
*/
var path = require("path"),
fs = require("fs"),
vm = require("vm")
;
if(process.argv.length < 3){
process.quit();
}
var file = process.argv[2];
function run(file){
fs.readFile(file, "utf8", function(error, content){
if(!error){
try{
console.log("-- RUNNING:", file);
vm.runInThisContext(content);
console.log("-- COMPLETE!");
}catch(e){
console.log(e.stack);
}
}
});
}
file && path.exists(file, function(exists){
if(!exists){
console.log("ERROR: file not found:", file);
return;
}
fs.watchFile(file, function(cur, prev){
if(cur.mtime > prev.mtime){
run(file);
}
});
run(file);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment