index.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.FSWatcher = exports.WatchHelper = void 0;
  4. exports.watch = watch;
  5. const fs_1 = require("fs");
  6. const promises_1 = require("fs/promises");
  7. const events_1 = require("events");
  8. const sysPath = require("path");
  9. const readdirp_1 = require("readdirp");
  10. const handler_js_1 = require("./handler.js");
  11. const SLASH = '/';
  12. const SLASH_SLASH = '//';
  13. const ONE_DOT = '.';
  14. const TWO_DOTS = '..';
  15. const STRING_TYPE = 'string';
  16. const BACK_SLASH_RE = /\\/g;
  17. const DOUBLE_SLASH_RE = /\/\//;
  18. const DOT_RE = /\..*\.(sw[px])$|~$|\.subl.*\.tmp/;
  19. const REPLACER_RE = /^\.[/\\]/;
  20. function arrify(item) {
  21. return Array.isArray(item) ? item : [item];
  22. }
  23. const isMatcherObject = (matcher) => typeof matcher === 'object' && matcher !== null && !(matcher instanceof RegExp);
  24. function createPattern(matcher) {
  25. if (typeof matcher === 'function')
  26. return matcher;
  27. if (typeof matcher === 'string')
  28. return (string) => matcher === string;
  29. if (matcher instanceof RegExp)
  30. return (string) => matcher.test(string);
  31. if (typeof matcher === 'object' && matcher !== null) {
  32. return (string) => {
  33. if (matcher.path === string)
  34. return true;
  35. if (matcher.recursive) {
  36. const relative = sysPath.relative(matcher.path, string);
  37. if (!relative) {
  38. return false;
  39. }
  40. return !relative.startsWith('..') && !sysPath.isAbsolute(relative);
  41. }
  42. return false;
  43. };
  44. }
  45. return () => false;
  46. }
  47. function normalizePath(path) {
  48. if (typeof path !== 'string')
  49. throw new Error('string expected');
  50. path = sysPath.normalize(path);
  51. path = path.replace(/\\/g, '/');
  52. let prepend = false;
  53. if (path.startsWith('//'))
  54. prepend = true;
  55. const DOUBLE_SLASH_RE = /\/\//;
  56. while (path.match(DOUBLE_SLASH_RE))
  57. path = path.replace(DOUBLE_SLASH_RE, '/');
  58. if (prepend)
  59. path = '/' + path;
  60. return path;
  61. }
  62. function matchPatterns(patterns, testString, stats) {
  63. const path = normalizePath(testString);
  64. for (let index = 0; index < patterns.length; index++) {
  65. const pattern = patterns[index];
  66. if (pattern(path, stats)) {
  67. return true;
  68. }
  69. }
  70. return false;
  71. }
  72. function anymatch(matchers, testString) {
  73. if (matchers == null) {
  74. throw new TypeError('anymatch: specify first argument');
  75. }
  76. // Early cache for matchers.
  77. const matchersArray = arrify(matchers);
  78. const patterns = matchersArray.map((matcher) => createPattern(matcher));
  79. if (testString == null) {
  80. return (testString, stats) => {
  81. return matchPatterns(patterns, testString, stats);
  82. };
  83. }
  84. return matchPatterns(patterns, testString);
  85. }
  86. const unifyPaths = (paths_) => {
  87. const paths = arrify(paths_).flat();
  88. if (!paths.every((p) => typeof p === STRING_TYPE)) {
  89. throw new TypeError(`Non-string provided as watch path: ${paths}`);
  90. }
  91. return paths.map(normalizePathToUnix);
  92. };
  93. // If SLASH_SLASH occurs at the beginning of path, it is not replaced
  94. // because "//StoragePC/DrivePool/Movies" is a valid network path
  95. const toUnix = (string) => {
  96. let str = string.replace(BACK_SLASH_RE, SLASH);
  97. let prepend = false;
  98. if (str.startsWith(SLASH_SLASH)) {
  99. prepend = true;
  100. }
  101. while (str.match(DOUBLE_SLASH_RE)) {
  102. str = str.replace(DOUBLE_SLASH_RE, SLASH);
  103. }
  104. if (prepend) {
  105. str = SLASH + str;
  106. }
  107. return str;
  108. };
  109. // Our version of upath.normalize
  110. // TODO: this is not equal to path-normalize module - investigate why
  111. const normalizePathToUnix = (path) => toUnix(sysPath.normalize(toUnix(path)));
  112. // TODO: refactor
  113. const normalizeIgnored = (cwd = '') => (path) => {
  114. if (typeof path === 'string') {
  115. return normalizePathToUnix(sysPath.isAbsolute(path) ? path : sysPath.join(cwd, path));
  116. }
  117. else {
  118. return path;
  119. }
  120. };
  121. const getAbsolutePath = (path, cwd) => {
  122. if (sysPath.isAbsolute(path)) {
  123. return path;
  124. }
  125. return sysPath.join(cwd, path);
  126. };
  127. const EMPTY_SET = Object.freeze(new Set());
  128. /**
  129. * Directory entry.
  130. */
  131. class DirEntry {
  132. constructor(dir, removeWatcher) {
  133. this.path = dir;
  134. this._removeWatcher = removeWatcher;
  135. this.items = new Set();
  136. }
  137. add(item) {
  138. const { items } = this;
  139. if (!items)
  140. return;
  141. if (item !== ONE_DOT && item !== TWO_DOTS)
  142. items.add(item);
  143. }
  144. async remove(item) {
  145. const { items } = this;
  146. if (!items)
  147. return;
  148. items.delete(item);
  149. if (items.size > 0)
  150. return;
  151. const dir = this.path;
  152. try {
  153. await (0, promises_1.readdir)(dir);
  154. }
  155. catch (err) {
  156. if (this._removeWatcher) {
  157. this._removeWatcher(sysPath.dirname(dir), sysPath.basename(dir));
  158. }
  159. }
  160. }
  161. has(item) {
  162. const { items } = this;
  163. if (!items)
  164. return;
  165. return items.has(item);
  166. }
  167. getChildren() {
  168. const { items } = this;
  169. if (!items)
  170. return [];
  171. return [...items.values()];
  172. }
  173. dispose() {
  174. this.items.clear();
  175. this.path = '';
  176. this._removeWatcher = handler_js_1.EMPTY_FN;
  177. this.items = EMPTY_SET;
  178. Object.freeze(this);
  179. }
  180. }
  181. const STAT_METHOD_F = 'stat';
  182. const STAT_METHOD_L = 'lstat';
  183. class WatchHelper {
  184. constructor(path, follow, fsw) {
  185. this.fsw = fsw;
  186. const watchPath = path;
  187. this.path = path = path.replace(REPLACER_RE, '');
  188. this.watchPath = watchPath;
  189. this.fullWatchPath = sysPath.resolve(watchPath);
  190. this.dirParts = [];
  191. this.dirParts.forEach((parts) => {
  192. if (parts.length > 1)
  193. parts.pop();
  194. });
  195. this.followSymlinks = follow;
  196. this.statMethod = follow ? STAT_METHOD_F : STAT_METHOD_L;
  197. }
  198. entryPath(entry) {
  199. return sysPath.join(this.watchPath, sysPath.relative(this.watchPath, entry.fullPath));
  200. }
  201. filterPath(entry) {
  202. const { stats } = entry;
  203. if (stats && stats.isSymbolicLink())
  204. return this.filterDir(entry);
  205. const resolvedPath = this.entryPath(entry);
  206. // TODO: what if stats is undefined? remove !
  207. return this.fsw._isntIgnored(resolvedPath, stats) && this.fsw._hasReadPermissions(stats);
  208. }
  209. filterDir(entry) {
  210. return this.fsw._isntIgnored(this.entryPath(entry), entry.stats);
  211. }
  212. }
  213. exports.WatchHelper = WatchHelper;
  214. /**
  215. * Watches files & directories for changes. Emitted events:
  216. * `add`, `addDir`, `change`, `unlink`, `unlinkDir`, `all`, `error`
  217. *
  218. * new FSWatcher()
  219. * .add(directories)
  220. * .on('add', path => log('File', path, 'was added'))
  221. */
  222. class FSWatcher extends events_1.EventEmitter {
  223. // Not indenting methods for history sake; for now.
  224. constructor(_opts = {}) {
  225. super();
  226. this.closed = false;
  227. this._closers = new Map();
  228. this._ignoredPaths = new Set();
  229. this._throttled = new Map();
  230. this._streams = new Set();
  231. this._symlinkPaths = new Map();
  232. this._watched = new Map();
  233. this._pendingWrites = new Map();
  234. this._pendingUnlinks = new Map();
  235. this._readyCount = 0;
  236. this._readyEmitted = false;
  237. const awf = _opts.awaitWriteFinish;
  238. const DEF_AWF = { stabilityThreshold: 2000, pollInterval: 100 };
  239. const opts = {
  240. // Defaults
  241. persistent: true,
  242. ignoreInitial: false,
  243. ignorePermissionErrors: false,
  244. interval: 100,
  245. binaryInterval: 300,
  246. followSymlinks: true,
  247. usePolling: false,
  248. // useAsync: false,
  249. atomic: true, // NOTE: overwritten later (depends on usePolling)
  250. ..._opts,
  251. // Change format
  252. ignored: _opts.ignored ? arrify(_opts.ignored) : arrify([]),
  253. awaitWriteFinish: awf === true ? DEF_AWF : typeof awf === 'object' ? { ...DEF_AWF, ...awf } : false,
  254. };
  255. // Always default to polling on IBM i because fs.watch() is not available on IBM i.
  256. if (handler_js_1.isIBMi)
  257. opts.usePolling = true;
  258. // Editor atomic write normalization enabled by default with fs.watch
  259. if (opts.atomic === undefined)
  260. opts.atomic = !opts.usePolling;
  261. // opts.atomic = typeof _opts.atomic === 'number' ? _opts.atomic : 100;
  262. // Global override. Useful for developers, who need to force polling for all
  263. // instances of chokidar, regardless of usage / dependency depth
  264. const envPoll = process.env.CHOKIDAR_USEPOLLING;
  265. if (envPoll !== undefined) {
  266. const envLower = envPoll.toLowerCase();
  267. if (envLower === 'false' || envLower === '0')
  268. opts.usePolling = false;
  269. else if (envLower === 'true' || envLower === '1')
  270. opts.usePolling = true;
  271. else
  272. opts.usePolling = !!envLower;
  273. }
  274. const envInterval = process.env.CHOKIDAR_INTERVAL;
  275. if (envInterval)
  276. opts.interval = Number.parseInt(envInterval, 10);
  277. // This is done to emit ready only once, but each 'add' will increase that?
  278. let readyCalls = 0;
  279. this._emitReady = () => {
  280. readyCalls++;
  281. if (readyCalls >= this._readyCount) {
  282. this._emitReady = handler_js_1.EMPTY_FN;
  283. this._readyEmitted = true;
  284. // use process.nextTick to allow time for listener to be bound
  285. process.nextTick(() => this.emit(handler_js_1.EVENTS.READY));
  286. }
  287. };
  288. this._emitRaw = (...args) => this.emit(handler_js_1.EVENTS.RAW, ...args);
  289. this._boundRemove = this._remove.bind(this);
  290. this.options = opts;
  291. this._nodeFsHandler = new handler_js_1.NodeFsHandler(this);
  292. // You’re frozen when your heart’s not open.
  293. Object.freeze(opts);
  294. }
  295. _addIgnoredPath(matcher) {
  296. if (isMatcherObject(matcher)) {
  297. // return early if we already have a deeply equal matcher object
  298. for (const ignored of this._ignoredPaths) {
  299. if (isMatcherObject(ignored) &&
  300. ignored.path === matcher.path &&
  301. ignored.recursive === matcher.recursive) {
  302. return;
  303. }
  304. }
  305. }
  306. this._ignoredPaths.add(matcher);
  307. }
  308. _removeIgnoredPath(matcher) {
  309. this._ignoredPaths.delete(matcher);
  310. // now find any matcher objects with the matcher as path
  311. if (typeof matcher === 'string') {
  312. for (const ignored of this._ignoredPaths) {
  313. // TODO (43081j): make this more efficient.
  314. // probably just make a `this._ignoredDirectories` or some
  315. // such thing.
  316. if (isMatcherObject(ignored) && ignored.path === matcher) {
  317. this._ignoredPaths.delete(ignored);
  318. }
  319. }
  320. }
  321. }
  322. // Public methods
  323. /**
  324. * Adds paths to be watched on an existing FSWatcher instance.
  325. * @param paths_ file or file list. Other arguments are unused
  326. */
  327. add(paths_, _origAdd, _internal) {
  328. const { cwd } = this.options;
  329. this.closed = false;
  330. this._closePromise = undefined;
  331. let paths = unifyPaths(paths_);
  332. if (cwd) {
  333. paths = paths.map((path) => {
  334. const absPath = getAbsolutePath(path, cwd);
  335. // Check `path` instead of `absPath` because the cwd portion can't be a glob
  336. return absPath;
  337. });
  338. }
  339. paths.forEach((path) => {
  340. this._removeIgnoredPath(path);
  341. });
  342. this._userIgnored = undefined;
  343. if (!this._readyCount)
  344. this._readyCount = 0;
  345. this._readyCount += paths.length;
  346. Promise.all(paths.map(async (path) => {
  347. const res = await this._nodeFsHandler._addToNodeFs(path, !_internal, undefined, 0, _origAdd);
  348. if (res)
  349. this._emitReady();
  350. return res;
  351. })).then((results) => {
  352. if (this.closed)
  353. return;
  354. results.forEach((item) => {
  355. if (item)
  356. this.add(sysPath.dirname(item), sysPath.basename(_origAdd || item));
  357. });
  358. });
  359. return this;
  360. }
  361. /**
  362. * Close watchers or start ignoring events from specified paths.
  363. */
  364. unwatch(paths_) {
  365. if (this.closed)
  366. return this;
  367. const paths = unifyPaths(paths_);
  368. const { cwd } = this.options;
  369. paths.forEach((path) => {
  370. // convert to absolute path unless relative path already matches
  371. if (!sysPath.isAbsolute(path) && !this._closers.has(path)) {
  372. if (cwd)
  373. path = sysPath.join(cwd, path);
  374. path = sysPath.resolve(path);
  375. }
  376. this._closePath(path);
  377. this._addIgnoredPath(path);
  378. if (this._watched.has(path)) {
  379. this._addIgnoredPath({
  380. path,
  381. recursive: true,
  382. });
  383. }
  384. // reset the cached userIgnored anymatch fn
  385. // to make ignoredPaths changes effective
  386. this._userIgnored = undefined;
  387. });
  388. return this;
  389. }
  390. /**
  391. * Close watchers and remove all listeners from watched paths.
  392. */
  393. close() {
  394. if (this._closePromise) {
  395. return this._closePromise;
  396. }
  397. this.closed = true;
  398. // Memory management.
  399. this.removeAllListeners();
  400. const closers = [];
  401. this._closers.forEach((closerList) => closerList.forEach((closer) => {
  402. const promise = closer();
  403. if (promise instanceof Promise)
  404. closers.push(promise);
  405. }));
  406. this._streams.forEach((stream) => stream.destroy());
  407. this._userIgnored = undefined;
  408. this._readyCount = 0;
  409. this._readyEmitted = false;
  410. this._watched.forEach((dirent) => dirent.dispose());
  411. this._closers.clear();
  412. this._watched.clear();
  413. this._streams.clear();
  414. this._symlinkPaths.clear();
  415. this._throttled.clear();
  416. this._closePromise = closers.length
  417. ? Promise.all(closers).then(() => undefined)
  418. : Promise.resolve();
  419. return this._closePromise;
  420. }
  421. /**
  422. * Expose list of watched paths
  423. * @returns for chaining
  424. */
  425. getWatched() {
  426. const watchList = {};
  427. this._watched.forEach((entry, dir) => {
  428. const key = this.options.cwd ? sysPath.relative(this.options.cwd, dir) : dir;
  429. const index = key || ONE_DOT;
  430. watchList[index] = entry.getChildren().sort();
  431. });
  432. return watchList;
  433. }
  434. emitWithAll(event, args) {
  435. this.emit(...args);
  436. if (event !== handler_js_1.EVENTS.ERROR)
  437. this.emit(handler_js_1.EVENTS.ALL, ...args);
  438. }
  439. // Common helpers
  440. // --------------
  441. /**
  442. * Normalize and emit events.
  443. * Calling _emit DOES NOT MEAN emit() would be called!
  444. * @param event Type of event
  445. * @param path File or directory path
  446. * @param stats arguments to be passed with event
  447. * @returns the error if defined, otherwise the value of the FSWatcher instance's `closed` flag
  448. */
  449. async _emit(event, path, stats) {
  450. if (this.closed)
  451. return;
  452. const opts = this.options;
  453. if (handler_js_1.isWindows)
  454. path = sysPath.normalize(path);
  455. if (opts.cwd)
  456. path = sysPath.relative(opts.cwd, path);
  457. const args = [event, path];
  458. if (stats != null)
  459. args.push(stats);
  460. const awf = opts.awaitWriteFinish;
  461. let pw;
  462. if (awf && (pw = this._pendingWrites.get(path))) {
  463. pw.lastChange = new Date();
  464. return this;
  465. }
  466. if (opts.atomic) {
  467. if (event === handler_js_1.EVENTS.UNLINK) {
  468. this._pendingUnlinks.set(path, args);
  469. setTimeout(() => {
  470. this._pendingUnlinks.forEach((entry, path) => {
  471. this.emit(...entry);
  472. this.emit(handler_js_1.EVENTS.ALL, ...entry);
  473. this._pendingUnlinks.delete(path);
  474. });
  475. }, typeof opts.atomic === 'number' ? opts.atomic : 100);
  476. return this;
  477. }
  478. if (event === handler_js_1.EVENTS.ADD && this._pendingUnlinks.has(path)) {
  479. event = args[0] = handler_js_1.EVENTS.CHANGE;
  480. this._pendingUnlinks.delete(path);
  481. }
  482. }
  483. if (awf && (event === handler_js_1.EVENTS.ADD || event === handler_js_1.EVENTS.CHANGE) && this._readyEmitted) {
  484. const awfEmit = (err, stats) => {
  485. if (err) {
  486. event = args[0] = handler_js_1.EVENTS.ERROR;
  487. args[1] = err;
  488. this.emitWithAll(event, args);
  489. }
  490. else if (stats) {
  491. // if stats doesn't exist the file must have been deleted
  492. if (args.length > 2) {
  493. args[2] = stats;
  494. }
  495. else {
  496. args.push(stats);
  497. }
  498. this.emitWithAll(event, args);
  499. }
  500. };
  501. this._awaitWriteFinish(path, awf.stabilityThreshold, event, awfEmit);
  502. return this;
  503. }
  504. if (event === handler_js_1.EVENTS.CHANGE) {
  505. const isThrottled = !this._throttle(handler_js_1.EVENTS.CHANGE, path, 50);
  506. if (isThrottled)
  507. return this;
  508. }
  509. if (opts.alwaysStat &&
  510. stats === undefined &&
  511. (event === handler_js_1.EVENTS.ADD || event === handler_js_1.EVENTS.ADD_DIR || event === handler_js_1.EVENTS.CHANGE)) {
  512. const fullPath = opts.cwd ? sysPath.join(opts.cwd, path) : path;
  513. let stats;
  514. try {
  515. stats = await (0, promises_1.stat)(fullPath);
  516. }
  517. catch (err) {
  518. // do nothing
  519. }
  520. // Suppress event when fs_stat fails, to avoid sending undefined 'stat'
  521. if (!stats || this.closed)
  522. return;
  523. args.push(stats);
  524. }
  525. this.emitWithAll(event, args);
  526. return this;
  527. }
  528. /**
  529. * Common handler for errors
  530. * @returns The error if defined, otherwise the value of the FSWatcher instance's `closed` flag
  531. */
  532. _handleError(error) {
  533. const code = error && error.code;
  534. if (error &&
  535. code !== 'ENOENT' &&
  536. code !== 'ENOTDIR' &&
  537. (!this.options.ignorePermissionErrors || (code !== 'EPERM' && code !== 'EACCES'))) {
  538. this.emit(handler_js_1.EVENTS.ERROR, error);
  539. }
  540. return error || this.closed;
  541. }
  542. /**
  543. * Helper utility for throttling
  544. * @param actionType type being throttled
  545. * @param path being acted upon
  546. * @param timeout duration of time to suppress duplicate actions
  547. * @returns tracking object or false if action should be suppressed
  548. */
  549. _throttle(actionType, path, timeout) {
  550. if (!this._throttled.has(actionType)) {
  551. this._throttled.set(actionType, new Map());
  552. }
  553. const action = this._throttled.get(actionType);
  554. if (!action)
  555. throw new Error('invalid throttle');
  556. const actionPath = action.get(path);
  557. if (actionPath) {
  558. actionPath.count++;
  559. return false;
  560. }
  561. // eslint-disable-next-line prefer-const
  562. let timeoutObject;
  563. const clear = () => {
  564. const item = action.get(path);
  565. const count = item ? item.count : 0;
  566. action.delete(path);
  567. clearTimeout(timeoutObject);
  568. if (item)
  569. clearTimeout(item.timeoutObject);
  570. return count;
  571. };
  572. timeoutObject = setTimeout(clear, timeout);
  573. const thr = { timeoutObject, clear, count: 0 };
  574. action.set(path, thr);
  575. return thr;
  576. }
  577. _incrReadyCount() {
  578. return this._readyCount++;
  579. }
  580. /**
  581. * Awaits write operation to finish.
  582. * Polls a newly created file for size variations. When files size does not change for 'threshold' milliseconds calls callback.
  583. * @param path being acted upon
  584. * @param threshold Time in milliseconds a file size must be fixed before acknowledging write OP is finished
  585. * @param event
  586. * @param awfEmit Callback to be called when ready for event to be emitted.
  587. */
  588. _awaitWriteFinish(path, threshold, event, awfEmit) {
  589. const awf = this.options.awaitWriteFinish;
  590. if (typeof awf !== 'object')
  591. return;
  592. const pollInterval = awf.pollInterval;
  593. let timeoutHandler;
  594. let fullPath = path;
  595. if (this.options.cwd && !sysPath.isAbsolute(path)) {
  596. fullPath = sysPath.join(this.options.cwd, path);
  597. }
  598. const now = new Date();
  599. const writes = this._pendingWrites;
  600. function awaitWriteFinishFn(prevStat) {
  601. (0, fs_1.stat)(fullPath, (err, curStat) => {
  602. if (err || !writes.has(path)) {
  603. if (err && err.code !== 'ENOENT')
  604. awfEmit(err);
  605. return;
  606. }
  607. const now = Number(new Date());
  608. if (prevStat && curStat.size !== prevStat.size) {
  609. writes.get(path).lastChange = now;
  610. }
  611. const pw = writes.get(path);
  612. const df = now - pw.lastChange;
  613. if (df >= threshold) {
  614. writes.delete(path);
  615. awfEmit(undefined, curStat);
  616. }
  617. else {
  618. timeoutHandler = setTimeout(awaitWriteFinishFn, pollInterval, curStat);
  619. }
  620. });
  621. }
  622. if (!writes.has(path)) {
  623. writes.set(path, {
  624. lastChange: now,
  625. cancelWait: () => {
  626. writes.delete(path);
  627. clearTimeout(timeoutHandler);
  628. return event;
  629. },
  630. });
  631. timeoutHandler = setTimeout(awaitWriteFinishFn, pollInterval);
  632. }
  633. }
  634. /**
  635. * Determines whether user has asked to ignore this path.
  636. */
  637. _isIgnored(path, stats) {
  638. if (this.options.atomic && DOT_RE.test(path))
  639. return true;
  640. if (!this._userIgnored) {
  641. const { cwd } = this.options;
  642. const ign = this.options.ignored;
  643. const ignored = (ign || []).map(normalizeIgnored(cwd));
  644. const ignoredPaths = [...this._ignoredPaths];
  645. const list = [...ignoredPaths.map(normalizeIgnored(cwd)), ...ignored];
  646. this._userIgnored = anymatch(list, undefined);
  647. }
  648. return this._userIgnored(path, stats);
  649. }
  650. _isntIgnored(path, stat) {
  651. return !this._isIgnored(path, stat);
  652. }
  653. /**
  654. * Provides a set of common helpers and properties relating to symlink handling.
  655. * @param path file or directory pattern being watched
  656. */
  657. _getWatchHelpers(path) {
  658. return new WatchHelper(path, this.options.followSymlinks, this);
  659. }
  660. // Directory helpers
  661. // -----------------
  662. /**
  663. * Provides directory tracking objects
  664. * @param directory path of the directory
  665. */
  666. _getWatchedDir(directory) {
  667. const dir = sysPath.resolve(directory);
  668. if (!this._watched.has(dir))
  669. this._watched.set(dir, new DirEntry(dir, this._boundRemove));
  670. return this._watched.get(dir);
  671. }
  672. // File helpers
  673. // ------------
  674. /**
  675. * Check for read permissions: https://stackoverflow.com/a/11781404/1358405
  676. */
  677. _hasReadPermissions(stats) {
  678. if (this.options.ignorePermissionErrors)
  679. return true;
  680. return Boolean(Number(stats.mode) & 0o400);
  681. }
  682. /**
  683. * Handles emitting unlink events for
  684. * files and directories, and via recursion, for
  685. * files and directories within directories that are unlinked
  686. * @param directory within which the following item is located
  687. * @param item base path of item/directory
  688. */
  689. _remove(directory, item, isDirectory) {
  690. // if what is being deleted is a directory, get that directory's paths
  691. // for recursive deleting and cleaning of watched object
  692. // if it is not a directory, nestedDirectoryChildren will be empty array
  693. const path = sysPath.join(directory, item);
  694. const fullPath = sysPath.resolve(path);
  695. isDirectory =
  696. isDirectory != null ? isDirectory : this._watched.has(path) || this._watched.has(fullPath);
  697. // prevent duplicate handling in case of arriving here nearly simultaneously
  698. // via multiple paths (such as _handleFile and _handleDir)
  699. if (!this._throttle('remove', path, 100))
  700. return;
  701. // if the only watched file is removed, watch for its return
  702. if (!isDirectory && this._watched.size === 1) {
  703. this.add(directory, item, true);
  704. }
  705. // This will create a new entry in the watched object in either case
  706. // so we got to do the directory check beforehand
  707. const wp = this._getWatchedDir(path);
  708. const nestedDirectoryChildren = wp.getChildren();
  709. // Recursively remove children directories / files.
  710. nestedDirectoryChildren.forEach((nested) => this._remove(path, nested));
  711. // Check if item was on the watched list and remove it
  712. const parent = this._getWatchedDir(directory);
  713. const wasTracked = parent.has(item);
  714. parent.remove(item);
  715. // Fixes issue #1042 -> Relative paths were detected and added as symlinks
  716. // (https://github.com/paulmillr/chokidar/blob/e1753ddbc9571bdc33b4a4af172d52cb6e611c10/lib/nodefs-handler.js#L612),
  717. // but never removed from the map in case the path was deleted.
  718. // This leads to an incorrect state if the path was recreated:
  719. // https://github.com/paulmillr/chokidar/blob/e1753ddbc9571bdc33b4a4af172d52cb6e611c10/lib/nodefs-handler.js#L553
  720. if (this._symlinkPaths.has(fullPath)) {
  721. this._symlinkPaths.delete(fullPath);
  722. }
  723. // If we wait for this file to be fully written, cancel the wait.
  724. let relPath = path;
  725. if (this.options.cwd)
  726. relPath = sysPath.relative(this.options.cwd, path);
  727. if (this.options.awaitWriteFinish && this._pendingWrites.has(relPath)) {
  728. const event = this._pendingWrites.get(relPath).cancelWait();
  729. if (event === handler_js_1.EVENTS.ADD)
  730. return;
  731. }
  732. // The Entry will either be a directory that just got removed
  733. // or a bogus entry to a file, in either case we have to remove it
  734. this._watched.delete(path);
  735. this._watched.delete(fullPath);
  736. const eventName = isDirectory ? handler_js_1.EVENTS.UNLINK_DIR : handler_js_1.EVENTS.UNLINK;
  737. if (wasTracked && !this._isIgnored(path))
  738. this._emit(eventName, path);
  739. // Avoid conflicts if we later create another file with the same name
  740. this._closePath(path);
  741. }
  742. /**
  743. * Closes all watchers for a path
  744. */
  745. _closePath(path) {
  746. this._closeFile(path);
  747. const dir = sysPath.dirname(path);
  748. this._getWatchedDir(dir).remove(sysPath.basename(path));
  749. }
  750. /**
  751. * Closes only file-specific watchers
  752. */
  753. _closeFile(path) {
  754. const closers = this._closers.get(path);
  755. if (!closers)
  756. return;
  757. closers.forEach((closer) => closer());
  758. this._closers.delete(path);
  759. }
  760. _addPathCloser(path, closer) {
  761. if (!closer)
  762. return;
  763. let list = this._closers.get(path);
  764. if (!list) {
  765. list = [];
  766. this._closers.set(path, list);
  767. }
  768. list.push(closer);
  769. }
  770. _readdirp(root, opts) {
  771. if (this.closed)
  772. return;
  773. const options = { type: handler_js_1.EVENTS.ALL, alwaysStat: true, lstat: true, ...opts, depth: 0 };
  774. let stream = (0, readdirp_1.readdirp)(root, options);
  775. this._streams.add(stream);
  776. stream.once(handler_js_1.STR_CLOSE, () => {
  777. stream = undefined;
  778. });
  779. stream.once(handler_js_1.STR_END, () => {
  780. if (stream) {
  781. this._streams.delete(stream);
  782. stream = undefined;
  783. }
  784. });
  785. return stream;
  786. }
  787. }
  788. exports.FSWatcher = FSWatcher;
  789. /**
  790. * Instantiates watcher with paths to be tracked.
  791. * @param paths file / directory paths
  792. * @param options opts, such as `atomic`, `awaitWriteFinish`, `ignored`, and others
  793. * @returns an instance of FSWatcher for chaining.
  794. * @example
  795. * const watcher = watch('.').on('all', (event, path) => { console.log(event, path); });
  796. * watch('.', { atomic: true, awaitWriteFinish: true, ignored: (f, stats) => stats?.isFile() && !f.endsWith('.js') })
  797. */
  798. function watch(paths, options = {}) {
  799. const watcher = new FSWatcher(options);
  800. watcher.add(paths);
  801. return watcher;
  802. }
  803. exports.default = { watch, FSWatcher };
  804. //# sourceMappingURL=index.js.map