FSEventsWatcher.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. 'use strict';
  2. function _fs() {
  3. const data = _interopRequireDefault(require('fs'));
  4. _fs = function _fs() {
  5. return data;
  6. };
  7. return data;
  8. }
  9. function _path() {
  10. const data = _interopRequireDefault(require('path'));
  11. _path = function _path() {
  12. return data;
  13. };
  14. return data;
  15. }
  16. function _events() {
  17. const data = require('events');
  18. _events = function _events() {
  19. return data;
  20. };
  21. return data;
  22. }
  23. function _anymatch() {
  24. const data = _interopRequireDefault(require('anymatch'));
  25. _anymatch = function _anymatch() {
  26. return data;
  27. };
  28. return data;
  29. }
  30. function _micromatch() {
  31. const data = _interopRequireDefault(require('micromatch'));
  32. _micromatch = function _micromatch() {
  33. return data;
  34. };
  35. return data;
  36. }
  37. function _walker() {
  38. const data = _interopRequireDefault(require('walker'));
  39. _walker = function _walker() {
  40. return data;
  41. };
  42. return data;
  43. }
  44. function _interopRequireDefault(obj) {
  45. return obj && obj.__esModule ? obj : {default: obj};
  46. }
  47. function _defineProperty(obj, key, value) {
  48. if (key in obj) {
  49. Object.defineProperty(obj, key, {
  50. value: value,
  51. enumerable: true,
  52. configurable: true,
  53. writable: true
  54. });
  55. } else {
  56. obj[key] = value;
  57. }
  58. return obj;
  59. }
  60. let fsevents;
  61. try {
  62. fsevents = require('fsevents');
  63. } catch (e) {
  64. // Optional dependency, only supported on Darwin.
  65. }
  66. const CHANGE_EVENT = 'change';
  67. const DELETE_EVENT = 'delete';
  68. const ADD_EVENT = 'add';
  69. const ALL_EVENT = 'all';
  70. /**
  71. * Export `FSEventsWatcher` class.
  72. * Watches `dir`.
  73. */
  74. class FSEventsWatcher extends _events().EventEmitter {
  75. static isSupported() {
  76. return fsevents !== undefined;
  77. }
  78. static normalizeProxy(callback) {
  79. return (filepath, stats) =>
  80. callback(_path().default.normalize(filepath), stats);
  81. }
  82. static recReaddir(
  83. dir,
  84. dirCallback,
  85. fileCallback,
  86. endCallback,
  87. errorCallback,
  88. ignored
  89. ) {
  90. (0, _walker().default)(dir)
  91. .filterDir(
  92. currentDir => !ignored || !(0, _anymatch().default)(ignored, currentDir)
  93. )
  94. .on('dir', FSEventsWatcher.normalizeProxy(dirCallback))
  95. .on('file', FSEventsWatcher.normalizeProxy(fileCallback))
  96. .on('error', errorCallback)
  97. .on('end', () => {
  98. endCallback();
  99. });
  100. }
  101. constructor(dir, opts) {
  102. if (!fsevents) {
  103. throw new Error(
  104. '`fsevents` unavailable (this watcher can only be used on Darwin)'
  105. );
  106. }
  107. super();
  108. _defineProperty(this, 'root', void 0);
  109. _defineProperty(this, 'ignored', void 0);
  110. _defineProperty(this, 'glob', void 0);
  111. _defineProperty(this, 'dot', void 0);
  112. _defineProperty(this, 'hasIgnore', void 0);
  113. _defineProperty(this, 'doIgnore', void 0);
  114. _defineProperty(this, 'watcher', void 0);
  115. _defineProperty(this, '_tracked', void 0);
  116. this.dot = opts.dot || false;
  117. this.ignored = opts.ignored;
  118. this.glob = Array.isArray(opts.glob) ? opts.glob : [opts.glob];
  119. this.hasIgnore =
  120. Boolean(opts.ignored) && !(Array.isArray(opts) && opts.length > 0);
  121. this.doIgnore = opts.ignored
  122. ? (0, _anymatch().default)(opts.ignored)
  123. : () => false;
  124. this.root = _path().default.resolve(dir);
  125. this.watcher = fsevents(this.root);
  126. this.watcher.start().on('change', this.handleEvent.bind(this));
  127. this._tracked = new Set();
  128. FSEventsWatcher.recReaddir(
  129. this.root,
  130. filepath => {
  131. this._tracked.add(filepath);
  132. },
  133. filepath => {
  134. this._tracked.add(filepath);
  135. },
  136. this.emit.bind(this, 'ready'),
  137. this.emit.bind(this, 'error'),
  138. this.ignored
  139. );
  140. }
  141. /**
  142. * End watching.
  143. */
  144. close(callback) {
  145. this.watcher.stop();
  146. this.removeAllListeners();
  147. if (typeof callback === 'function') {
  148. process.nextTick(callback.bind(null, null, true));
  149. }
  150. }
  151. isFileIncluded(relativePath) {
  152. if (this.doIgnore(relativePath)) {
  153. return false;
  154. }
  155. return this.glob.length
  156. ? _micromatch().default.some(relativePath, this.glob, {
  157. dot: this.dot
  158. })
  159. : this.dot || _micromatch().default.some(relativePath, '**/*');
  160. }
  161. handleEvent(filepath) {
  162. const relativePath = _path().default.relative(this.root, filepath);
  163. if (!this.isFileIncluded(relativePath)) {
  164. return;
  165. }
  166. _fs().default.lstat(filepath, (error, stat) => {
  167. if (error && error.code !== 'ENOENT') {
  168. this.emit('error', error);
  169. return;
  170. }
  171. if (error) {
  172. // Ignore files that aren't tracked and don't exist.
  173. if (!this._tracked.has(filepath)) {
  174. return;
  175. }
  176. this._emit(DELETE_EVENT, relativePath);
  177. this._tracked.delete(filepath);
  178. return;
  179. }
  180. if (this._tracked.has(filepath)) {
  181. this._emit(CHANGE_EVENT, relativePath, stat);
  182. } else {
  183. this._tracked.add(filepath);
  184. this._emit(ADD_EVENT, relativePath, stat);
  185. }
  186. });
  187. }
  188. /**
  189. * Emit events.
  190. */
  191. _emit(type, file, stat) {
  192. this.emit(type, file, this.root, stat);
  193. this.emit(ALL_EVENT, type, file, this.root, stat);
  194. }
  195. }
  196. module.exports = FSEventsWatcher;