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