watcher.js
7.23 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
var assert = require("assert");
var path = require("path");
var fs = require("graceful-fs");
var spawn = require("child_process").spawn;
var Q = require("q");
var EventEmitter = require("events").EventEmitter;
var ReadFileCache = require("./cache").ReadFileCache;
var util = require("./util");
var hasOwn = Object.prototype.hasOwnProperty;
function Watcher(readFileCache, persistent) {
assert.ok(this instanceof Watcher);
assert.ok(this instanceof EventEmitter);
assert.ok(readFileCache instanceof ReadFileCache);
// During tests (and only during tests), persistent === false so that
// the test suite can actually finish and exit.
if (typeof persistent === "undefined") {
persistent = true;
}
EventEmitter.call(this);
var self = this;
var sourceDir = readFileCache.sourceDir;
var dirWatcher = new DirWatcher(sourceDir, persistent);
Object.defineProperties(self, {
sourceDir: { value: sourceDir },
readFileCache: { value: readFileCache },
dirWatcher: { value: dirWatcher }
});
// Watch everything the readFileCache already knows about, and any new
// files added in the future.
readFileCache.subscribe(function(relativePath) {
self.watch(relativePath);
});
readFileCache.on("changed", function(relativePath) {
self.emit("changed", relativePath);
});
function handleDirEvent(event, relativePath) {
if (self.dirWatcher.ready) {
self.getFileHandler(relativePath)(event);
}
}
dirWatcher.on("added", function(relativePath) {
handleDirEvent("added", relativePath);
}).on("deleted", function(relativePath) {
handleDirEvent("deleted", relativePath);
}).on("changed", function(relativePath) {
handleDirEvent("changed", relativePath);
});
}
util.inherits(Watcher, EventEmitter);
var Wp = Watcher.prototype;
Wp.watch = function(relativePath) {
this.dirWatcher.add(path.dirname(path.join(
this.sourceDir, relativePath)));
};
Wp.readFileP = function(relativePath) {
return this.readFileCache.readFileP(relativePath);
};
Wp.noCacheReadFileP = function(relativePath) {
return this.readFileCache.noCacheReadFileP(relativePath);
};
Wp.getFileHandler = util.cachedMethod(function(relativePath) {
var self = this;
return function handler(event) {
self.readFileCache.reportPossiblyChanged(relativePath);
};
});
function orNull(err) {
return null;
}
Wp.close = function() {
this.dirWatcher.close();
};
/**
* DirWatcher code adapted from Jeffrey Lin's original implementation:
* https://github.com/jeffreylin/jsx_transformer_fun/blob/master/dirWatcher.js
*
* Invariant: this only watches the dir inode, not the actual path.
* That means the dir can't be renamed and swapped with another dir.
*/
function DirWatcher(inputPath, persistent) {
assert.ok(this instanceof DirWatcher);
var self = this;
var absPath = path.resolve(inputPath);
if (!fs.statSync(absPath).isDirectory()) {
throw new Error(inputPath + "is not a directory!");
}
EventEmitter.call(self);
self.ready = false;
self.on("ready", function(){
self.ready = true;
});
Object.defineProperties(self, {
// Map of absDirPaths to fs.FSWatcher objects from fs.watch().
watchers: { value: {} },
dirContents: { value: {} },
rootPath: { value: absPath },
persistent: { value: !!persistent }
});
process.nextTick(function() {
self.add(absPath);
self.emit("ready");
});
}
util.inherits(DirWatcher, EventEmitter);
var DWp = DirWatcher.prototype;
DWp.add = function(absDirPath) {
var self = this;
if (hasOwn.call(self.watchers, absDirPath)) {
return;
}
self.watchers[absDirPath] = fs.watch(absDirPath, {
persistent: this.persistent
}).on("change", function(event, filename) {
self.updateDirContents(absDirPath, event, filename);
});
// Update internal dir contents.
self.updateDirContents(absDirPath);
// Since we've never seen this path before, recursively add child
// directories of this path. TODO: Don't do fs.readdirSync on the
// same dir twice in a row. We already do an fs.statSync in
// this.updateDirContents() and we're just going to do another one
// here...
fs.readdirSync(absDirPath).forEach(function(filename) {
var filepath = path.join(absDirPath, filename);
// Look for directories.
if (fs.statSync(filepath).isDirectory()) {
self.add(filepath);
}
});
};
DWp.updateDirContents = function(absDirPath, event, fsWatchReportedFilename) {
var self = this;
if (!hasOwn.call(self.dirContents, absDirPath)) {
self.dirContents[absDirPath] = [];
}
var oldContents = self.dirContents[absDirPath];
var newContents = fs.readdirSync(absDirPath);
var deleted = {};
var added = {};
oldContents.forEach(function(filename) {
deleted[filename] = true;
});
newContents.forEach(function(filename) {
if (hasOwn.call(deleted, filename)) {
delete deleted[filename];
} else {
added[filename] = true;
}
});
var deletedNames = Object.keys(deleted);
deletedNames.forEach(function(filename) {
self.emit(
"deleted",
path.relative(
self.rootPath,
path.join(absDirPath, filename)
)
);
});
var addedNames = Object.keys(added);
addedNames.forEach(function(filename) {
self.emit(
"added",
path.relative(
self.rootPath,
path.join(absDirPath, filename)
)
);
});
// So changed is not deleted or added?
if (fsWatchReportedFilename &&
!hasOwn.call(deleted, fsWatchReportedFilename) &&
!hasOwn.call(added, fsWatchReportedFilename))
{
self.emit(
"changed",
path.relative(
self.rootPath,
path.join(absDirPath, fsWatchReportedFilename)
)
);
}
// If any of the things removed were directories, remove their watchers.
// If a dir was moved, hopefully two changed events fired?
// 1) event in dir where it was removed
// 2) event in dir where it was moved to (added)
deletedNames.forEach(function(filename) {
var filepath = path.join(absDirPath, filename);
delete self.dirContents[filepath];
delete self.watchers[filepath];
});
// if any of the things added were directories, recursively deal with them
addedNames.forEach(function(filename) {
var filepath = path.join(absDirPath, filename);
if (fs.existsSync(filepath) &&
fs.statSync(filepath).isDirectory())
{
self.add(filepath);
// mighttttttt need a self.updateDirContents() here in case
// we're somehow adding a path that replaces another one...?
}
});
// Update state of internal dir contents.
self.dirContents[absDirPath] = newContents;
};
DWp.close = function() {
var watchers = this.watchers;
Object.keys(watchers).forEach(function(filename) {
watchers[filename].close();
});
};
exports.Watcher = Watcher;