forked from webpack/webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNodeWatchFileSystem.js
More file actions
251 lines (229 loc) · 6.22 KB
/
NodeWatchFileSystem.js
File metadata and controls
251 lines (229 loc) · 6.22 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
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
var fs = require("fs");
var path = require("path");
var async = require("async");
function NodeWatchFileSystem(inputFileSystem) {
this.inputFileSystem = inputFileSystem;
}
module.exports = NodeWatchFileSystem;
/**
*
* @param files {String[]} a sorted array of paths to files
* @param dirs {String[]} a sorted array of paths to directories
* @param startTime {number} the virtual start time
* @param delay {number} in ms, the time to wait to signal after the first change
* @param callback {function(err, filesModified: String[], dirsModified: String[], fileTimestamps: Object, dirTimestamps: Object)] called once after change plus delay
* @param callbackUndelayed {function()} called once after first change
*/
NodeWatchFileSystem.prototype.watch = function(files, dirs, startTime, delay, callback, callbackUndelayed) {
var inputFileSystem = this.inputFileSystem;
if(!callbackUndelayed) callbackUndelayed = function() {}
var closed = false;
var fileTimestamps = {};
var dirTimestamps = {};
var filesModified = {};
var dirsModified = {};
var lastChangeTime;
startTime = Math.floor(startTime / 1000) * 1000; // only 1 second accuracy
var directories = {};
dirs.forEach(function(dir) {
directories[dir] = {
context: dir,
files: []
};
});
files.forEach(function(file) {
var dir = path.dirname(file);
if(!directories[dir]) directories[dir] = {
files: []
};
directories[dir].files.push(file);
});
var items = Object.keys(directories).map(function(dir) {
directories[dir].path = dir;
return directories[dir];
});
items.sort(function(a,b) {
if(a.path == b.path) return 0;
return a.path < b.path ? -1 : 1;
});
items.forEach(function(item) {
if(item.files) {
item.files.sort();
}
});
var initialChange = false;
var change = function(path) {
initialChange = true;
}
function readStat(item, callback) {
if(item.context) {
fs.readdir(item.path, function(err, files) {
function onTimestamp(ts) {
if(!dirTimestamps[item.context] || dirTimestamps[item.context] < ts)
dirTimestamps[item.context] = ts;
if(ts >= startTime) {
dirsModified[item.context] = true;
change(item.path);
}
return callback();
}
if(err) return onTimestamp(Infinity);
async.map(files, function(file, callback) {
file = path.join(item.path, file);
var isFile = false;
if(item.files) {
if(binarySearch(item.files, function(path) {
if(path == file) return 0;
return path < file ? -1 : 1;
}) >= 0) {
isFile = true;
}
}
fs.stat(file, function(err, stat) {
var ts = err ? Infinity : stat.mtime.getTime();
if(isFile) {
fileTimestamps[file] = ts;
if(ts >= startTime) filesModified[file] = true;
}
return callback(null, ts);
});
}, function(err, timestamps) {
if(err) return onTimestamp(Infinity);
var ts = timestamps.reduce(function(max, ts) {
if(ts > max)
return ts;
return max;
}, 0);
onTimestamp(ts);
});
});
} else {
async.forEach(item.files, function(file, callback) {
fs.stat(file, function(err, stat) {
var ts = err ? Infinity : stat.mtime.getTime();
fileTimestamps[file] = ts;
if(ts >= startTime) {
filesModified[file] = true;
change(file);
}
return callback(null, ts);
});
}, callback);
}
}
async.forEach(items, function processItem(item, callback) {
var isRunning = false;
var isScheduled = false;
item.watcher = fs.watch(item.path, function() {
if(isRunning) return isScheduled = true;
isRunning = true;
readStat(item, done);
});
if(item.context) {
item.children = [];
fs.readdir(item.path, function(err, files) {
if(err) return change(file), onWatcherApplied();
async.forEach(files, function(file, callback) {
file = path.join(item.path, file);
fs.stat(file, function(err, stat) {
if(err) return change(file), callback();
if(!stat.isDirectory()) return callback();
var subitem = {
path: file,
context: item.context
};
item.children.push(subitem);
processItem(subitem, callback);
});
}, onWatcherApplied);
});
} else onWatcherApplied();
function onWatcherApplied() {
readStat(item, function() {
callback();
done();
});
}
function done() {
if(closed) return;
if(isScheduled) {
isScheduled = false;
readStat(item, done);
} else {
isRunning = false;
}
}
}, function() {
var timeout;
if(initialChange) {
callbackUndelayed();
if(delay) {
lastChangeTime = Date.now();
change = restartDelay;
timeout = setTimeout(onTimeout, delay);
} else onTimeout();
} else {
change = function(path) {
callbackUndelayed();
if(delay) {
lastChangeTime = Date.now();
change = restartDelay;
timeout = setTimeout(onTimeout, delay);
} else {
change = function() {};
onTimeout();
}
};
}
function restartDelay() {
lastChangeTime = Date.now();
clearTimeout(timeout);
timeout = setTimeout(onTimeout, delay);
}
});
// 7.
function onTimeout() {
var nextSecond = Math.ceil(lastChangeTime / 1000) * 1000;
var timeToNextSecond = nextSecond - Date.now();
if(timeToNextSecond > 0) {
setTimeout(onTimeout, timeToNextSecond);
return;
}
change = function() {};
if(closed) return;
var outdatedFiles = Object.keys(filesModified).sort();
var outdatedDirs = Object.keys(dirsModified).sort();
if(inputFileSystem && inputFileSystem.purge) {
inputFileSystem.purge(outdatedFiles);
inputFileSystem.purge(outdatedDirs);
}
callback(null, outdatedFiles, outdatedDirs, fileTimestamps, dirTimestamps);
close();
}
function close() {
closed = true;
items.forEach(function closeItem(item) {
item.watcher.close();
if(item.children) item.children.forEach(closeItem);
});
}
return {
close: close
}
};
function binarySearch(array, comparator) {
var left = 0;
var right = array.length - 1;
while(left <= right) {
var middle = ((left + right)/2)|0;
var comp = comparator(array[middle]);
if(comp == 0) return middle;
if(comp > 0) right = middle-1;
if(comp < 0) left = middle+1;
}
return -1;
}