You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
2.7 KiB

4 years ago
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const Watchpack = require("watchpack");
  7. const objectToMap = require("../util/objectToMap");
  8. class NodeWatchFileSystem {
  9. constructor(inputFileSystem) {
  10. this.inputFileSystem = inputFileSystem;
  11. this.watcherOptions = {
  12. aggregateTimeout: 0
  13. };
  14. this.watcher = new Watchpack(this.watcherOptions);
  15. }
  16. watch(files, dirs, missing, startTime, options, callback, callbackUndelayed) {
  17. if (!Array.isArray(files)) {
  18. throw new Error("Invalid arguments: 'files'");
  19. }
  20. if (!Array.isArray(dirs)) {
  21. throw new Error("Invalid arguments: 'dirs'");
  22. }
  23. if (!Array.isArray(missing)) {
  24. throw new Error("Invalid arguments: 'missing'");
  25. }
  26. if (typeof callback !== "function") {
  27. throw new Error("Invalid arguments: 'callback'");
  28. }
  29. if (typeof startTime !== "number" && startTime) {
  30. throw new Error("Invalid arguments: 'startTime'");
  31. }
  32. if (typeof options !== "object") {
  33. throw new Error("Invalid arguments: 'options'");
  34. }
  35. if (typeof callbackUndelayed !== "function" && callbackUndelayed) {
  36. throw new Error("Invalid arguments: 'callbackUndelayed'");
  37. }
  38. const oldWatcher = this.watcher;
  39. this.watcher = new Watchpack(options);
  40. if (callbackUndelayed) {
  41. this.watcher.once("change", callbackUndelayed);
  42. }
  43. const cachedFiles = files;
  44. const cachedDirs = dirs;
  45. this.watcher.once("aggregated", (changes, removals) => {
  46. changes = changes.concat(removals);
  47. if (this.inputFileSystem && this.inputFileSystem.purge) {
  48. this.inputFileSystem.purge(changes);
  49. }
  50. const times = objectToMap(this.watcher.getTimes());
  51. files = new Set(files);
  52. dirs = new Set(dirs);
  53. missing = new Set(missing);
  54. removals = new Set(removals.filter(file => files.has(file)));
  55. callback(
  56. null,
  57. changes.filter(file => files.has(file)).sort(),
  58. changes.filter(file => dirs.has(file)).sort(),
  59. changes.filter(file => missing.has(file)).sort(),
  60. times,
  61. times,
  62. removals
  63. );
  64. });
  65. this.watcher.watch(
  66. cachedFiles.concat(missing),
  67. cachedDirs.concat(missing),
  68. startTime
  69. );
  70. if (oldWatcher) {
  71. oldWatcher.close();
  72. }
  73. return {
  74. close: () => {
  75. if (this.watcher) {
  76. this.watcher.close();
  77. this.watcher = null;
  78. }
  79. },
  80. pause: () => {
  81. if (this.watcher) {
  82. this.watcher.pause();
  83. }
  84. },
  85. getFileTimestamps: () => {
  86. if (this.watcher) {
  87. return objectToMap(this.watcher.getTimes());
  88. } else {
  89. return new Map();
  90. }
  91. },
  92. getContextTimestamps: () => {
  93. if (this.watcher) {
  94. return objectToMap(this.watcher.getTimes());
  95. } else {
  96. return new Map();
  97. }
  98. }
  99. };
  100. }
  101. }
  102. module.exports = NodeWatchFileSystem;