/
FSEventsWatcher.ts
187 lines (164 loc) · 4.71 KB
/
FSEventsWatcher.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
import * as fs from 'fs';
import * as path from 'path';
import {EventEmitter} from 'events';
import anymatch, {Matcher} from 'anymatch';
import micromatch from 'micromatch';
// eslint-disable-next-line
import {Watcher} from 'fsevents';
// @ts-ignore no types
import walker from 'walker';
let fsevents: (path: string) => Watcher;
try {
fsevents = require('fsevents');
} catch (e) {
// Optional dependency, only supported on Darwin.
}
const CHANGE_EVENT = 'change';
const DELETE_EVENT = 'delete';
const ADD_EVENT = 'add';
const ALL_EVENT = 'all';
type FsEventsWatcherEvent =
| typeof CHANGE_EVENT
| typeof DELETE_EVENT
| typeof ADD_EVENT
| typeof ALL_EVENT;
/**
* Export `FSEventsWatcher` class.
* Watches `dir`.
*/
class FSEventsWatcher extends EventEmitter {
public readonly root: string;
public readonly ignored?: Matcher;
public readonly glob: Array<string>;
public readonly dot: boolean;
public readonly hasIgnore: boolean;
public readonly doIgnore: (path: string) => boolean;
public readonly watcher: Watcher;
private _tracked: Set<string>;
static isSupported() {
return fsevents !== undefined;
}
private static normalizeProxy(
callback: (normalizedPath: string, stats: fs.Stats) => void,
) {
return (filepath: string, stats: fs.Stats) =>
callback(path.normalize(filepath), stats);
}
private static recReaddir(
dir: string,
dirCallback: (normalizedPath: string, stats: fs.Stats) => void,
fileCallback: (normalizedPath: string, stats: fs.Stats) => void,
endCallback: Function,
errorCallback: Function,
ignored?: Matcher,
) {
walker(dir)
.filterDir(
(currentDir: string) => !ignored || !anymatch(ignored, currentDir),
)
.on('dir', FSEventsWatcher.normalizeProxy(dirCallback))
.on('file', FSEventsWatcher.normalizeProxy(fileCallback))
.on('error', errorCallback)
.on('end', () => {
endCallback();
});
}
constructor(
dir: string,
opts: {
root: string;
ignored?: Matcher;
glob: string | Array<string>;
dot: boolean;
},
) {
if (!fsevents) {
throw new Error(
'`fsevents` unavailable (this watcher can only be used on Darwin)',
);
}
super();
this.dot = opts.dot || false;
this.ignored = opts.ignored;
this.glob = Array.isArray(opts.glob) ? opts.glob : [opts.glob];
this.hasIgnore =
Boolean(opts.ignored) && !(Array.isArray(opts) && opts.length > 0);
this.doIgnore = opts.ignored ? anymatch(opts.ignored) : () => false;
this.root = path.resolve(dir);
this.watcher = fsevents(this.root);
this.watcher.start().on('change', this.handleEvent.bind(this));
this._tracked = new Set();
FSEventsWatcher.recReaddir(
this.root,
(filepath: string) => {
this._tracked.add(filepath);
},
(filepath: string) => {
this._tracked.add(filepath);
},
this.emit.bind(this, 'ready'),
this.emit.bind(this, 'error'),
this.ignored,
);
}
/**
* End watching.
*/
close(callback?: () => void) {
this.watcher.stop();
this.removeAllListeners();
if (typeof callback === 'function') {
process.nextTick(callback.bind(null, null, true));
}
}
private isFileIncluded(relativePath: string) {
if (this.doIgnore(relativePath)) {
return false;
}
return this.glob.length
? !!micromatch([relativePath], this.glob, {dot: this.dot}).length
: !!this.dot || micromatch([relativePath], '**/*').length;
}
private handleEvent(filepath: string) {
const relativePath = path.relative(this.root, filepath);
if (!this.isFileIncluded(relativePath)) {
return;
}
fs.lstat(filepath, (error, stat) => {
if (error && error.code !== 'ENOENT') {
this.emit('error', error);
return;
}
if (error) {
// Ignore files that aren't tracked and don't exist.
if (!this._tracked.has(filepath)) {
return;
}
this._emit(DELETE_EVENT, relativePath);
this._tracked.delete(filepath);
return;
}
if (this._tracked.has(filepath)) {
this._emit(CHANGE_EVENT, relativePath, stat);
} else {
this._tracked.add(filepath);
this._emit(ADD_EVENT, relativePath, stat);
}
});
}
/**
* Emit events.
*/
private _emit(type: FsEventsWatcherEvent, file: string, stat?: fs.Stats) {
this.emit(type, file, this.root, stat);
this.emit(ALL_EVENT, type, file, this.root, stat);
}
}
export = FSEventsWatcher;