gaze.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /*
  2. * gaze
  3. * https://github.com/shama/gaze
  4. *
  5. * Copyright (c) 2018 Kyle Robinson Young
  6. * Licensed under the MIT license.
  7. */
  8. 'use strict';
  9. // libs
  10. var util = require('util');
  11. var EE = require('events').EventEmitter;
  12. var fs = require('fs');
  13. var path = require('path');
  14. var globule = require('globule');
  15. var helper = require('./helper');
  16. // shim setImmediate for node v0.8
  17. var setImmediate = require('timers').setImmediate;
  18. if (typeof setImmediate !== 'function') {
  19. setImmediate = process.nextTick;
  20. }
  21. // globals
  22. var delay = 10;
  23. // `Gaze` EventEmitter object to return in the callback
  24. function Gaze (patterns, opts, done) {
  25. var self = this;
  26. EE.call(self);
  27. // If second arg is the callback
  28. if (typeof opts === 'function') {
  29. done = opts;
  30. opts = {};
  31. }
  32. // Default options
  33. opts = opts || {};
  34. opts.mark = true;
  35. opts.interval = opts.interval || 100;
  36. opts.debounceDelay = opts.debounceDelay || 500;
  37. opts.cwd = opts.cwd || process.cwd();
  38. this.options = opts;
  39. // Default done callback
  40. done = done || function () {};
  41. // Remember our watched dir:files
  42. this._watched = Object.create(null);
  43. // Store watchers
  44. this._watchers = Object.create(null);
  45. // Store watchFile listeners
  46. this._pollers = Object.create(null);
  47. // Store patterns
  48. this._patterns = [];
  49. // Cached events for debouncing
  50. this._cached = Object.create(null);
  51. // Set maxListeners
  52. if (this.options.maxListeners != null) {
  53. this.setMaxListeners(this.options.maxListeners);
  54. Gaze.super_.prototype.setMaxListeners(this.options.maxListeners);
  55. delete this.options.maxListeners;
  56. }
  57. // Initialize the watch on files
  58. if (patterns) {
  59. this.add(patterns, done);
  60. }
  61. // keep the process alive
  62. this._keepalive = setInterval(function () {}, 200);
  63. return this;
  64. }
  65. util.inherits(Gaze, EE);
  66. // Main entry point. Start watching and call done when setup
  67. module.exports = function gaze (patterns, opts, done) {
  68. return new Gaze(patterns, opts, done);
  69. };
  70. module.exports.Gaze = Gaze;
  71. // Override the emit function to emit `all` events
  72. // and debounce on duplicate events per file
  73. Gaze.prototype.emit = function () {
  74. var self = this;
  75. var args = arguments;
  76. var e = args[0];
  77. var filepath = args[1];
  78. var timeoutId;
  79. // If not added/deleted/changed/renamed then just emit the event
  80. if (e.slice(-2) !== 'ed') {
  81. Gaze.super_.prototype.emit.apply(self, args);
  82. return this;
  83. }
  84. // Detect rename event, if added and previous deleted is in the cache
  85. if (e === 'added') {
  86. Object.keys(this._cached).forEach(function (oldFile) {
  87. if (self._cached[oldFile].indexOf('deleted') !== -1) {
  88. args[0] = e = 'renamed';
  89. [].push.call(args, oldFile);
  90. delete self._cached[oldFile];
  91. return false;
  92. }
  93. });
  94. }
  95. // If cached doesnt exist, create a delay before running the next
  96. // then emit the event
  97. var cache = this._cached[filepath] || [];
  98. if (cache.indexOf(e) === -1) {
  99. helper.objectPush(self._cached, filepath, e);
  100. clearTimeout(timeoutId);
  101. timeoutId = setTimeout(function () {
  102. delete self._cached[filepath];
  103. }, this.options.debounceDelay);
  104. // Emit the event and `all` event
  105. Gaze.super_.prototype.emit.apply(self, args);
  106. Gaze.super_.prototype.emit.apply(self, ['all', e].concat([].slice.call(args, 1)));
  107. }
  108. // Detect if new folder added to trigger for matching files within folder
  109. if (e === 'added') {
  110. if (helper.isDir(filepath)) {
  111. // It's possible that between `isDir` and `readdirSync()` calls the `filepath`
  112. // gets removed, which will result in `ENOENT` exception
  113. var files;
  114. try {
  115. files = fs.readdirSync(filepath);
  116. } catch (e) {
  117. // Rethrow the error if it's anything other than `ENOENT`
  118. if (e.code !== 'ENOENT') {
  119. throw e;
  120. }
  121. files = [];
  122. }
  123. files.map(function (file) {
  124. return path.join(filepath, file);
  125. }).filter(function (file) {
  126. return globule.isMatch(self._patterns, file, self.options);
  127. }).forEach(function (file) {
  128. self.emit('added', file);
  129. });
  130. }
  131. }
  132. return this;
  133. };
  134. // Close watchers
  135. Gaze.prototype.close = function (_reset) {
  136. var self = this;
  137. Object.keys(self._watchers).forEach(function (file) {
  138. self._watchers[file].close();
  139. });
  140. self._watchers = Object.create(null);
  141. Object.keys(this._watched).forEach(function (dir) {
  142. self._unpollDir(dir);
  143. });
  144. if (_reset !== false) {
  145. self._watched = Object.create(null);
  146. setTimeout(function () {
  147. self.emit('end');
  148. self.removeAllListeners();
  149. clearInterval(self._keepalive);
  150. }, delay + 100);
  151. }
  152. return self;
  153. };
  154. // Add file patterns to be watched
  155. Gaze.prototype.add = function (files, done) {
  156. if (typeof files === 'string') { files = [files]; }
  157. this._patterns = helper.unique.apply(null, [this._patterns, files]);
  158. files = globule.find(this._patterns, this.options);
  159. this._addToWatched(files);
  160. this.close(false);
  161. this._initWatched(done);
  162. };
  163. // Dont increment patterns and dont call done if nothing added
  164. Gaze.prototype._internalAdd = function (file, done) {
  165. var files = [];
  166. if (helper.isDir(file)) {
  167. files = [helper.markDir(file)].concat(globule.find(this._patterns, this.options));
  168. } else {
  169. if (globule.isMatch(this._patterns, file, this.options)) {
  170. files = [file];
  171. }
  172. }
  173. if (files.length > 0) {
  174. this._addToWatched(files);
  175. this.close(false);
  176. this._initWatched(done);
  177. }
  178. };
  179. // Remove file/dir from `watched`
  180. Gaze.prototype.remove = function (file) {
  181. var self = this;
  182. if (this._watched[file]) {
  183. // is dir, remove all files
  184. this._unpollDir(file);
  185. delete this._watched[file];
  186. } else {
  187. // is a file, find and remove
  188. Object.keys(this._watched).forEach(function (dir) {
  189. var index = self._watched[dir].indexOf(file);
  190. if (index !== -1) {
  191. self._unpollFile(file);
  192. self._watched[dir].splice(index, 1);
  193. return false;
  194. }
  195. });
  196. }
  197. if (this._watchers[file]) {
  198. this._watchers[file].close();
  199. }
  200. return this;
  201. };
  202. // Return watched files
  203. Gaze.prototype.watched = function () {
  204. return this._watched;
  205. };
  206. // Returns `watched` files with relative paths to process.cwd()
  207. Gaze.prototype.relative = function (dir, unixify) {
  208. var self = this;
  209. var relative = Object.create(null);
  210. var relDir, relFile, unixRelDir;
  211. var cwd = this.options.cwd || process.cwd();
  212. if (dir === '') { dir = '.'; }
  213. dir = helper.markDir(dir);
  214. unixify = unixify || false;
  215. Object.keys(this._watched).forEach(function (dir) {
  216. relDir = path.relative(cwd, dir) + path.sep;
  217. if (relDir === path.sep) { relDir = '.'; }
  218. unixRelDir = unixify ? helper.unixifyPathSep(relDir) : relDir;
  219. relative[unixRelDir] = self._watched[dir].map(function (file) {
  220. relFile = path.relative(path.join(cwd, relDir) || '', file || '');
  221. if (helper.isDir(file)) {
  222. relFile = helper.markDir(relFile);
  223. }
  224. if (unixify) {
  225. relFile = helper.unixifyPathSep(relFile);
  226. }
  227. return relFile;
  228. });
  229. });
  230. if (dir && unixify) {
  231. dir = helper.unixifyPathSep(dir);
  232. }
  233. return dir ? relative[dir] || [] : relative;
  234. };
  235. // Adds files and dirs to watched
  236. Gaze.prototype._addToWatched = function (files) {
  237. var dirs = [];
  238. for (var i = 0; i < files.length; i++) {
  239. var file = files[i];
  240. var filepath = path.resolve(this.options.cwd, file);
  241. var dirname = (helper.isDir(file)) ? filepath : path.dirname(filepath);
  242. dirname = helper.markDir(dirname);
  243. // If a new dir is added
  244. if (helper.isDir(file) && !(dirname in this._watched)) {
  245. helper.objectPush(this._watched, dirname, []);
  246. }
  247. if (file.slice(-1) === '/') { filepath += path.sep; }
  248. helper.objectPush(this._watched, path.dirname(filepath) + path.sep, filepath);
  249. dirs.push(dirname);
  250. }
  251. dirs = helper.unique(dirs);
  252. for (var k = 0; k < dirs.length; k++) {
  253. dirname = dirs[k];
  254. // add folders into the mix
  255. var readdir = fs.readdirSync(dirname);
  256. for (var j = 0; j < readdir.length; j++) {
  257. var dirfile = path.join(dirname, readdir[j]);
  258. if (fs.lstatSync(dirfile).isDirectory()) {
  259. helper.objectPush(this._watched, dirname, dirfile + path.sep);
  260. }
  261. }
  262. }
  263. return this;
  264. };
  265. Gaze.prototype._watchDir = function (dir, done) {
  266. var self = this;
  267. var timeoutId;
  268. try {
  269. this._watchers[dir] = fs.watch(dir, function (event) {
  270. // race condition. Let's give the fs a little time to settle down. so we
  271. // don't fire events on non existent files.
  272. clearTimeout(timeoutId);
  273. timeoutId = setTimeout(function () {
  274. // race condition. Ensure that this directory is still being watched
  275. // before continuing.
  276. if ((dir in self._watchers) && fs.existsSync(dir)) {
  277. done(null, dir);
  278. }
  279. }, delay + 100);
  280. });
  281. this._watchers[dir].on('error', function (err) {
  282. self._handleError(err);
  283. });
  284. } catch (err) {
  285. return this._handleError(err);
  286. }
  287. return this;
  288. };
  289. Gaze.prototype._unpollFile = function (file) {
  290. if (this._pollers[file]) {
  291. fs.unwatchFile(file, this._pollers[file]);
  292. delete this._pollers[file];
  293. }
  294. return this;
  295. };
  296. Gaze.prototype._unpollDir = function (dir) {
  297. this._unpollFile(dir);
  298. for (var i = 0; i < this._watched[dir].length; i++) {
  299. this._unpollFile(this._watched[dir][i]);
  300. }
  301. };
  302. Gaze.prototype._pollFile = function (file, done) {
  303. var opts = { persistent: true, interval: this.options.interval };
  304. if (!this._pollers[file]) {
  305. this._pollers[file] = function (curr, prev) {
  306. done(null, file);
  307. };
  308. try {
  309. fs.watchFile(file, opts, this._pollers[file]);
  310. } catch (err) {
  311. return this._handleError(err);
  312. }
  313. }
  314. return this;
  315. };
  316. // Initialize the actual watch on `watched` files
  317. Gaze.prototype._initWatched = function (done) {
  318. var self = this;
  319. var cwd = this.options.cwd || process.cwd();
  320. var curWatched = Object.keys(self._watched);
  321. // if no matching files
  322. if (curWatched.length < 1) {
  323. // Defer to emitting to give a chance to attach event handlers.
  324. setImmediate(function () {
  325. self.emit('ready', self);
  326. if (done) { done.call(self, null, self); }
  327. self.emit('nomatch');
  328. });
  329. return;
  330. }
  331. helper.forEachSeries(curWatched, function (dir, next) {
  332. dir = dir || '';
  333. var files = self._watched[dir];
  334. // Triggered when a watched dir has an event
  335. self._watchDir(dir, function (event, dirpath) {
  336. var relDir = cwd === dir ? '.' : path.relative(cwd, dir);
  337. relDir = relDir || '';
  338. fs.readdir(dirpath, function (err, current) {
  339. if (err) { return self.emit('error', err); }
  340. if (!current) { return; }
  341. try {
  342. // append path.sep to directories so they match previous.
  343. current = current.map(function (curPath) {
  344. if (fs.existsSync(path.join(dir, curPath)) && fs.lstatSync(path.join(dir, curPath)).isDirectory()) {
  345. return curPath + path.sep;
  346. } else {
  347. return curPath;
  348. }
  349. });
  350. } catch (err) {
  351. // race condition-- sometimes the file no longer exists
  352. }
  353. // Get watched files for this dir
  354. var previous = self.relative(relDir);
  355. // If file was deleted
  356. previous.filter(function (file) {
  357. return current.indexOf(file) < 0;
  358. }).forEach(function (file) {
  359. if (!helper.isDir(file)) {
  360. var filepath = path.join(dir, file);
  361. self.remove(filepath);
  362. self.emit('deleted', filepath);
  363. }
  364. });
  365. // If file was added
  366. current.filter(function (file) {
  367. return previous.indexOf(file) < 0;
  368. }).forEach(function (file) {
  369. // Is it a matching pattern?
  370. var relFile = path.join(relDir, file);
  371. // Add to watch then emit event
  372. self._internalAdd(relFile, function () {
  373. self.emit('added', path.join(dir, file));
  374. });
  375. });
  376. });
  377. });
  378. // Watch for change/rename events on files
  379. files.forEach(function (file) {
  380. if (helper.isDir(file)) { return; }
  381. self._pollFile(file, function (err, filepath) {
  382. if (err) {
  383. self.emit('error', err);
  384. return;
  385. }
  386. // Only emit changed if the file still exists
  387. // Prevents changed/deleted duplicate events
  388. if (fs.existsSync(filepath)) {
  389. self.emit('changed', filepath);
  390. }
  391. });
  392. });
  393. next();
  394. }, function () {
  395. // Return this instance of Gaze
  396. // delay before ready solves a lot of issues
  397. setTimeout(function () {
  398. self.emit('ready', self);
  399. if (done) { done.call(self, null, self); }
  400. }, delay + 100);
  401. });
  402. };
  403. // If an error, handle it here
  404. Gaze.prototype._handleError = function (err) {
  405. if (err.code === 'EMFILE') {
  406. return this.emit('error', new Error('EMFILE: Too many opened files.'));
  407. }
  408. return this.emit('error', err);
  409. };