FSEventsWatcher.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.FSEventsWatcher = void 0;
  6. function _events() {
  7. const data = require('events');
  8. _events = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function path() {
  14. const data = _interopRequireWildcard(require('path'));
  15. path = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _anymatch() {
  21. const data = _interopRequireDefault(require('anymatch'));
  22. _anymatch = function () {
  23. return data;
  24. };
  25. return data;
  26. }
  27. function fs() {
  28. const data = _interopRequireWildcard(require('graceful-fs'));
  29. fs = function () {
  30. return data;
  31. };
  32. return data;
  33. }
  34. function _micromatch() {
  35. const data = _interopRequireDefault(require('micromatch'));
  36. _micromatch = function () {
  37. return data;
  38. };
  39. return data;
  40. }
  41. function _walker() {
  42. const data = _interopRequireDefault(require('walker'));
  43. _walker = function () {
  44. return data;
  45. };
  46. return data;
  47. }
  48. function _interopRequireDefault(obj) {
  49. return obj && obj.__esModule ? obj : {default: obj};
  50. }
  51. function _getRequireWildcardCache(nodeInterop) {
  52. if (typeof WeakMap !== 'function') return null;
  53. var cacheBabelInterop = new WeakMap();
  54. var cacheNodeInterop = new WeakMap();
  55. return (_getRequireWildcardCache = function (nodeInterop) {
  56. return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
  57. })(nodeInterop);
  58. }
  59. function _interopRequireWildcard(obj, nodeInterop) {
  60. if (!nodeInterop && obj && obj.__esModule) {
  61. return obj;
  62. }
  63. if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
  64. return {default: obj};
  65. }
  66. var cache = _getRequireWildcardCache(nodeInterop);
  67. if (cache && cache.has(obj)) {
  68. return cache.get(obj);
  69. }
  70. var newObj = {};
  71. var hasPropertyDescriptor =
  72. Object.defineProperty && Object.getOwnPropertyDescriptor;
  73. for (var key in obj) {
  74. if (key !== 'default' && Object.prototype.hasOwnProperty.call(obj, key)) {
  75. var desc = hasPropertyDescriptor
  76. ? Object.getOwnPropertyDescriptor(obj, key)
  77. : null;
  78. if (desc && (desc.get || desc.set)) {
  79. Object.defineProperty(newObj, key, desc);
  80. } else {
  81. newObj[key] = obj[key];
  82. }
  83. }
  84. }
  85. newObj.default = obj;
  86. if (cache) {
  87. cache.set(obj, newObj);
  88. }
  89. return newObj;
  90. }
  91. /**
  92. * Copyright (c) Meta Platforms, Inc. and affiliates.
  93. *
  94. * This source code is licensed under the MIT license found in the
  95. * LICENSE file in the root directory of this source tree.
  96. *
  97. */
  98. // @ts-expect-error no types
  99. // eslint-disable-next-line @typescript-eslint/prefer-ts-expect-error, @typescript-eslint/ban-ts-comment
  100. // @ts-ignore: this is for CI which runs linux and might not have this
  101. let fsevents = null;
  102. try {
  103. fsevents = require('fsevents');
  104. } catch {
  105. // Optional dependency, only supported on Darwin.
  106. }
  107. const CHANGE_EVENT = 'change';
  108. const DELETE_EVENT = 'delete';
  109. const ADD_EVENT = 'add';
  110. const ALL_EVENT = 'all';
  111. /**
  112. * Export `FSEventsWatcher` class.
  113. * Watches `dir`.
  114. */
  115. class FSEventsWatcher extends _events().EventEmitter {
  116. root;
  117. ignored;
  118. glob;
  119. dot;
  120. hasIgnore;
  121. doIgnore;
  122. fsEventsWatchStopper;
  123. _tracked;
  124. static isSupported() {
  125. return fsevents !== null;
  126. }
  127. static normalizeProxy(callback) {
  128. return (filepath, stats) => callback(path().normalize(filepath), stats);
  129. }
  130. static recReaddir(
  131. dir,
  132. dirCallback,
  133. fileCallback,
  134. endCallback,
  135. errorCallback,
  136. ignored
  137. ) {
  138. (0, _walker().default)(dir)
  139. .filterDir(
  140. currentDir => !ignored || !(0, _anymatch().default)(ignored, currentDir)
  141. )
  142. .on('dir', FSEventsWatcher.normalizeProxy(dirCallback))
  143. .on('file', FSEventsWatcher.normalizeProxy(fileCallback))
  144. .on('error', errorCallback)
  145. .on('end', () => {
  146. endCallback();
  147. });
  148. }
  149. constructor(dir, opts) {
  150. if (!fsevents) {
  151. throw new Error(
  152. '`fsevents` unavailable (this watcher can only be used on Darwin)'
  153. );
  154. }
  155. super();
  156. this.dot = opts.dot || false;
  157. this.ignored = opts.ignored;
  158. this.glob = Array.isArray(opts.glob) ? opts.glob : [opts.glob];
  159. this.hasIgnore =
  160. Boolean(opts.ignored) && !(Array.isArray(opts) && opts.length > 0);
  161. this.doIgnore = opts.ignored
  162. ? (0, _anymatch().default)(opts.ignored)
  163. : () => false;
  164. this.root = path().resolve(dir);
  165. this.fsEventsWatchStopper = fsevents.watch(
  166. this.root,
  167. this.handleEvent.bind(this)
  168. );
  169. this._tracked = new Set();
  170. FSEventsWatcher.recReaddir(
  171. this.root,
  172. filepath => {
  173. this._tracked.add(filepath);
  174. },
  175. filepath => {
  176. this._tracked.add(filepath);
  177. },
  178. this.emit.bind(this, 'ready'),
  179. this.emit.bind(this, 'error'),
  180. this.ignored
  181. );
  182. }
  183. /**
  184. * End watching.
  185. */
  186. async close(callback) {
  187. await this.fsEventsWatchStopper();
  188. this.removeAllListeners();
  189. if (typeof callback === 'function') {
  190. process.nextTick(() => callback());
  191. }
  192. }
  193. isFileIncluded(relativePath) {
  194. if (this.doIgnore(relativePath)) {
  195. return false;
  196. }
  197. return this.glob.length
  198. ? (0, _micromatch().default)([relativePath], this.glob, {
  199. dot: this.dot
  200. }).length > 0
  201. : this.dot ||
  202. (0, _micromatch().default)([relativePath], '**/*').length > 0;
  203. }
  204. handleEvent(filepath) {
  205. const relativePath = path().relative(this.root, filepath);
  206. if (!this.isFileIncluded(relativePath)) {
  207. return;
  208. }
  209. fs().lstat(filepath, (error, stat) => {
  210. if (error && error.code !== 'ENOENT') {
  211. this.emit('error', error);
  212. return;
  213. }
  214. if (error) {
  215. // Ignore files that aren't tracked and don't exist.
  216. if (!this._tracked.has(filepath)) {
  217. return;
  218. }
  219. this._emit(DELETE_EVENT, relativePath);
  220. this._tracked.delete(filepath);
  221. return;
  222. }
  223. if (this._tracked.has(filepath)) {
  224. this._emit(CHANGE_EVENT, relativePath, stat);
  225. } else {
  226. this._tracked.add(filepath);
  227. this._emit(ADD_EVENT, relativePath, stat);
  228. }
  229. });
  230. }
  231. /**
  232. * Emit events.
  233. */
  234. _emit(type, file, stat) {
  235. this.emit(type, file, this.root, stat);
  236. this.emit(ALL_EVENT, type, file, this.root, stat);
  237. }
  238. }
  239. exports.FSEventsWatcher = FSEventsWatcher;