Projekt

Obecné

Profil

Stáhnout (3.12 KB) Statistiky
| Větev: | Revize:
1
/*
2
 ** © 2014 by Philipp Dunkel <pip@pipobscure.com>
3
 ** Licensed under MIT License.
4
 */
5

    
6
/* jshint node:true */
7
'use strict';
8

    
9
if (process.platform !== 'darwin')
10
  throw new Error('Module \'fsevents\' is not compatible with platform \'' + process.platform + '\'');
11

    
12
var Native = require("bindings")("fse");
13

    
14
var EventEmitter = require('events').EventEmitter;
15
var fs = require('fs');
16
var inherits = require('util').inherits;
17

    
18
function FSEvents(path, handler) {
19
  EventEmitter.call(this);
20

    
21
  Object.defineProperty(this, '_impl', {
22
    value: new Native.FSEvents(String(path || ''), handler),
23
    enumerable: false,
24
    writable: false
25
  });
26
}
27

    
28
inherits(FSEvents, EventEmitter);
29
proxies(FSEvents, Native.FSEvents);
30

    
31
module.exports = watch;
32
module.exports.getInfo = getInfo;
33
module.exports.FSEvents = Native.FSEvents;
34
module.exports.Constants = Native.Constants;
35

    
36
var defer = global.setImmediate || process.nextTick;
37

    
38
function watch(path) {
39
  var fse = new FSEvents(String(path || ''), handler);
40
  EventEmitter.call(fse);
41
  return fse;
42

    
43
  function handler(path, flags, id) {
44
    defer(function() {
45
      fse.emit('fsevent', path, flags, id);
46
      var info = getInfo(path, flags);
47
      info.id = id;
48
      if (info.event === 'moved') {
49
        fs.stat(info.path, function(err, stat) {
50
          info.event = (err || !stat) ? 'moved-out' : 'moved-in';
51
          fse.emit('change', path, info);
52
          fse.emit(info.event, path, info);
53
        });
54
      } else {
55
        fse.emit('change', path, info);
56
        fse.emit(info.event, path, info);
57
      }
58
    });
59
  }
60
}
61

    
62
function proxies(ctor, target) {
63
  Object.keys(target.prototype).filter(function(key) {
64
    return typeof target.prototype[key] === 'function';
65
  }).forEach(function(key) {
66
    ctor.prototype[key] = function() {
67
      this._impl[key].apply(this._impl, arguments);
68
      return this;
69
    }
70
  });
71
}
72

    
73
function getFileType(flags) {
74
  if (Native.Constants.kFSEventStreamEventFlagItemIsFile & flags) return 'file';
75
  if (Native.Constants.kFSEventStreamEventFlagItemIsDir & flags) return 'directory';
76
  if (Native.Constants.kFSEventStreamEventFlagItemIsSymlink & flags) return 'symlink';
77
}
78

    
79
function getEventType(flags) {
80
  if (Native.Constants.kFSEventStreamEventFlagItemRemoved & flags) return 'deleted';
81
  if (Native.Constants.kFSEventStreamEventFlagItemRenamed & flags) return 'moved';
82
  if (Native.Constants.kFSEventStreamEventFlagItemCreated & flags) return 'created';
83
  if (Native.Constants.kFSEventStreamEventFlagItemModified & flags) return 'modified';
84
  if (Native.Constants.kFSEventStreamEventFlagRootChanged & flags) return 'root-changed';
85

    
86
  return 'unknown';
87
}
88

    
89
function getFileChanges(flags) {
90
  return {
91
    inode: !! (Native.Constants.kFSEventStreamEventFlagItemInodeMetaMod & flags),
92
    finder: !! (Native.Constants.kFSEventStreamEventFlagItemFinderInfoMod & flags),
93
    access: !! (Native.Constants.kFSEventStreamEventFlagItemChangeOwner & flags),
94
    xattrs: !! (Native.Constants.kFSEventStreamEventFlagItemXattrMod & flags)
95
  };
96
}
97

    
98
function getInfo(path, flags) {
99
  return {
100
    path: path,
101
    event: getEventType(flags),
102
    type: getFileType(flags),
103
    changes: getFileChanges(flags),
104
    flags: flags
105
  };
106
}
(7-7/8)