Blame view

node_modules/fsevents/fsevents.js 3.22 KB
aaac7fed   liuqimichale   add
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
  /*
   ** © 2014 by Philipp Dunkel <pip@pipobscure.com>
   ** Licensed under MIT License.
   */
  
  /* jshint node:true */
  'use strict';
  
  if (process.platform !== 'darwin')
    throw new Error('Module \'fsevents\' is not compatible with platform \'' + process.platform + '\'');
  
  var path = require('path');
  var binary = require('node-pre-gyp');
  var Native = require(binary.find(path.join(__dirname, 'package.json')));
  
  var EventEmitter = require('events').EventEmitter;
  var fs = require('fs');
  var inherits = require('util').inherits;
  
  function FSEvents(path, handler) {
    EventEmitter.call(this);
  
    Object.defineProperty(this, '_impl', {
      value: new Native.FSEvents(String(path || ''), handler),
      enumerable: false,
      writable: false
    });
  }
  
  inherits(FSEvents, EventEmitter);
  proxies(FSEvents, Native.FSEvents);
  
  module.exports = watch;
  module.exports.getInfo = getInfo;
  module.exports.FSEvents = Native.FSEvents;
  module.exports.Constants = Native.Constants;
  
  var defer = global.setImmediate || process.nextTick;
  
  function watch(path) {
    var fse = new FSEvents(String(path || ''), handler);
    EventEmitter.call(fse);
    return fse;
  
    function handler(path, flags, id) {
      defer(function() {
        fse.emit('fsevent', path, flags, id);
        var info = getInfo(path, flags);
        info.id = id;
        if (info.event === 'moved') {
          fs.stat(info.path, function(err, stat) {
            info.event = (err || !stat) ? 'moved-out' : 'moved-in';
            fse.emit('change', path, info);
            fse.emit(info.event, path, info);
          });
        } else {
          fse.emit('change', path, info);
          fse.emit(info.event, path, info);
        }
      });
    }
  }
  
  function proxies(ctor, target) {
    Object.keys(target.prototype).filter(function(key) {
      return typeof target.prototype[key] === 'function';
    }).forEach(function(key) {
      ctor.prototype[key] = function() {
        this._impl[key].apply(this._impl, arguments);
        return this;
      }
    });
  }
  
  function getFileType(flags) {
    if (Native.Constants.kFSEventStreamEventFlagItemIsFile & flags) return 'file';
    if (Native.Constants.kFSEventStreamEventFlagItemIsDir & flags) return 'directory';
    if (Native.Constants.kFSEventStreamEventFlagItemIsSymlink & flags) return 'symlink';
  }
  
  function getEventType(flags) {
    if (Native.Constants.kFSEventStreamEventFlagItemRemoved & flags) return 'deleted';
    if (Native.Constants.kFSEventStreamEventFlagItemRenamed & flags) return 'moved';
    if (Native.Constants.kFSEventStreamEventFlagItemCreated & flags) return 'created';
    if (Native.Constants.kFSEventStreamEventFlagItemModified & flags) return 'modified';
    if (Native.Constants.kFSEventStreamEventFlagRootChanged & flags) return 'root-changed';
  
    return 'unknown';
  }
  
  function getFileChanges(flags) {
    return {
      inode: !! (Native.Constants.kFSEventStreamEventFlagItemInodeMetaMod & flags),
      finder: !! (Native.Constants.kFSEventStreamEventFlagItemFinderInfoMod & flags),
      access: !! (Native.Constants.kFSEventStreamEventFlagItemChangeOwner & flags),
      xattrs: !! (Native.Constants.kFSEventStreamEventFlagItemXattrMod & flags)
    };
  }
  
  function getInfo(path, flags) {
    return {
      path: path,
      event: getEventType(flags),
      type: getFileType(flags),
      changes: getFileChanges(flags),
      flags: flags
    };
  }