NodeWatchFileSystem.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. this.watcher.once("aggregated", (changes, removals) => {
  44. changes = changes.concat(removals);
  45. if (this.inputFileSystem && this.inputFileSystem.purge) {
  46. this.inputFileSystem.purge(changes);
  47. }
  48. const times = objectToMap(this.watcher.getTimes());
  49. callback(
  50. null,
  51. changes.filter(file => files.includes(file)).sort(),
  52. changes.filter(file => dirs.includes(file)).sort(),
  53. changes.filter(file => missing.includes(file)).sort(),
  54. times,
  55. times
  56. );
  57. });
  58. this.watcher.watch(files.concat(missing), dirs.concat(missing), startTime);
  59. if (oldWatcher) {
  60. oldWatcher.close();
  61. }
  62. return {
  63. close: () => {
  64. if (this.watcher) {
  65. this.watcher.close();
  66. this.watcher = null;
  67. }
  68. },
  69. pause: () => {
  70. if (this.watcher) {
  71. this.watcher.pause();
  72. }
  73. },
  74. getFileTimestamps: () => {
  75. if (this.watcher) {
  76. return objectToMap(this.watcher.getTimes());
  77. } else {
  78. return new Map();
  79. }
  80. },
  81. getContextTimestamps: () => {
  82. if (this.watcher) {
  83. return objectToMap(this.watcher.getTimes());
  84. } else {
  85. return new Map();
  86. }
  87. }
  88. };
  89. }
  90. }
  91. module.exports = NodeWatchFileSystem;