watching.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // @ts-check
  2. "use strict";
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. Object.defineProperty(exports, "createWatcher", {
  7. enumerable: true,
  8. get: ()=>createWatcher
  9. });
  10. const _chokidar = /*#__PURE__*/ _interopRequireDefault(require("chokidar"));
  11. const _fs = /*#__PURE__*/ _interopRequireDefault(require("fs"));
  12. const _micromatch = /*#__PURE__*/ _interopRequireDefault(require("micromatch"));
  13. const _normalizePath = /*#__PURE__*/ _interopRequireDefault(require("normalize-path"));
  14. const _path = /*#__PURE__*/ _interopRequireDefault(require("path"));
  15. const _utilsJs = require("./utils.js");
  16. function _interopRequireDefault(obj) {
  17. return obj && obj.__esModule ? obj : {
  18. default: obj
  19. };
  20. }
  21. function createWatcher(args, { state , rebuild }) {
  22. let shouldPoll = args["--poll"];
  23. let shouldCoalesceWriteEvents = shouldPoll || process.platform === "win32";
  24. // Polling interval in milliseconds
  25. // Used only when polling or coalescing add/change events on Windows
  26. let pollInterval = 10;
  27. let watcher = _chokidar.default.watch([], {
  28. // Force checking for atomic writes in all situations
  29. // This causes chokidar to wait up to 100ms for a file to re-added after it's been unlinked
  30. // This only works when watching directories though
  31. atomic: true,
  32. usePolling: shouldPoll,
  33. interval: shouldPoll ? pollInterval : undefined,
  34. ignoreInitial: true,
  35. awaitWriteFinish: shouldCoalesceWriteEvents ? {
  36. stabilityThreshold: 50,
  37. pollInterval: pollInterval
  38. } : false
  39. });
  40. // A queue of rebuilds, file reads, etc… to run
  41. let chain = Promise.resolve();
  42. /**
  43. * A list of files that have been changed since the last rebuild
  44. *
  45. * @type {{file: string, content: () => Promise<string>, extension: string}[]}
  46. */ let changedContent = [];
  47. /**
  48. * A list of files for which a rebuild has already been queued.
  49. * This is used to prevent duplicate rebuilds when multiple events are fired for the same file.
  50. * The rebuilt file is cleared from this list when it's associated rebuild has _started_
  51. * This is because if the file is changed during a rebuild it won't trigger a new rebuild which it should
  52. **/ let pendingRebuilds = new Set();
  53. let _timer;
  54. let _reject;
  55. /**
  56. * Rebuilds the changed files and resolves when the rebuild is
  57. * complete regardless of whether it was successful or not
  58. */ async function rebuildAndContinue() {
  59. let changes = changedContent.splice(0);
  60. // There are no changes to rebuild so we can just do nothing
  61. if (changes.length === 0) {
  62. return Promise.resolve();
  63. }
  64. // Clear all pending rebuilds for the about-to-be-built files
  65. changes.forEach((change)=>pendingRebuilds.delete(change.file));
  66. // Resolve the promise even when the rebuild fails
  67. return rebuild(changes).then(()=>{}, (e)=>{
  68. console.error(e.toString());
  69. });
  70. }
  71. /**
  72. *
  73. * @param {*} file
  74. * @param {(() => Promise<string>) | null} content
  75. * @param {boolean} skipPendingCheck
  76. * @returns {Promise<void>}
  77. */ function recordChangedFile(file, content = null, skipPendingCheck = false) {
  78. file = _path.default.resolve(file);
  79. // Applications like Vim/Neovim fire both rename and change events in succession for atomic writes
  80. // In that case rebuild has already been queued by rename, so can be skipped in change
  81. if (pendingRebuilds.has(file) && !skipPendingCheck) {
  82. return Promise.resolve();
  83. }
  84. // Mark that a rebuild of this file is going to happen
  85. // It MUST happen synchronously before the rebuild is queued for this to be effective
  86. pendingRebuilds.add(file);
  87. changedContent.push({
  88. file,
  89. content: content !== null && content !== void 0 ? content : ()=>_fs.default.promises.readFile(file, "utf8"),
  90. extension: _path.default.extname(file).slice(1)
  91. });
  92. if (_timer) {
  93. clearTimeout(_timer);
  94. _reject();
  95. }
  96. // If a rebuild is already in progress we don't want to start another one until the 10ms timer has expired
  97. chain = chain.then(()=>new Promise((resolve, reject)=>{
  98. _timer = setTimeout(resolve, 10);
  99. _reject = reject;
  100. }));
  101. // Resolves once this file has been rebuilt (or the rebuild for this file has failed)
  102. // This queues as many rebuilds as there are changed files
  103. // But those rebuilds happen after some delay
  104. // And will immediately resolve if there are no changes
  105. chain = chain.then(rebuildAndContinue, rebuildAndContinue);
  106. return chain;
  107. }
  108. watcher.on("change", (file)=>recordChangedFile(file));
  109. watcher.on("add", (file)=>recordChangedFile(file));
  110. // Restore watching any files that are "removed"
  111. // This can happen when a file is pseudo-atomically replaced (a copy is created, overwritten, the old one is unlinked, and the new one is renamed)
  112. // TODO: An an optimization we should allow removal when the config changes
  113. watcher.on("unlink", (file)=>{
  114. file = (0, _normalizePath.default)(file);
  115. // Only re-add the file if it's not covered by a dynamic pattern
  116. if (!_micromatch.default.some([
  117. file
  118. ], state.contentPatterns.dynamic)) {
  119. watcher.add(file);
  120. }
  121. });
  122. // Some applications such as Visual Studio (but not VS Code)
  123. // will only fire a rename event for atomic writes and not a change event
  124. // This is very likely a chokidar bug but it's one we need to work around
  125. // We treat this as a change event and rebuild the CSS
  126. watcher.on("raw", (evt, filePath, meta)=>{
  127. if (evt !== "rename") {
  128. return;
  129. }
  130. let watchedPath = meta.watchedPath;
  131. // Watched path might be the file itself
  132. // Or the directory it is in
  133. filePath = watchedPath.endsWith(filePath) ? watchedPath : _path.default.join(watchedPath, filePath);
  134. // Skip this event since the files it is for does not match any of the registered content globs
  135. if (!_micromatch.default.some([
  136. filePath
  137. ], state.contentPatterns.all)) {
  138. return;
  139. }
  140. // Skip since we've already queued a rebuild for this file that hasn't happened yet
  141. if (pendingRebuilds.has(filePath)) {
  142. return;
  143. }
  144. // We'll go ahead and add the file to the pending rebuilds list here
  145. // It'll be removed when the rebuild starts unless the read fails
  146. // which will be taken care of as well
  147. pendingRebuilds.add(filePath);
  148. async function enqueue() {
  149. try {
  150. // We need to read the file as early as possible outside of the chain
  151. // because it may be gone by the time we get to it. doing the read
  152. // immediately increases the chance that the file is still there
  153. let content = await (0, _utilsJs.readFileWithRetries)(_path.default.resolve(filePath));
  154. if (content === undefined) {
  155. return;
  156. }
  157. // This will push the rebuild onto the chain
  158. // We MUST skip the rebuild check here otherwise the rebuild will never happen on Linux
  159. // This is because the order of events and timing is different on Linux
  160. // @ts-ignore: TypeScript isn't picking up that content is a string here
  161. await recordChangedFile(filePath, ()=>content, true);
  162. } catch {
  163. // If reading the file fails, it's was probably a deleted temporary file
  164. // So we can ignore it and no rebuild is needed
  165. }
  166. }
  167. enqueue().then(()=>{
  168. // If the file read fails we still need to make sure the file isn't stuck in the pending rebuilds list
  169. pendingRebuilds.delete(filePath);
  170. });
  171. });
  172. return {
  173. fswatcher: watcher,
  174. refreshWatchedFiles () {
  175. watcher.add(Array.from(state.contextDependencies));
  176. watcher.add(Array.from(state.configBag.dependencies));
  177. watcher.add(state.contentPatterns.all);
  178. }
  179. };
  180. }