-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
118 lines (114 loc) · 3.38 KB
/
main.js
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
109
110
111
112
113
114
115
116
117
118
// Generated by CoffeeScript 1.6.3
(function() {
var fs, np;
fs = require('fs');
np = require('path');
exports.watchDirectory = function(dirname, options, listener) {
var filter, fsListener, initial, matches, notifyListener, unwatchFile, watchFile, watchedFiles;
if (listener == null) {
listener = options;
options = {};
}
if (options.persistent == null) {
options.persistent = true;
}
if (options.interval == null) {
options.interval = 100;
}
if (options.recursive == null) {
options.recursive = true;
}
if (options.initial == null) {
options.initial = 'initial';
}
if (options.exclude == null) {
options.exclude = {
node_modules: true
};
}
matches = function(name, filter, defaultValue) {
var ext;
if (filter == null) {
return defaultValue;
} else if (typeof filter === 'string') {
ext = filter;
return name.indexOf(ext, name.length - ext.length) !== -1;
} else if (filter.constructor === RegExp) {
return filter.test(name);
} else if (typeof filter === 'function') {
return filter(name);
} else {
return filter[name] === true;
}
};
filter = function(name) {
if (matches(name, options.exclude, false)) {
return false;
} else {
return matches(name, options.include, true);
}
};
watchedFiles = {};
notifyListener = function(filename, curr, prev, change) {
if (filter(filename)) {
return listener(filename, curr, prev, change);
}
};
fsListener = function(filename, depth, curr, prev) {
var change;
change = curr.nlink === 0 ? 'deleted' : prev.nlink === 0 ? 'created' : 'modified';
notifyListener(filename, curr, prev, change);
if (change !== 'deleted') {
return watchFile(filename, depth, curr);
} else {
return unwatchFile(filename);
}
};
unwatchFile = function(filename) {
fs.unwatchFile(filename, watchedFiles[filename]);
return delete watchedFiles[filename];
};
watchFile = function(filename, depth, stats) {
var boundListener, child, _i, _len, _ref;
if (depth == null) {
depth = 0;
}
if (stats == null) {
stats = fs.statSync(filename);
}
if (stats.nlink > 0) {
if (stats.isDirectory()) {
if (!matches(filename, options.exclude, false)) {
if (depth === 0 || options.recursive) {
_ref = fs.readdirSync(filename);
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
child = _ref[_i];
child = np.join(filename, child);
watchFile(child, depth + 1);
}
}
}
}
if (watchedFiles[filename] == null) {
boundListener = fsListener.bind(this, filename, depth);
watchedFiles[filename] = boundListener;
fs.watchFile(filename, options, boundListener);
if (initial) {
notifyListener(filename, stats, stats, initial);
}
}
}
};
initial = options.initial;
watchFile(dirname);
initial = 'created';
return function() {
var key, _results;
_results = [];
for (key in watchedFiles) {
_results.push(unwatchFile(key));
}
return _results;
};
};
}).call(this);