Skip to content

Instantly share code, notes, and snippets.

@gsdevme
Created March 29, 2012 04:08
Show Gist options
  • Save gsdevme/2233212 to your computer and use it in GitHub Desktop.
Save gsdevme/2233212 to your computer and use it in GitHub Desktop.
var fs = require('fs');
function processWatch(file, event) {
fs.stat(file, function(error, stat){
if(error === null){
switch(event){
case 'rename':
// has it actually been changed or is it a node false positive (stupid node bug)
if(stat.ctime.toString() != stat.mtime.toString()){
console.log(file + ' has actually been renamed');
}
break;
case 'change':
if(stat.isFile()){
// do shit
console.log('File changed!' + file);
}
if(stat.isDirectory()){
// woops its a folder!
console.log('Folder changed! ' + file);
}
break;
}
}
});
}
function watcher(directory) {
fs.watch(directory, { persistent: true, interval: 0 }, function (event, filename) {
// issue with MacOS perhaps more... so check we have a Filename
if (filename) {
processWatch(directory + filename, event);
}
console.log('Oh no!, we dont have the filename... are you using MacOS perhaps?');
});
}
watcher('/root/test/');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment