Skip to content

Commit

Permalink
Working on porting to fs.watch() for issue #7. Vows isn't happy, but …
Browse files Browse the repository at this point in the history
…manual testing seems to fine
  • Loading branch information
Justin Slattery committed Mar 1, 2012
1 parent 64a478b commit 202328f
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 34 deletions.
2 changes: 1 addition & 1 deletion TODO
@@ -1,3 +1,3 @@
Things I should do:

- write unit tests for "stop"
- fixed broken unit tests
80 changes: 49 additions & 31 deletions lib/stalker.js
Expand Up @@ -10,44 +10,60 @@

//Helpful object friend
var watcher = require('./watcher').makeWatcher();

//Sucky work around for lame fs.watch() api
var handles = {};

var options = {};

var st = exports;

var folderChanged = function(folderPath, fnAdd, fnRemove) {
return function (curr, prev) {
if (curr.mtime.valueOf() === prev.mtime.valueOf()) { return; } //Don't act on a read
fs.readdir(folderPath, function _readdir(err, files) {
if (err) { return fnAdd && fnAdd(err); }

files.forEach(function _forEach(file){
if (file[0] === '.') { return; } //ignore files starting with "."

var fPath = path.join(folderPath, file);
fs.stat(fPath, function _stat(err, stats) {
if (err) { return fnAdd && fnAdd(err); }

//If we have a file, send it to our callback
if (stats.isFile()) {

watcher.checkFile(fPath, function _checkFile(err, result) {
if (err) { return fnAdd && fnAdd(err); }
if (!result) {
watcher.addFile(fPath, function onAddFile() {
return fnAdd && fnAdd(null, fPath);
});
}
});
}
else if (stats.isDirectory() && options.recurse) {
watchFolderTree(fPath, fnAdd, fnRemove, Infinity); //if we have a dir, match sure it is watched
}
return function (event, filename) {
if (event !== 'rename') { return; }

//Because of the goofy fs.watch() api, we have to close and recreate the
//file watch handle. Except sometimes the file was moved and this blows up
//So close, then check and recreate if it still exists
handles[folderPath].close();

fs.stat(folderPath, function(err) {
if (err) { return; }

console.log('folderPath is ' + folderPath)
handles[folderPath] = fs.watch(folderPath, folderChanged(folderPath, fnAdd, fnRemove));

fs.readdir(folderPath, function _readdir(err, files) {
if (err) { console.log('read');return fnAdd && fnAdd(err); }

files.forEach(function _forEach(file){
if (file[0] === '.') { return; } //ignore files starting with "."

var fPath = path.join(folderPath, file);
fs.stat(fPath, function _stat(err, stats) {
if (err) { return fnAdd && fnAdd(err); }

//If we have a file, send it to our callback
if (stats.isFile()) {

watcher.checkFile(fPath, function _checkFile(err, result) {
if (err) { return fnAdd && fnAdd(err); }
if (!result) {
watcher.addFile(fPath, function onAddFile() {
return fnAdd && fnAdd(null, fPath);
});
}
});
}
else if (stats.isDirectory() && options.recurse) {
watchFolderTree(fPath, fnAdd, fnRemove, Infinity); //if we have a dir, match sure it is watched
}
});
});
});
});

watcher.syncFolder(folderPath, fnRemove);
watcher.syncFolder(folderPath, fnRemove);
});
};
};

Expand All @@ -65,8 +81,10 @@

if (!result) {
watcher.addFile(fPath, function _addFile() {
fs.unwatchFile(fPath); //Clear out any old listeners. Is there a better way?
fs.watchFile(fPath, folderChanged(fPath, fnAdd, fnRemove));
//Clear out any old listeners. Is there a better way?
if (typeof(handles[fPath]) === 'object') { handles[fPath].close(); }

handles[fPath] = fs.watch(fPath, folderChanged(fPath, fnAdd, fnRemove));
});
}
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -2,7 +2,7 @@
"author": "Justin Slattery <Justin.Slattery@fzysqr.com> (http://fzysqr.com/)",
"name": "stalker",
"description": "Monitor directory trees for new files then do... something.",
"version": "0.0.15",
"version": "0.0.16",
"homepage": "https://github.com/jslatts/stalker",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion spec/stalker-vows.js
Expand Up @@ -87,7 +87,7 @@ vows.describe('stalker').addBatch({
assert.isNull(err);
assert.equal(file, lPath + '/temp');
fs.unlink(lPath + '/temp', function() {
fs.rmdir(oPath, function() {
fs.rmdir(oPath, function(err) {
fs.rmdir(lPath);
});
});
Expand Down

0 comments on commit 202328f

Please sign in to comment.