fsWatcher.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. "use strict";
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  4. var __getOwnPropNames = Object.getOwnPropertyNames;
  5. var __hasOwnProp = Object.prototype.hasOwnProperty;
  6. var __export = (target, all) => {
  7. for (var name in all)
  8. __defProp(target, name, { get: all[name], enumerable: true });
  9. };
  10. var __copyProps = (to, from, except, desc) => {
  11. if (from && typeof from === "object" || typeof from === "function") {
  12. for (let key of __getOwnPropNames(from))
  13. if (!__hasOwnProp.call(to, key) && key !== except)
  14. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  15. }
  16. return to;
  17. };
  18. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  19. var fsWatcher_exports = {};
  20. __export(fsWatcher_exports, {
  21. Watcher: () => Watcher
  22. });
  23. module.exports = __toCommonJS(fsWatcher_exports);
  24. var import_utilsBundle = require("./utilsBundle");
  25. class Watcher {
  26. constructor(onChange) {
  27. this._watchedPaths = [];
  28. this._ignoredFolders = [];
  29. this._collector = [];
  30. this._onChange = onChange;
  31. }
  32. async update(watchedPaths, ignoredFolders, reportPending) {
  33. if (JSON.stringify([this._watchedPaths, this._ignoredFolders]) === JSON.stringify([watchedPaths, ignoredFolders]))
  34. return;
  35. if (reportPending)
  36. this._reportEventsIfAny();
  37. this._watchedPaths = watchedPaths;
  38. this._ignoredFolders = ignoredFolders;
  39. void this._fsWatcher?.close();
  40. this._fsWatcher = void 0;
  41. this._collector.length = 0;
  42. clearTimeout(this._throttleTimer);
  43. this._throttleTimer = void 0;
  44. if (!this._watchedPaths.length)
  45. return;
  46. const ignored = [...this._ignoredFolders, "**/node_modules/**"];
  47. this._fsWatcher = import_utilsBundle.chokidar.watch(watchedPaths, { ignoreInitial: true, ignored }).on("all", async (event, file) => {
  48. if (this._throttleTimer)
  49. clearTimeout(this._throttleTimer);
  50. this._collector.push({ event, file });
  51. this._throttleTimer = setTimeout(() => this._reportEventsIfAny(), 250);
  52. });
  53. await new Promise((resolve, reject) => this._fsWatcher.once("ready", resolve).once("error", reject));
  54. }
  55. async close() {
  56. await this._fsWatcher?.close();
  57. }
  58. _reportEventsIfAny() {
  59. if (this._collector.length)
  60. this._onChange(this._collector.slice());
  61. this._collector.length = 0;
  62. }
  63. }
  64. // Annotate the CommonJS export names for ESM import in node:
  65. 0 && (module.exports = {
  66. Watcher
  67. });