fsevents
Advanced tools
Comparing version 0.1.5 to 0.1.6
@@ -5,4 +5,5 @@ /* | ||
var fs = require('fs'); | ||
var util = require('util'); | ||
var events = require('events'); | ||
var EventEmitter = require('events').EventEmitter; | ||
var binding; | ||
@@ -14,35 +15,50 @@ try { | ||
} | ||
var FSEvents = binding.FSEvents; | ||
util.inherits(FSEvents, EventEmitter); | ||
var Fs = require('fs'); | ||
function getFileType(flags) { | ||
if (FSEvents.kFSEventStreamEventFlagItemIsFile & flags) return 'file'; | ||
if (FSEvents.kFSEventStreamEventFlagItemIsDir & flags) return 'directory'; | ||
if (FSEvents.kFSEventStreamEventFlagItemIsSymlink & flags) return 'symlink'; | ||
} | ||
module.exports = function(path) { | ||
function getEventType(flags) { | ||
if (FSEvents.kFSEventStreamEventFlagItemRemoved & flags) return 'deleted'; | ||
if (FSEvents.kFSEventStreamEventFlagItemCreated & flags) return 'created' | ||
if (FSEvents.kFSEventStreamEventFlagItemRenamed & flags) return 'moved'; | ||
if (FSEvents.kFSEventStreamEventFlagItemModified & flags) return 'modified'; | ||
return 'unknown'; | ||
} | ||
function getFileChanges(flags) { | ||
return { | ||
inode: !!(FSEvents.kFSEventStreamEventFlagItemInodeMetaMod & flags), | ||
finder: !!(FSEvents.kFSEventStreamEventFlagItemFinderInfoMod & flags), | ||
access: !!(FSEvents.kFSEventStreamEventFlagItemChangeOwner & flags), | ||
xattrs: !!(FSEvents.kFSEventStreamEventFlagItemXattrMod & flags) | ||
}; | ||
} | ||
function getInfo(path, flags) { | ||
return { | ||
path: path, | ||
event: getEventType(flags), | ||
type: getFileType(flags), | ||
changes: getFileChanges(flags), | ||
}; | ||
} | ||
function watch(path) { | ||
var fsevents = new FSEvents(path); | ||
fsevents.on('fsevent', function(path, flags, id) { | ||
var info = { | ||
event:'unknown', | ||
id:id, | ||
path: path, | ||
type: fileType(flags), | ||
changes: fileChanges(flags), | ||
}; | ||
if (FSEvents.kFSEventStreamEventFlagItemCreated & flags) { | ||
info.event = 'created'; | ||
} else if (FSEvents.kFSEventStreamEventFlagItemRemoved & flags) { | ||
info.event = 'deleted'; | ||
} else if (FSEvents.kFSEventStreamEventFlagItemRenamed & flags) { | ||
info.event = 'moved'; | ||
} else if (FSEvents.kFSEventStreamEventFlagItemModified & flags) { | ||
info.event = 'modified'; | ||
} | ||
var info = getInfo(path, flags); | ||
info.id = id; | ||
if (info.event == 'moved') { | ||
Fs.stat(info.path, function(err, stat) { | ||
if (err || !stat) { | ||
info.event = 'moved-out'; | ||
} else { | ||
info.event = 'moved-in'; | ||
} | ||
fsevents.emit('change', path, info); | ||
fsevents.emit(info.event, path, info); | ||
}); | ||
fs.stat(info.path, function(err, stat) { | ||
info.event = (err || !stat) ? 'moved-out' : 'moved-in'; | ||
fsevents.emit('change', path, info); | ||
fsevents.emit(info.event, path, info); | ||
}); | ||
} else { | ||
@@ -53,20 +69,8 @@ fsevents.emit('change', path, info); | ||
}); | ||
return fsevents; | ||
}; | ||
var FSEvents = binding.FSEvents; | ||
util.inherits(FSEvents, events.EventEmitter); | ||
function fileType(flags) { | ||
if (FSEvents.kFSEventStreamEventFlagItemIsFile & flags) return 'file'; | ||
if (FSEvents.kFSEventStreamEventFlagItemIsDir & flags) return 'directory'; | ||
if (FSEvents.kFSEventStreamEventFlagItemIsSymlink & flags) return 'symlink'; | ||
} | ||
function fileChanges(flags) { | ||
var res = {}; | ||
res.inode = !!(FSEvents.kFSEventStreamEventFlagItemInodeMetaMod & flags); | ||
res.finder = !!(FSEvents.kFSEventStreamEventFlagItemFinderInfoMod & flags); | ||
res.access = !!(FSEvents.kFSEventStreamEventFlagItemChangeOwner & flags); | ||
res.xattrs = !!(FSEvents.kFSEventStreamEventFlagItemXattrMod & flags); | ||
return res; | ||
} | ||
module.exports = watch; | ||
watch.FSEvents = binding.FSEvents; | ||
watch.getInfo = getInfo; |
@@ -5,3 +5,3 @@ { | ||
"homepage": "https://github.com/phidelta/NodeJS-FSEvents", | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"maintainers": [ | ||
@@ -8,0 +8,0 @@ "Philipp Dunkel <phidelta@phideltacity.net>" |
@@ -1,2 +0,3 @@ | ||
# Native Access to Mac OS-X FSEvents | ||
# FSEvents [![NPM](https://nodei.co/npm/fsevents.png)](https://nodei.co/npm/fsevents/) | ||
## Native Access to Mac OS-X FSEvents | ||
@@ -3,0 +4,0 @@ * [Node.js](http://nodejs.org/) |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
15296
73
2