DirectoryWatcher.js 11 KB

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