選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

DirectoryWatcher.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. };
  259. DirectoryWatcher.prototype.doInitialScan = function doInitialScan() {
  260. fs.readdir(this.path, function(err, items) {
  261. if(err) {
  262. this.parentWatcher = watcherManager.watchFile(this.path + "#directory", this.options, 1);
  263. this.parentWatcher.on("change", function(mtime, type) {
  264. if(this.watchers[withoutCase(this.path)]) {
  265. this.watchers[withoutCase(this.path)].forEach(function(w) {
  266. w.emit("change", this.path, mtime, type);
  267. }, this);
  268. }
  269. }.bind(this));
  270. this.initialScan = false;
  271. return;
  272. }
  273. async.forEach(items, function(item, callback) {
  274. var itemPath = path.join(this.path, item);
  275. fs.stat(itemPath, function(err2, stat) {
  276. if(!this.initialScan) return;
  277. if(err2) {
  278. callback();
  279. return;
  280. }
  281. if(stat.isFile()) {
  282. if(!this.files[itemPath])
  283. this.setFileTime(itemPath, +stat.mtime || +stat.ctime || 1, true);
  284. } else if(stat.isDirectory()) {
  285. if(!this.directories[itemPath])
  286. this.setDirectory(itemPath, true, true);
  287. }
  288. callback();
  289. }.bind(this));
  290. }.bind(this), function() {
  291. this.initialScan = false;
  292. this.initialScanRemoved = null;
  293. }.bind(this));
  294. }.bind(this));
  295. };
  296. DirectoryWatcher.prototype.getTimes = function() {
  297. var obj = Object.create(null);
  298. var selfTime = 0;
  299. Object.keys(this.files).forEach(function(file) {
  300. var data = this.files[file];
  301. var time;
  302. if(data[1]) {
  303. time = Math.max(data[0], data[1] + FS_ACCURACY);
  304. } else {
  305. time = data[0];
  306. }
  307. obj[file] = time;
  308. if(time > selfTime)
  309. selfTime = time;
  310. }, this);
  311. if(this.nestedWatching) {
  312. Object.keys(this.directories).forEach(function(dir) {
  313. var w = this.directories[dir];
  314. var times = w.directoryWatcher.getTimes();
  315. Object.keys(times).forEach(function(file) {
  316. var time = times[file];
  317. obj[file] = time;
  318. if(time > selfTime)
  319. selfTime = time;
  320. });
  321. }, this);
  322. obj[this.path] = selfTime;
  323. }
  324. return obj;
  325. };
  326. DirectoryWatcher.prototype.close = function() {
  327. this.initialScan = false;
  328. this.watcher.close();
  329. if(this.nestedWatching) {
  330. Object.keys(this.directories).forEach(function(dir) {
  331. this.directories[dir].close();
  332. }, this);
  333. }
  334. if(this.parentWatcher) this.parentWatcher.close();
  335. this.emit("closed");
  336. };
  337. function ensureFsAccuracy(mtime) {
  338. if(!mtime) return;
  339. if(FS_ACCURACY > 1 && mtime % 1 !== 0)
  340. FS_ACCURACY = 1;
  341. else if(FS_ACCURACY > 10 && mtime % 10 !== 0)
  342. FS_ACCURACY = 10;
  343. else if(FS_ACCURACY > 100 && mtime % 100 !== 0)
  344. FS_ACCURACY = 100;
  345. }