1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | Author Tobias Koppers @sokra
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | const Watchpack = require("watchpack");
|
---|
9 |
|
---|
10 | /** @typedef {import("../../declarations/WebpackOptions").WatchOptions} WatchOptions */
|
---|
11 | /** @typedef {import("../FileSystemInfo").FileSystemInfoEntry} FileSystemInfoEntry */
|
---|
12 | /** @typedef {import("../util/fs").WatchFileSystem} WatchFileSystem */
|
---|
13 | /** @typedef {import("../util/fs").WatchMethod} WatchMethod */
|
---|
14 | /** @typedef {import("../util/fs").Watcher} Watcher */
|
---|
15 |
|
---|
16 | class NodeWatchFileSystem {
|
---|
17 | constructor(inputFileSystem) {
|
---|
18 | this.inputFileSystem = inputFileSystem;
|
---|
19 | this.watcherOptions = {
|
---|
20 | aggregateTimeout: 0
|
---|
21 | };
|
---|
22 | this.watcher = new Watchpack(this.watcherOptions);
|
---|
23 | }
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * @param {Iterable<string>} files watched files
|
---|
27 | * @param {Iterable<string>} directories watched directories
|
---|
28 | * @param {Iterable<string>} missing watched exitance entries
|
---|
29 | * @param {number} startTime timestamp of start time
|
---|
30 | * @param {WatchOptions} options options object
|
---|
31 | * @param {function(Error=, Map<string, FileSystemInfoEntry>, Map<string, FileSystemInfoEntry>, Set<string>, Set<string>): void} callback aggregated callback
|
---|
32 | * @param {function(string, number): void} callbackUndelayed callback when the first change was detected
|
---|
33 | * @returns {Watcher} a watcher
|
---|
34 | */
|
---|
35 | watch(
|
---|
36 | files,
|
---|
37 | directories,
|
---|
38 | missing,
|
---|
39 | startTime,
|
---|
40 | options,
|
---|
41 | callback,
|
---|
42 | callbackUndelayed
|
---|
43 | ) {
|
---|
44 | if (!files || typeof files[Symbol.iterator] !== "function") {
|
---|
45 | throw new Error("Invalid arguments: 'files'");
|
---|
46 | }
|
---|
47 | if (!directories || typeof directories[Symbol.iterator] !== "function") {
|
---|
48 | throw new Error("Invalid arguments: 'directories'");
|
---|
49 | }
|
---|
50 | if (!missing || typeof missing[Symbol.iterator] !== "function") {
|
---|
51 | throw new Error("Invalid arguments: 'missing'");
|
---|
52 | }
|
---|
53 | if (typeof callback !== "function") {
|
---|
54 | throw new Error("Invalid arguments: 'callback'");
|
---|
55 | }
|
---|
56 | if (typeof startTime !== "number" && startTime) {
|
---|
57 | throw new Error("Invalid arguments: 'startTime'");
|
---|
58 | }
|
---|
59 | if (typeof options !== "object") {
|
---|
60 | throw new Error("Invalid arguments: 'options'");
|
---|
61 | }
|
---|
62 | if (typeof callbackUndelayed !== "function" && callbackUndelayed) {
|
---|
63 | throw new Error("Invalid arguments: 'callbackUndelayed'");
|
---|
64 | }
|
---|
65 | const oldWatcher = this.watcher;
|
---|
66 | this.watcher = new Watchpack(options);
|
---|
67 |
|
---|
68 | if (callbackUndelayed) {
|
---|
69 | this.watcher.once("change", callbackUndelayed);
|
---|
70 | }
|
---|
71 | this.watcher.once("aggregated", (changes, removals) => {
|
---|
72 | if (this.inputFileSystem && this.inputFileSystem.purge) {
|
---|
73 | const fs = this.inputFileSystem;
|
---|
74 | for (const item of changes) {
|
---|
75 | fs.purge(item);
|
---|
76 | }
|
---|
77 | for (const item of removals) {
|
---|
78 | fs.purge(item);
|
---|
79 | }
|
---|
80 | }
|
---|
81 | const times = this.watcher.getTimeInfoEntries();
|
---|
82 | callback(null, times, times, changes, removals);
|
---|
83 | });
|
---|
84 |
|
---|
85 | this.watcher.watch({ files, directories, missing, startTime });
|
---|
86 |
|
---|
87 | if (oldWatcher) {
|
---|
88 | oldWatcher.close();
|
---|
89 | }
|
---|
90 | return {
|
---|
91 | close: () => {
|
---|
92 | if (this.watcher) {
|
---|
93 | this.watcher.close();
|
---|
94 | this.watcher = null;
|
---|
95 | }
|
---|
96 | },
|
---|
97 | pause: () => {
|
---|
98 | if (this.watcher) {
|
---|
99 | this.watcher.pause();
|
---|
100 | }
|
---|
101 | },
|
---|
102 | getAggregatedRemovals: () => {
|
---|
103 | const items = this.watcher && this.watcher.aggregatedRemovals;
|
---|
104 | if (items && this.inputFileSystem && this.inputFileSystem.purge) {
|
---|
105 | const fs = this.inputFileSystem;
|
---|
106 | for (const item of items) {
|
---|
107 | fs.purge(item);
|
---|
108 | }
|
---|
109 | }
|
---|
110 | return items;
|
---|
111 | },
|
---|
112 | getAggregatedChanges: () => {
|
---|
113 | const items = this.watcher && this.watcher.aggregatedChanges;
|
---|
114 | if (items && this.inputFileSystem && this.inputFileSystem.purge) {
|
---|
115 | const fs = this.inputFileSystem;
|
---|
116 | for (const item of items) {
|
---|
117 | fs.purge(item);
|
---|
118 | }
|
---|
119 | }
|
---|
120 | return items;
|
---|
121 | },
|
---|
122 | getFileTimeInfoEntries: () => {
|
---|
123 | if (this.watcher) {
|
---|
124 | return this.watcher.getTimeInfoEntries();
|
---|
125 | } else {
|
---|
126 | return new Map();
|
---|
127 | }
|
---|
128 | },
|
---|
129 | getContextTimeInfoEntries: () => {
|
---|
130 | if (this.watcher) {
|
---|
131 | return this.watcher.getTimeInfoEntries();
|
---|
132 | } else {
|
---|
133 | return new Map();
|
---|
134 | }
|
---|
135 | }
|
---|
136 | };
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | module.exports = NodeWatchFileSystem;
|
---|