DirectoryWatcher.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. var EventEmitter = require("events").EventEmitter;
  6. var async = require("async");
  7. var chokidar = require("chokidar");
  8. var fs = require("graceful-fs");
  9. var path = require("path");
  10. var watcherManager = require("./watcherManager");
  11. var FS_ACCURACY = 10000;
  12. function withoutCase(str) {
  13. return str.toLowerCase();
  14. }
  15. function Watcher(directoryWatcher, filePath, startTime) {
  16. EventEmitter.call(this);
  17. this.directoryWatcher = directoryWatcher;
  18. this.path = filePath;
  19. this.startTime = startTime && +startTime;
  20. this.data = 0;
  21. }
  22. Watcher.prototype = Object.create(EventEmitter.prototype);
  23. Watcher.prototype.constructor = Watcher;
  24. Watcher.prototype.checkStartTime = function checkStartTime(mtime, initial) {
  25. if(typeof this.startTime !== "number") return !initial;
  26. var startTime = this.startTime;
  27. return startTime <= mtime;
  28. };
  29. Watcher.prototype.close = function close() {
  30. this.emit("closed");
  31. };
  32. function DirectoryWatcher(directoryPath, options) {
  33. EventEmitter.call(this);
  34. this.options = options;
  35. this.path = directoryPath;
  36. this.files = Object.create(null);
  37. this.directories = Object.create(null);
  38. this.watcher = chokidar.watch(directoryPath, {
  39. ignoreInitial: true,
  40. persistent: true,
  41. followSymlinks: false,
  42. depth: 0,
  43. atomic: false,
  44. alwaysStat: true,
  45. ignorePermissionErrors: true,
  46. ignored: options.ignored,
  47. usePolling: options.poll ? true : undefined,
  48. interval: typeof options.poll === "number" ? options.poll : undefined,
  49. disableGlobbing: true
  50. });
  51. this.watcher.on("add", this.onFileAdded.bind(this));
  52. this.watcher.on("addDir", this.onDirectoryAdded.bind(this));
  53. this.watcher.on("change", this.onChange.bind(this));
  54. this.watcher.on("unlink", this.onFileUnlinked.bind(this));
  55. this.watcher.on("unlinkDir", this.onDirectoryUnlinked.bind(this));
  56. this.watcher.on("error", this.onWatcherError.bind(this));
  57. this.initialScan = true;
  58. this.nestedWatching = false;
  59. this.initialScanRemoved = [];
  60. this.doInitialScan();
  61. this.watchers = Object.create(null);
  62. this.refs = 0;
  63. }
  64. module.exports = DirectoryWatcher;
  65. DirectoryWatcher.prototype = Object.create(EventEmitter.prototype);
  66. DirectoryWatcher.prototype.constructor = DirectoryWatcher;
  67. DirectoryWatcher.prototype.setFileTime = function setFileTime(filePath, mtime, initial, type) {
  68. var now = Date.now();
  69. var old = this.files[filePath];
  70. this.files[filePath] = [initial ? Math.min(now, mtime) : now, mtime];
  71. // we add the fs accurency to reach the maximum possible mtime
  72. if(mtime)
  73. mtime = mtime + FS_ACCURACY;
  74. if(!old) {
  75. if(mtime) {
  76. if(this.watchers[withoutCase(filePath)]) {
  77. this.watchers[withoutCase(filePath)].forEach(function(w) {
  78. if(!initial || w.checkStartTime(mtime, initial)) {
  79. w.emit("change", mtime, initial ? "initial" : type);
  80. }
  81. });
  82. }
  83. }
  84. } else if(!initial && mtime && type !== "add") {
  85. if(this.watchers[withoutCase(filePath)]) {
  86. this.watchers[withoutCase(filePath)].forEach(function(w) {
  87. w.emit("change", mtime, type);
  88. });
  89. }
  90. } else if(!initial && !mtime) {
  91. if(this.watchers[withoutCase(filePath)]) {
  92. this.watchers[withoutCase(filePath)].forEach(function(w) {
  93. w.emit("remove", type);
  94. });
  95. }
  96. }
  97. if(this.watchers[withoutCase(this.path)]) {
  98. this.watchers[withoutCase(this.path)].forEach(function(w) {
  99. if(!initial || w.checkStartTime(mtime, initial)) {
  100. w.emit("change", filePath, mtime, initial ? "initial" : type);
  101. }
  102. });
  103. }
  104. };
  105. DirectoryWatcher.prototype.setDirectory = function setDirectory(directoryPath, exist, initial, type) {
  106. if(directoryPath === this.path) {
  107. if(!initial && this.watchers[withoutCase(this.path)]) {
  108. this.watchers[withoutCase(this.path)].forEach(function(w) {
  109. w.emit("change", directoryPath, w.data, initial ? "initial" : type);
  110. });
  111. }
  112. } else {
  113. var old = this.directories[directoryPath];
  114. if(!old) {
  115. if(exist) {
  116. if(this.nestedWatching) {
  117. this.createNestedWatcher(directoryPath);
  118. } else {
  119. this.directories[directoryPath] = true;
  120. }
  121. if(!initial && this.watchers[withoutCase(this.path)]) {
  122. this.watchers[withoutCase(this.path)].forEach(function(w) {
  123. w.emit("change", directoryPath, w.data, initial ? "initial" : type);
  124. });
  125. }
  126. }
  127. } else {
  128. if(!exist) {
  129. if(this.nestedWatching)
  130. this.directories[directoryPath].close();
  131. delete this.directories[directoryPath];
  132. if(!initial && this.watchers[withoutCase(this.path)]) {
  133. this.watchers[withoutCase(this.path)].forEach(function(w) {
  134. w.emit("change", directoryPath, w.data, initial ? "initial" : type);
  135. });
  136. }
  137. }
  138. }
  139. }
  140. };
  141. DirectoryWatcher.prototype.createNestedWatcher = function(directoryPath) {
  142. this.directories[directoryPath] = watcherManager.watchDirectory(directoryPath, this.options, 1);
  143. this.directories[directoryPath].on("change", function(filePath, mtime, type) {
  144. if(this.watchers[withoutCase(this.path)]) {
  145. this.watchers[withoutCase(this.path)].forEach(function(w) {
  146. if(w.checkStartTime(mtime, false)) {
  147. w.emit("change", filePath, mtime, type);
  148. }
  149. });
  150. }
  151. }.bind(this));
  152. };
  153. DirectoryWatcher.prototype.setNestedWatching = function(flag) {
  154. if(this.nestedWatching !== !!flag) {
  155. this.nestedWatching = !!flag;
  156. if(this.nestedWatching) {
  157. Object.keys(this.directories).forEach(function(directory) {
  158. this.createNestedWatcher(directory);
  159. }, this);
  160. } else {
  161. Object.keys(this.directories).forEach(function(directory) {
  162. this.directories[directory].close();
  163. this.directories[directory] = true;
  164. }, this);
  165. }
  166. }
  167. };
  168. DirectoryWatcher.prototype.watch = function watch(filePath, startTime) {
  169. this.watchers[withoutCase(filePath)] = this.watchers[withoutCase(filePath)] || [];
  170. this.refs++;
  171. var watcher = new Watcher(this, filePath, startTime);
  172. watcher.on("closed", function() {
  173. var idx = this.watchers[withoutCase(filePath)].indexOf(watcher);
  174. this.watchers[withoutCase(filePath)].splice(idx, 1);
  175. if(this.watchers[withoutCase(filePath)].length === 0) {
  176. delete this.watchers[withoutCase(filePath)];
  177. if(this.path === filePath)
  178. this.setNestedWatching(false);
  179. }
  180. if(--this.refs <= 0)
  181. this.close();
  182. }.bind(this));
  183. this.watchers[withoutCase(filePath)].push(watcher);
  184. var data;
  185. if(filePath === this.path) {
  186. this.setNestedWatching(true);
  187. data = false;
  188. Object.keys(this.files).forEach(function(file) {
  189. var d = this.files[file];
  190. if(!data)
  191. data = d;
  192. else
  193. data = [Math.max(data[0], d[0]), Math.max(data[1], d[1])];
  194. }, this);
  195. } else {
  196. data = this.files[filePath];
  197. }
  198. process.nextTick(function() {
  199. if(data) {
  200. var ts = data[0] === data[1] ? data[0] + FS_ACCURACY : data[0];
  201. if(ts >= startTime)
  202. watcher.emit("change", data[1]);
  203. } else if(this.initialScan && this.initialScanRemoved.indexOf(filePath) >= 0) {
  204. watcher.emit("remove");
  205. }
  206. }.bind(this));
  207. return watcher;
  208. };
  209. DirectoryWatcher.prototype.onFileAdded = function onFileAdded(filePath, stat) {
  210. if(filePath.indexOf(this.path) !== 0) return;
  211. if(/[\\\/]/.test(filePath.substr(this.path.length + 1))) return;
  212. this.setFileTime(filePath, +stat.mtime, false, "add");
  213. };
  214. DirectoryWatcher.prototype.onDirectoryAdded = function onDirectoryAdded(directoryPath /*, stat */) {
  215. if(directoryPath.indexOf(this.path) !== 0) return;
  216. if(/[\\\/]/.test(directoryPath.substr(this.path.length + 1))) return;
  217. this.setDirectory(directoryPath, true, false, "add");
  218. };
  219. DirectoryWatcher.prototype.onChange = function onChange(filePath, stat) {
  220. if(filePath.indexOf(this.path) !== 0) return;
  221. if(/[\\\/]/.test(filePath.substr(this.path.length + 1))) return;
  222. var mtime = +stat.mtime;
  223. ensureFsAccuracy(mtime);
  224. this.setFileTime(filePath, mtime, false, "change");
  225. };
  226. DirectoryWatcher.prototype.onFileUnlinked = function onFileUnlinked(filePath) {
  227. if(filePath.indexOf(this.path) !== 0) return;
  228. if(/[\\\/]/.test(filePath.substr(this.path.length + 1))) return;
  229. this.setFileTime(filePath, null, false, "unlink");
  230. if(this.initialScan) {
  231. this.initialScanRemoved.push(filePath);
  232. }
  233. };
  234. DirectoryWatcher.prototype.onDirectoryUnlinked = function onDirectoryUnlinked(directoryPath) {
  235. if(directoryPath.indexOf(this.path) !== 0) return;
  236. if(/[\\\/]/.test(directoryPath.substr(this.path.length + 1))) return;
  237. this.setDirectory(directoryPath, false, false, "unlink");
  238. if(this.initialScan) {
  239. this.initialScanRemoved.push(directoryPath);
  240. }
  241. };
  242. DirectoryWatcher.prototype.onWatcherError = function onWatcherError(/* err */) {
  243. };
  244. DirectoryWatcher.prototype.doInitialScan = function doInitialScan() {
  245. fs.readdir(this.path, function(err, items) {
  246. if(err) {
  247. this.initialScan = false;
  248. return;
  249. }
  250. async.forEach(items, function(item, callback) {
  251. var itemPath = path.join(this.path, item);
  252. fs.stat(itemPath, function(err2, stat) {
  253. if(!this.initialScan) return;
  254. if(err2) {
  255. callback();
  256. return;
  257. }
  258. if(stat.isFile()) {
  259. if(!this.files[itemPath])
  260. this.setFileTime(itemPath, +stat.mtime, true);
  261. } else if(stat.isDirectory()) {
  262. if(!this.directories[itemPath])
  263. this.setDirectory(itemPath, true, true);
  264. }
  265. callback();
  266. }.bind(this));
  267. }.bind(this), function() {
  268. this.initialScan = false;
  269. this.initialScanRemoved = null;
  270. }.bind(this));
  271. }.bind(this));
  272. };
  273. DirectoryWatcher.prototype.getTimes = function() {
  274. var obj = Object.create(null);
  275. var selfTime = 0;
  276. Object.keys(this.files).forEach(function(file) {
  277. var data = this.files[file];
  278. var time;
  279. if(data[1]) {
  280. time = Math.max(data[0], data[1] + FS_ACCURACY);
  281. } else {
  282. time = data[0];
  283. }
  284. obj[file] = time;
  285. if(time > selfTime)
  286. selfTime = time;
  287. }, this);
  288. if(this.nestedWatching) {
  289. Object.keys(this.directories).forEach(function(dir) {
  290. var w = this.directories[dir];
  291. var times = w.directoryWatcher.getTimes();
  292. Object.keys(times).forEach(function(file) {
  293. var time = times[file];
  294. obj[file] = time;
  295. if(time > selfTime)
  296. selfTime = time;
  297. });
  298. }, this);
  299. obj[this.path] = selfTime;
  300. }
  301. return obj;
  302. };
  303. DirectoryWatcher.prototype.close = function() {
  304. this.initialScan = false;
  305. this.watcher.close();
  306. if(this.nestedWatching) {
  307. Object.keys(this.directories).forEach(function(dir) {
  308. this.directories[dir].close();
  309. }, this);
  310. }
  311. this.emit("closed");
  312. };
  313. function ensureFsAccuracy(mtime) {
  314. if(!mtime) return;
  315. if(FS_ACCURACY > 1 && mtime % 1 !== 0)
  316. FS_ACCURACY = 1;
  317. else if(FS_ACCURACY > 10 && mtime % 10 !== 0)
  318. FS_ACCURACY = 10;
  319. else if(FS_ACCURACY > 100 && mtime % 100 !== 0)
  320. FS_ACCURACY = 100;
  321. else if(FS_ACCURACY > 1000 && mtime % 1000 !== 0)
  322. FS_ACCURACY = 1000;
  323. else if(FS_ACCURACY > 2000 && mtime % 2000 !== 0)
  324. FS_ACCURACY = 2000;
  325. }