Created
March 29, 2012 04:08
-
-
Save gsdevme/2233212 to your computer and use it in GitHub Desktop.
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 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