@idlebox/chokidar 0.0.26 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/__create_index.generated.cjs.map +1 -1
- package/lib/cjs/main.cjs +101 -40
- package/lib/cjs/main.cjs.map +1 -1
- package/lib/esm/__create_index.generated.d.ts +1 -0
- package/lib/esm/__create_index.generated.d.ts.map +1 -1
- package/lib/esm/__create_index.generated.js.map +1 -1
- package/lib/esm/main.d.ts +40 -13
- package/lib/esm/main.d.ts.map +1 -1
- package/lib/esm/main.js +103 -42
- package/lib/esm/main.js.map +1 -1
- package/package.json +2 -2
- package/src/main.ts +138 -50
|
@@ -6,5 +6,5 @@
|
|
|
6
6
|
"../../src/__create_index.generated.ts"
|
|
7
7
|
],
|
|
8
8
|
"names": [],
|
|
9
|
-
"mappings": ";AAAA,wBAAwB;AACxB,aAAa;AACb,oBAAoB;;;AAEpB,aAAa;AACZ,cAAc;AACd,sCAAsC;AAA9B,sGAAA,WAAW,OAAA;
|
|
9
|
+
"mappings": ";AAAA,wBAAwB;AACxB,aAAa;AACb,oBAAoB;;;AAEpB,aAAa;AACZ,cAAc;AACd,sCAAsC;AAA9B,sGAAA,WAAW,OAAA;AAGnB,sCAAwC;AAAhC,wGAAA,aAAa,OAAA;AACtB,kBAAkB;AACjB,cAAc;AACd,gDAAyC;AAAjC,yGAAA,SAAS,OAAA"
|
|
10
10
|
}
|
package/lib/cjs/main.cjs
CHANGED
|
@@ -3,9 +3,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.WatchHelper = void 0;
|
|
4
4
|
exports.startChokidar = startChokidar;
|
|
5
5
|
const tslib_1 = require("tslib");
|
|
6
|
-
const common_1 = require("@idlebox/common");
|
|
7
6
|
const chokidar_1 = require("chokidar");
|
|
8
7
|
const debug_1 = tslib_1.__importDefault(require("debug"));
|
|
8
|
+
const node_path_1 = require("node:path");
|
|
9
|
+
const node_util_1 = require("node:util");
|
|
9
10
|
const log = (0, debug_1.default)('chokidar');
|
|
10
11
|
var State;
|
|
11
12
|
(function (State) {
|
|
@@ -21,18 +22,32 @@ var State;
|
|
|
21
22
|
class WatchHelper {
|
|
22
23
|
watcher;
|
|
23
24
|
onChange;
|
|
24
|
-
_watches = new Set();
|
|
25
25
|
state = State.IDLE;
|
|
26
26
|
debounceMs = 800;
|
|
27
27
|
lastRun;
|
|
28
28
|
changes = new Set();
|
|
29
|
+
cwd;
|
|
30
|
+
trackedFiles = new Set();
|
|
29
31
|
constructor(watcher, onChange) {
|
|
30
32
|
this.watcher = watcher;
|
|
31
33
|
this.onChange = onChange;
|
|
32
|
-
this.
|
|
33
|
-
|
|
34
|
-
watcher.
|
|
35
|
-
|
|
34
|
+
this.lowlevel_handler = this.lowlevel_handler.bind(this);
|
|
35
|
+
this.handler = this.handler.bind(this);
|
|
36
|
+
this.cwd = watcher.options.cwd;
|
|
37
|
+
}
|
|
38
|
+
listen(item) {
|
|
39
|
+
if (!allowedEvents.includes(item)) {
|
|
40
|
+
throw new Error(`not allowed watcher event name: ${item}`);
|
|
41
|
+
}
|
|
42
|
+
if (this.watcher.listenerCount(item) === 0) {
|
|
43
|
+
this.watcher.addListener(item, this.lowlevel_handler);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
unlisten(item) {
|
|
47
|
+
if (!allowedEvents.includes(item)) {
|
|
48
|
+
throw new Error(`not allowed watcher event name: ${item}`);
|
|
49
|
+
}
|
|
50
|
+
this.watcher.removeAllListeners(item);
|
|
36
51
|
}
|
|
37
52
|
_debounce;
|
|
38
53
|
debounce() {
|
|
@@ -40,13 +55,13 @@ class WatchHelper {
|
|
|
40
55
|
clearTimeout(this._debounce);
|
|
41
56
|
this._debounce = undefined;
|
|
42
57
|
}
|
|
43
|
-
this._debounce = setTimeout(this.
|
|
58
|
+
this._debounce = setTimeout(this.handler, this.debounceMs);
|
|
44
59
|
}
|
|
45
60
|
changeState(s) {
|
|
46
61
|
log(`state change: ${State[s]}`);
|
|
47
62
|
this.state = s;
|
|
48
63
|
}
|
|
49
|
-
|
|
64
|
+
lowlevel_handler(path) {
|
|
50
65
|
log('file changes: %s', path);
|
|
51
66
|
this.changes.add(path);
|
|
52
67
|
switch (this.state) {
|
|
@@ -66,7 +81,7 @@ class WatchHelper {
|
|
|
66
81
|
throw new Error(`Invalid program state. ${this.state}`);
|
|
67
82
|
}
|
|
68
83
|
}
|
|
69
|
-
|
|
84
|
+
handler() {
|
|
70
85
|
log('trigger...');
|
|
71
86
|
const changes = [...this.changes.values()];
|
|
72
87
|
this.changes.clear();
|
|
@@ -76,12 +91,12 @@ class WatchHelper {
|
|
|
76
91
|
.then(() => {
|
|
77
92
|
this.onChange(changes);
|
|
78
93
|
})
|
|
79
|
-
.catch(
|
|
94
|
+
.catch(e => {
|
|
80
95
|
log('Failed callback: %s', e.stack);
|
|
81
96
|
})
|
|
82
97
|
.finally(() => {
|
|
83
98
|
if (this.state === State.RESCHEDULE) {
|
|
84
|
-
this.
|
|
99
|
+
this.handler();
|
|
85
100
|
}
|
|
86
101
|
else if (this.state === State.BUSY) {
|
|
87
102
|
this.changeState(State.IDLE);
|
|
@@ -95,52 +110,98 @@ class WatchHelper {
|
|
|
95
110
|
this.lastRun = undefined;
|
|
96
111
|
});
|
|
97
112
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
return this._watches.size;
|
|
113
|
+
get empty() {
|
|
114
|
+
return Object.keys(this.watcher.getWatched()).length === 0;
|
|
101
115
|
}
|
|
102
|
-
|
|
116
|
+
get size() {
|
|
117
|
+
let sum = 0;
|
|
118
|
+
for (const item of Object.values(this.watcher.getWatched())) {
|
|
119
|
+
sum += item.length;
|
|
120
|
+
}
|
|
121
|
+
return sum;
|
|
122
|
+
}
|
|
123
|
+
replace(newList) {
|
|
103
124
|
const newSet = new Set(newList);
|
|
104
|
-
for (const item of
|
|
105
|
-
|
|
125
|
+
for (const item of this.trackedFiles) {
|
|
126
|
+
if (!newSet.has(item)) {
|
|
127
|
+
this.delete(item);
|
|
128
|
+
}
|
|
106
129
|
}
|
|
107
|
-
for (const item of this.
|
|
130
|
+
for (const item of this.watches) {
|
|
108
131
|
if (!newSet.has(item)) {
|
|
109
|
-
this.
|
|
132
|
+
this.delete(item);
|
|
110
133
|
}
|
|
111
134
|
}
|
|
135
|
+
this.add([...newSet]);
|
|
112
136
|
}
|
|
113
|
-
addWatch(
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
137
|
+
addWatch = (0, node_util_1.deprecate)(this.add.bind(this), 'use add instead of addWatch');
|
|
138
|
+
delWatch = (0, node_util_1.deprecate)(this.delete.bind(this), 'use delete instead of delWatch');
|
|
139
|
+
add(files) {
|
|
140
|
+
// console.log('[watch:add] %s', files)
|
|
141
|
+
if (typeof files === 'string') {
|
|
142
|
+
this.trackedFiles.add(files);
|
|
118
143
|
}
|
|
144
|
+
else {
|
|
145
|
+
for (const file of files) {
|
|
146
|
+
this.trackedFiles.add(file);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
this.watcher.add(files);
|
|
119
150
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
this.
|
|
124
|
-
this._watches.delete(oldWatch);
|
|
151
|
+
delete(files) {
|
|
152
|
+
// console.log('[watch:del] %s', files)
|
|
153
|
+
if (typeof files === 'string') {
|
|
154
|
+
this.trackedFiles.delete(files);
|
|
125
155
|
}
|
|
156
|
+
else {
|
|
157
|
+
for (const file of files) {
|
|
158
|
+
this.trackedFiles.delete(file);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
this.watcher.unwatch(files);
|
|
126
162
|
}
|
|
127
163
|
reset() {
|
|
128
|
-
this.watcher.unwatch([...this.
|
|
129
|
-
this.
|
|
164
|
+
this.watcher.unwatch([...this.trackedFiles]);
|
|
165
|
+
this.trackedFiles.clear();
|
|
166
|
+
this.watcher.unwatch(this.watches);
|
|
167
|
+
}
|
|
168
|
+
get expectedWatches() {
|
|
169
|
+
return [...this.trackedFiles];
|
|
170
|
+
}
|
|
171
|
+
get trackingSize() {
|
|
172
|
+
return this.trackedFiles.size;
|
|
130
173
|
}
|
|
131
174
|
get watches() {
|
|
132
|
-
|
|
175
|
+
const list = [];
|
|
176
|
+
for (const [folder, files] of Object.entries(this.watcher.getWatched())) {
|
|
177
|
+
for (const file of files) {
|
|
178
|
+
if (this.cwd) {
|
|
179
|
+
list.push((0, node_path_1.join)(this.cwd, folder, file));
|
|
180
|
+
}
|
|
181
|
+
else {
|
|
182
|
+
list.push((0, node_path_1.join)(folder, file));
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return list;
|
|
133
187
|
}
|
|
134
|
-
dispose() {
|
|
135
|
-
|
|
188
|
+
async dispose() {
|
|
189
|
+
await Promise.all([this.watcher.close(), this.lastRun]);
|
|
136
190
|
}
|
|
137
191
|
}
|
|
138
192
|
exports.WatchHelper = WatchHelper;
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
193
|
+
const allowedEvents = ['add', 'addDir', 'change', 'unlink', 'unlinkDir', 'ready'];
|
|
194
|
+
const defaultOptions = {
|
|
195
|
+
ignoreInitial: true,
|
|
196
|
+
debounceMs: 500,
|
|
197
|
+
watchingEvents: ['add', 'change'],
|
|
198
|
+
};
|
|
199
|
+
function startChokidar(reload, options = {}) {
|
|
200
|
+
const opts = Object.assign({}, defaultOptions, options);
|
|
201
|
+
const watcher = new WatchHelper(new chokidar_1.FSWatcher(opts), reload);
|
|
202
|
+
for (const item of opts.watchingEvents) {
|
|
203
|
+
watcher.listen(item);
|
|
204
|
+
}
|
|
205
|
+
return watcher;
|
|
145
206
|
}
|
|
146
207
|
//# sourceMappingURL=main.cjs.map
|
package/lib/cjs/main.cjs.map
CHANGED
|
@@ -6,5 +6,5 @@
|
|
|
6
6
|
"../../src/main.ts"
|
|
7
7
|
],
|
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AI,IAAI,CAAC,cAAc,EAAE,CAAC;QACxC,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IACtB,CAAC;IACD,OAAO,OAAO,CAAC;AAChB,CAAC"
|
|
10
10
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { WatchHelper } from "./main.js";
|
|
2
2
|
export { type IWatchHelper } from "./main.js";
|
|
3
|
+
export { type IExtraOptions } from "./main.js";
|
|
3
4
|
export { startChokidar } from "./main.js";
|
|
4
5
|
export { FSWatcher } from "./re-export.js";
|
|
5
6
|
//# sourceMappingURL=__create_index.generated.d.ts.map
|
|
@@ -6,5 +6,5 @@
|
|
|
6
6
|
"../../src/__create_index.generated.ts"
|
|
7
7
|
],
|
|
8
8
|
"names": [],
|
|
9
|
-
"mappings": "AAMC,OAAO,EAAC,WAAW,EAAC,MAAM,WAAW,CAAC;AACtC,OAAO,EAAC,KAAK,YAAY,EAAC,MAAM,WAAW,CAAC;AAC5C,OAAO,EAAC,aAAa,EAAC,MAAM,WAAW,CAAC;AAGxC,OAAO,EAAC,SAAS,EAAC,MAAM,gBAAgB,CAAC"
|
|
9
|
+
"mappings": "AAMC,OAAO,EAAC,WAAW,EAAC,MAAM,WAAW,CAAC;AACtC,OAAO,EAAC,KAAK,YAAY,EAAC,MAAM,WAAW,CAAC;AAC5C,OAAO,EAAC,KAAK,aAAa,EAAC,MAAM,WAAW,CAAC;AAC7C,OAAO,EAAC,aAAa,EAAC,MAAM,WAAW,CAAC;AAGxC,OAAO,EAAC,SAAS,EAAC,MAAM,gBAAgB,CAAC"
|
|
10
10
|
}
|
|
@@ -6,5 +6,5 @@
|
|
|
6
6
|
"../../src/__create_index.generated.ts"
|
|
7
7
|
],
|
|
8
8
|
"names": [],
|
|
9
|
-
"mappings": "AAAA,wBAAwB;AACxB,aAAa;AACb,oBAAoB;AAEpB,aAAa;AACZ,cAAc;AACd,OAAO,EAAC,WAAW,EAAC,MAAM,WAAW,CAAC;
|
|
9
|
+
"mappings": "AAAA,wBAAwB;AACxB,aAAa;AACb,oBAAoB;AAEpB,aAAa;AACZ,cAAc;AACd,OAAO,EAAC,WAAW,EAAC,MAAM,WAAW,CAAC;AAGtC,OAAO,EAAC,aAAa,EAAC,MAAM,WAAW,CAAC;AACzC,kBAAkB;AACjB,cAAc;AACd,OAAO,EAAC,SAAS,EAAC,MAAM,gBAAgB,CAAC"
|
|
10
10
|
}
|
package/lib/esm/main.d.ts
CHANGED
|
@@ -1,34 +1,61 @@
|
|
|
1
|
-
import { FSWatcher } from 'chokidar';
|
|
1
|
+
import { FSWatcher, type ChokidarOptions } from 'chokidar';
|
|
2
2
|
export declare class WatchHelper implements IWatchHelper {
|
|
3
3
|
private readonly watcher;
|
|
4
4
|
private readonly onChange;
|
|
5
|
-
private _watches;
|
|
6
5
|
private state;
|
|
7
6
|
debounceMs: number;
|
|
8
7
|
private lastRun?;
|
|
9
8
|
private changes;
|
|
9
|
+
private readonly cwd;
|
|
10
|
+
private trackedFiles;
|
|
10
11
|
constructor(watcher: FSWatcher, onChange: IReloadFunction);
|
|
12
|
+
listen(item: (typeof allowedEvents)[number]): void;
|
|
13
|
+
unlisten(item: (typeof allowedEvents)[number]): void;
|
|
11
14
|
private _debounce?;
|
|
12
15
|
private debounce;
|
|
13
16
|
private changeState;
|
|
14
|
-
private
|
|
15
|
-
private
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
private lowlevel_handler;
|
|
18
|
+
private handler;
|
|
19
|
+
get empty(): boolean;
|
|
20
|
+
get size(): number;
|
|
21
|
+
replace(newList: readonly string[]): void;
|
|
22
|
+
addWatch: (files: string | readonly string[]) => void;
|
|
23
|
+
delWatch: (files: string | readonly string[]) => void;
|
|
24
|
+
add(files: string | readonly string[]): void;
|
|
25
|
+
delete(files: string | readonly string[]): void;
|
|
20
26
|
reset(): void;
|
|
21
|
-
get
|
|
27
|
+
get expectedWatches(): string[];
|
|
28
|
+
get trackingSize(): number;
|
|
29
|
+
get watches(): string[];
|
|
22
30
|
dispose(): Promise<void>;
|
|
23
31
|
}
|
|
24
32
|
export interface IWatchHelper {
|
|
25
|
-
|
|
26
|
-
|
|
33
|
+
/** @deprecated use add */
|
|
34
|
+
addWatch(files: string | readonly string[]): void;
|
|
35
|
+
/** @deprecated use delete */
|
|
36
|
+
delWatch(files: string | readonly string[]): void;
|
|
37
|
+
add(files: string | readonly string[]): void;
|
|
38
|
+
delete(files: string | readonly string[]): void;
|
|
39
|
+
replace(files: readonly string[]): void;
|
|
27
40
|
reset(): void;
|
|
41
|
+
/** 注意: chokidar的api无法同步获取监听文件数量,这个数值不会实时的反映实际情况 */
|
|
42
|
+
readonly size: number;
|
|
43
|
+
/** 注意: chokidar的api无法同步获取监听文件数量,这个数值不会实时的反映实际情况 */
|
|
44
|
+
readonly empty: boolean;
|
|
45
|
+
/** 本地维护的文件列表,和实际不一定相符,比如 符号链接、文件删除自动取消监视 等情况均无法同步 */
|
|
46
|
+
readonly trackingSize: number;
|
|
47
|
+
readonly expectedWatches: string[];
|
|
48
|
+
listen(item: (typeof allowedEvents)[number]): void;
|
|
49
|
+
unlisten(item: (typeof allowedEvents)[number]): void;
|
|
28
50
|
dispose(): Promise<void>;
|
|
29
|
-
readonly watches:
|
|
51
|
+
readonly watches: string[];
|
|
30
52
|
}
|
|
31
53
|
type IReloadFunction = (changes: string[]) => void | Promise<void>;
|
|
32
|
-
|
|
54
|
+
declare const allowedEvents: readonly ["add", "addDir", "change", "unlink", "unlinkDir", "ready"];
|
|
55
|
+
export interface IExtraOptions extends ChokidarOptions {
|
|
56
|
+
debounceMs: number;
|
|
57
|
+
watchingEvents: (typeof allowedEvents)[number][];
|
|
58
|
+
}
|
|
59
|
+
export declare function startChokidar(reload: IReloadFunction, options?: Partial<IExtraOptions>): IWatchHelper;
|
|
33
60
|
export {};
|
|
34
61
|
//# sourceMappingURL=main.d.ts.map
|
package/lib/esm/main.d.ts.map
CHANGED
|
@@ -6,5 +6,5 @@
|
|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c,EAAE,CAAC,OAAO,aAAa,CAAC,CAAC,MAAM,CAAC,EAAE,CAAC;CACjD;AAQD,wBAAgB,aAAa,CAAC,MAAM,EAAE,eAAe,EAAE,OAAO,GAAE,OAAO,CAAC,aAAa,CAAM,GAAG,YAAY,CAOzG"
|
|
10
10
|
}
|
package/lib/esm/main.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
const { FSWatcher } = _chokidar_59;
|
|
1
|
+
import _chokidar_0 from 'chokidar';
|
|
2
|
+
const { FSWatcher, ChokidarOptions } = _chokidar_0;
|
|
4
3
|
import debug from 'debug';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import { deprecate } from 'node:util';
|
|
5
6
|
const log = debug('chokidar');
|
|
6
7
|
var State;
|
|
7
8
|
(function (State) {
|
|
@@ -17,18 +18,32 @@ var State;
|
|
|
17
18
|
export class WatchHelper {
|
|
18
19
|
watcher;
|
|
19
20
|
onChange;
|
|
20
|
-
_watches = new Set();
|
|
21
21
|
state = State.IDLE;
|
|
22
22
|
debounceMs = 800;
|
|
23
23
|
lastRun;
|
|
24
24
|
changes = new Set();
|
|
25
|
+
cwd;
|
|
26
|
+
trackedFiles = new Set();
|
|
25
27
|
constructor(watcher, onChange) {
|
|
26
28
|
this.watcher = watcher;
|
|
27
29
|
this.onChange = onChange;
|
|
28
|
-
this.
|
|
29
|
-
|
|
30
|
-
watcher.
|
|
31
|
-
|
|
30
|
+
this.lowlevel_handler = this.lowlevel_handler.bind(this);
|
|
31
|
+
this.handler = this.handler.bind(this);
|
|
32
|
+
this.cwd = watcher.options.cwd;
|
|
33
|
+
}
|
|
34
|
+
listen(item) {
|
|
35
|
+
if (!allowedEvents.includes(item)) {
|
|
36
|
+
throw new Error(`not allowed watcher event name: ${item}`);
|
|
37
|
+
}
|
|
38
|
+
if (this.watcher.listenerCount(item) === 0) {
|
|
39
|
+
this.watcher.addListener(item, this.lowlevel_handler);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
unlisten(item) {
|
|
43
|
+
if (!allowedEvents.includes(item)) {
|
|
44
|
+
throw new Error(`not allowed watcher event name: ${item}`);
|
|
45
|
+
}
|
|
46
|
+
this.watcher.removeAllListeners(item);
|
|
32
47
|
}
|
|
33
48
|
_debounce;
|
|
34
49
|
debounce() {
|
|
@@ -36,13 +51,13 @@ export class WatchHelper {
|
|
|
36
51
|
clearTimeout(this._debounce);
|
|
37
52
|
this._debounce = undefined;
|
|
38
53
|
}
|
|
39
|
-
this._debounce = setTimeout(this.
|
|
54
|
+
this._debounce = setTimeout(this.handler, this.debounceMs);
|
|
40
55
|
}
|
|
41
56
|
changeState(s) {
|
|
42
57
|
log(`state change: ${State[s]}`);
|
|
43
58
|
this.state = s;
|
|
44
59
|
}
|
|
45
|
-
|
|
60
|
+
lowlevel_handler(path) {
|
|
46
61
|
log('file changes: %s', path);
|
|
47
62
|
this.changes.add(path);
|
|
48
63
|
switch (this.state) {
|
|
@@ -62,7 +77,7 @@ export class WatchHelper {
|
|
|
62
77
|
throw new Error(`Invalid program state. ${this.state}`);
|
|
63
78
|
}
|
|
64
79
|
}
|
|
65
|
-
|
|
80
|
+
handler() {
|
|
66
81
|
log('trigger...');
|
|
67
82
|
const changes = [...this.changes.values()];
|
|
68
83
|
this.changes.clear();
|
|
@@ -72,12 +87,12 @@ export class WatchHelper {
|
|
|
72
87
|
.then(() => {
|
|
73
88
|
this.onChange(changes);
|
|
74
89
|
})
|
|
75
|
-
.catch(
|
|
90
|
+
.catch(e => {
|
|
76
91
|
log('Failed callback: %s', e.stack);
|
|
77
92
|
})
|
|
78
93
|
.finally(() => {
|
|
79
94
|
if (this.state === State.RESCHEDULE) {
|
|
80
|
-
this.
|
|
95
|
+
this.handler();
|
|
81
96
|
}
|
|
82
97
|
else if (this.state === State.BUSY) {
|
|
83
98
|
this.changeState(State.IDLE);
|
|
@@ -91,51 +106,97 @@ export class WatchHelper {
|
|
|
91
106
|
this.lastRun = undefined;
|
|
92
107
|
});
|
|
93
108
|
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
return this._watches.size;
|
|
109
|
+
get empty() {
|
|
110
|
+
return Object.keys(this.watcher.getWatched()).length === 0;
|
|
97
111
|
}
|
|
98
|
-
|
|
112
|
+
get size() {
|
|
113
|
+
let sum = 0;
|
|
114
|
+
for (const item of Object.values(this.watcher.getWatched())) {
|
|
115
|
+
sum += item.length;
|
|
116
|
+
}
|
|
117
|
+
return sum;
|
|
118
|
+
}
|
|
119
|
+
replace(newList) {
|
|
99
120
|
const newSet = new Set(newList);
|
|
100
|
-
for (const item of
|
|
101
|
-
|
|
121
|
+
for (const item of this.trackedFiles) {
|
|
122
|
+
if (!newSet.has(item)) {
|
|
123
|
+
this.delete(item);
|
|
124
|
+
}
|
|
102
125
|
}
|
|
103
|
-
for (const item of this.
|
|
126
|
+
for (const item of this.watches) {
|
|
104
127
|
if (!newSet.has(item)) {
|
|
105
|
-
this.
|
|
128
|
+
this.delete(item);
|
|
106
129
|
}
|
|
107
130
|
}
|
|
131
|
+
this.add([...newSet]);
|
|
108
132
|
}
|
|
109
|
-
addWatch(
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
133
|
+
addWatch = deprecate(this.add.bind(this), 'use add instead of addWatch');
|
|
134
|
+
delWatch = deprecate(this.delete.bind(this), 'use delete instead of delWatch');
|
|
135
|
+
add(files) {
|
|
136
|
+
// console.log('[watch:add] %s', files)
|
|
137
|
+
if (typeof files === 'string') {
|
|
138
|
+
this.trackedFiles.add(files);
|
|
114
139
|
}
|
|
140
|
+
else {
|
|
141
|
+
for (const file of files) {
|
|
142
|
+
this.trackedFiles.add(file);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
this.watcher.add(files);
|
|
115
146
|
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
this.
|
|
120
|
-
this._watches.delete(oldWatch);
|
|
147
|
+
delete(files) {
|
|
148
|
+
// console.log('[watch:del] %s', files)
|
|
149
|
+
if (typeof files === 'string') {
|
|
150
|
+
this.trackedFiles.delete(files);
|
|
121
151
|
}
|
|
152
|
+
else {
|
|
153
|
+
for (const file of files) {
|
|
154
|
+
this.trackedFiles.delete(file);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
this.watcher.unwatch(files);
|
|
122
158
|
}
|
|
123
159
|
reset() {
|
|
124
|
-
this.watcher.unwatch([...this.
|
|
125
|
-
this.
|
|
160
|
+
this.watcher.unwatch([...this.trackedFiles]);
|
|
161
|
+
this.trackedFiles.clear();
|
|
162
|
+
this.watcher.unwatch(this.watches);
|
|
163
|
+
}
|
|
164
|
+
get expectedWatches() {
|
|
165
|
+
return [...this.trackedFiles];
|
|
166
|
+
}
|
|
167
|
+
get trackingSize() {
|
|
168
|
+
return this.trackedFiles.size;
|
|
126
169
|
}
|
|
127
170
|
get watches() {
|
|
128
|
-
|
|
171
|
+
const list = [];
|
|
172
|
+
for (const [folder, files] of Object.entries(this.watcher.getWatched())) {
|
|
173
|
+
for (const file of files) {
|
|
174
|
+
if (this.cwd) {
|
|
175
|
+
list.push(join(this.cwd, folder, file));
|
|
176
|
+
}
|
|
177
|
+
else {
|
|
178
|
+
list.push(join(folder, file));
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return list;
|
|
129
183
|
}
|
|
130
|
-
dispose() {
|
|
131
|
-
|
|
184
|
+
async dispose() {
|
|
185
|
+
await Promise.all([this.watcher.close(), this.lastRun]);
|
|
132
186
|
}
|
|
133
187
|
}
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
188
|
+
const allowedEvents = ['add', 'addDir', 'change', 'unlink', 'unlinkDir', 'ready'];
|
|
189
|
+
const defaultOptions = {
|
|
190
|
+
ignoreInitial: true,
|
|
191
|
+
debounceMs: 500,
|
|
192
|
+
watchingEvents: ['add', 'change'],
|
|
193
|
+
};
|
|
194
|
+
export function startChokidar(reload, options = {}) {
|
|
195
|
+
const opts = Object.assign({}, defaultOptions, options);
|
|
196
|
+
const watcher = new WatchHelper(new FSWatcher(opts), reload);
|
|
197
|
+
for (const item of opts.watchingEvents) {
|
|
198
|
+
watcher.listen(item);
|
|
199
|
+
}
|
|
200
|
+
return watcher;
|
|
140
201
|
}
|
|
141
202
|
//# sourceMappingURL=main.js.map
|
package/lib/esm/main.js.map
CHANGED
|
@@ -6,5 +6,5 @@
|
|
|
6
6
|
"../../src/main.ts"
|
|
7
7
|
],
|
|
8
8
|
"names": [],
|
|
9
|
-
"mappings": "
|
|
9
|
+
"mappings": "wBAAgD,UAAU;;AAC1D,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAEtC,MAAM,GAAG,GAAG,KAAK,CAAC,UAAU,CAAC,CAAC;AAE9B,IAAK,KASJ;AATD,WAAK,KAAK;IACT,iBAAiB;IACjB,iCAAQ,CAAA;IACR,mBAAmB;IACnB,iCAAQ,CAAA;IACR,WAAW;IACX,yCAAY,CAAA;IACZ,uDAAuD;IACvD,6CAAc,CAAA;AACf,CAAC,EATI,KAAK,KAAL,KAAK,QAST;AAED,MAAM,OAAO,WAAW;IAUL;IACA;IAVV,KAAK,GAAG,KAAK,CAAC,IAAI,CAAC;IAEpB,UAAU,GAAG,GAAG,CAAC;IAChB,OAAO,CAAiB;IACxB,OAAO,GAAG,IAAI,GAAG,EAAU,CAAC;IACnB,GAAG,CAAqB;IACjC,YAAY,GAAG,IAAI,GAAG,EAAU,CAAC;IAEzC,YACkB,OAAkB,EAClB,QAAyB;QADzB,YAAO,GAAP,OAAO,CAAW;QAClB,aAAQ,GAAR,QAAQ,CAAiB;QAE1C,IAAI,CAAC,gBAAgB,GAAG,IAAI,CAAC,gBAAgB,CAAC,IAAI,CAAC,IAAI,CAAC,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ACrC,aAAa,EAAE,IAAI;IACnB,UAAU,EAAE,GAAG;IACf,cAAc,EAAE,CAAC,KAAK,EAAE,QAAQ,CAAC;CACjC,CAAC;AAEF,MAAM,UAAU,aAAa,CAAC,MAAuB,EAAE,UAAkC,EAAE;IAC1F,MAAM,IAAI,GAAG,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,cAAc,EAAE,OAAO,CAAC,CAAC;IACxD,MAAM,OAAO,GAAG,IAAI,WAAW,CAAC,IAAI,SAAS,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC,CAAC;IAC7D,KAAK,MAAM,IAAI,IAAI,IAAI,CAAC,cAAc,EAAE,CAAC;QACxC,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IACtB,CAAC;IACD,OAAO,OAAO,CAAC;AAChB,CAAC"
|
|
10
10
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@idlebox/chokidar",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0
|
|
4
|
+
"version": "0.1.0",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "GongT <admin@gongt.me> https://github.com/gongt/",
|
|
7
7
|
"main": "./lib/cjs/__create_index.generated.cjs",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"dependencies": {
|
|
19
19
|
"chokidar": "^4.0.1",
|
|
20
20
|
"debug": "^4.3.7",
|
|
21
|
-
"@idlebox/common": "^1.4.
|
|
21
|
+
"@idlebox/common": "^1.4.6"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
24
24
|
"@rushstack/heft": "^0.68.2",
|
package/src/main.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { FSWatcher } from 'chokidar';
|
|
1
|
+
import { FSWatcher, type ChokidarOptions } from 'chokidar';
|
|
3
2
|
import debug from 'debug';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { deprecate } from 'node:util';
|
|
4
5
|
|
|
5
6
|
const log = debug('chokidar');
|
|
6
7
|
|
|
@@ -16,21 +17,38 @@ enum State {
|
|
|
16
17
|
}
|
|
17
18
|
|
|
18
19
|
export class WatchHelper implements IWatchHelper {
|
|
19
|
-
private _watches = new Set<string>();
|
|
20
20
|
private state = State.IDLE;
|
|
21
21
|
|
|
22
22
|
public debounceMs = 800;
|
|
23
23
|
private lastRun?: Promise<void>;
|
|
24
24
|
private changes = new Set<string>();
|
|
25
|
+
private readonly cwd: string | undefined;
|
|
26
|
+
private trackedFiles = new Set<string>();
|
|
25
27
|
|
|
26
28
|
constructor(
|
|
27
29
|
private readonly watcher: FSWatcher,
|
|
28
|
-
private readonly onChange: IReloadFunction
|
|
30
|
+
private readonly onChange: IReloadFunction,
|
|
29
31
|
) {
|
|
30
|
-
this.
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
watcher.
|
|
32
|
+
this.lowlevel_handler = this.lowlevel_handler.bind(this);
|
|
33
|
+
this.handler = this.handler.bind(this);
|
|
34
|
+
|
|
35
|
+
this.cwd = watcher.options.cwd;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
listen(item: (typeof allowedEvents)[number]) {
|
|
39
|
+
if (!allowedEvents.includes(item)) {
|
|
40
|
+
throw new Error(`not allowed watcher event name: ${item}`);
|
|
41
|
+
}
|
|
42
|
+
if (this.watcher.listenerCount(item) === 0) {
|
|
43
|
+
this.watcher.addListener(item, this.lowlevel_handler);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
unlisten(item: (typeof allowedEvents)[number]) {
|
|
47
|
+
if (!allowedEvents.includes(item)) {
|
|
48
|
+
throw new Error(`not allowed watcher event name: ${item}`);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
this.watcher.removeAllListeners(item);
|
|
34
52
|
}
|
|
35
53
|
|
|
36
54
|
private _debounce?: NodeJS.Timeout;
|
|
@@ -39,7 +57,7 @@ export class WatchHelper implements IWatchHelper {
|
|
|
39
57
|
clearTimeout(this._debounce);
|
|
40
58
|
this._debounce = undefined;
|
|
41
59
|
}
|
|
42
|
-
this._debounce = setTimeout(this.
|
|
60
|
+
this._debounce = setTimeout(this.handler, this.debounceMs);
|
|
43
61
|
}
|
|
44
62
|
|
|
45
63
|
private changeState(s: State) {
|
|
@@ -47,7 +65,7 @@ export class WatchHelper implements IWatchHelper {
|
|
|
47
65
|
this.state = s;
|
|
48
66
|
}
|
|
49
67
|
|
|
50
|
-
private
|
|
68
|
+
private lowlevel_handler(path: string) {
|
|
51
69
|
log('file changes: %s', path);
|
|
52
70
|
this.changes.add(path);
|
|
53
71
|
switch (this.state) {
|
|
@@ -68,7 +86,7 @@ export class WatchHelper implements IWatchHelper {
|
|
|
68
86
|
}
|
|
69
87
|
}
|
|
70
88
|
|
|
71
|
-
private
|
|
89
|
+
private handler() {
|
|
72
90
|
log('trigger...');
|
|
73
91
|
|
|
74
92
|
const changes = [...this.changes.values()];
|
|
@@ -80,12 +98,12 @@ export class WatchHelper implements IWatchHelper {
|
|
|
80
98
|
.then(() => {
|
|
81
99
|
this.onChange(changes);
|
|
82
100
|
})
|
|
83
|
-
.catch(
|
|
101
|
+
.catch(e => {
|
|
84
102
|
log('Failed callback: %s', e.stack);
|
|
85
103
|
})
|
|
86
104
|
.finally(() => {
|
|
87
105
|
if (this.state === State.RESCHEDULE) {
|
|
88
|
-
this.
|
|
106
|
+
this.handler();
|
|
89
107
|
} else if (this.state === State.BUSY) {
|
|
90
108
|
this.changeState(State.IDLE);
|
|
91
109
|
} else {
|
|
@@ -98,70 +116,140 @@ export class WatchHelper implements IWatchHelper {
|
|
|
98
116
|
});
|
|
99
117
|
}
|
|
100
118
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
return this._watches.size;
|
|
119
|
+
get empty() {
|
|
120
|
+
return Object.keys(this.watcher.getWatched()).length === 0;
|
|
104
121
|
}
|
|
105
122
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
for (const item of
|
|
109
|
-
|
|
123
|
+
get size() {
|
|
124
|
+
let sum = 0;
|
|
125
|
+
for (const item of Object.values(this.watcher.getWatched())) {
|
|
126
|
+
sum += item.length;
|
|
110
127
|
}
|
|
128
|
+
return sum;
|
|
129
|
+
}
|
|
111
130
|
|
|
112
|
-
|
|
131
|
+
replace(newList: readonly string[]) {
|
|
132
|
+
const newSet = new Set(newList);
|
|
133
|
+
for (const item of this.trackedFiles) {
|
|
134
|
+
if (!newSet.has(item)) {
|
|
135
|
+
this.delete(item);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
for (const item of this.watches) {
|
|
113
139
|
if (!newSet.has(item)) {
|
|
114
|
-
this.
|
|
140
|
+
this.delete(item);
|
|
115
141
|
}
|
|
116
142
|
}
|
|
143
|
+
|
|
144
|
+
this.add([...newSet]);
|
|
117
145
|
}
|
|
118
146
|
|
|
119
|
-
addWatch(
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
147
|
+
addWatch = deprecate(this.add.bind(this), 'use add instead of addWatch');
|
|
148
|
+
delWatch = deprecate(this.delete.bind(this), 'use delete instead of delWatch');
|
|
149
|
+
|
|
150
|
+
add(files: string | readonly string[]) {
|
|
151
|
+
// console.log('[watch:add] %s', files)
|
|
152
|
+
if (typeof files === 'string') {
|
|
153
|
+
this.trackedFiles.add(files);
|
|
154
|
+
} else {
|
|
155
|
+
for (const file of files) {
|
|
156
|
+
this.trackedFiles.add(file);
|
|
157
|
+
}
|
|
124
158
|
}
|
|
159
|
+
this.watcher.add(files as string[]);
|
|
125
160
|
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
this.
|
|
161
|
+
|
|
162
|
+
delete(files: string | readonly string[]) {
|
|
163
|
+
// console.log('[watch:del] %s', files)
|
|
164
|
+
if (typeof files === 'string') {
|
|
165
|
+
this.trackedFiles.delete(files);
|
|
166
|
+
} else {
|
|
167
|
+
for (const file of files) {
|
|
168
|
+
this.trackedFiles.delete(file);
|
|
169
|
+
}
|
|
131
170
|
}
|
|
171
|
+
this.watcher.unwatch(files as string[]);
|
|
132
172
|
}
|
|
133
173
|
|
|
134
174
|
reset() {
|
|
135
|
-
this.watcher.unwatch([...this.
|
|
136
|
-
this.
|
|
175
|
+
this.watcher.unwatch([...this.trackedFiles]);
|
|
176
|
+
this.trackedFiles.clear();
|
|
177
|
+
this.watcher.unwatch(this.watches);
|
|
137
178
|
}
|
|
138
179
|
|
|
139
|
-
get
|
|
140
|
-
return [...this.
|
|
180
|
+
get expectedWatches() {
|
|
181
|
+
return [...this.trackedFiles];
|
|
141
182
|
}
|
|
142
183
|
|
|
143
|
-
|
|
144
|
-
return
|
|
184
|
+
get trackingSize() {
|
|
185
|
+
return this.trackedFiles.size;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
get watches(): string[] {
|
|
189
|
+
const list = [];
|
|
190
|
+
for (const [folder, files] of Object.entries(this.watcher.getWatched())) {
|
|
191
|
+
for (const file of files) {
|
|
192
|
+
if (this.cwd) {
|
|
193
|
+
list.push(join(this.cwd, folder, file));
|
|
194
|
+
} else {
|
|
195
|
+
list.push(join(folder, file));
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
return list;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
async dispose() {
|
|
203
|
+
await Promise.all([this.watcher.close(), this.lastRun]);
|
|
145
204
|
}
|
|
146
205
|
}
|
|
147
206
|
|
|
148
207
|
export interface IWatchHelper {
|
|
149
|
-
|
|
150
|
-
|
|
208
|
+
/** @deprecated use add */
|
|
209
|
+
addWatch(files: string | readonly string[]): void;
|
|
210
|
+
/** @deprecated use delete */
|
|
211
|
+
delWatch(files: string | readonly string[]): void;
|
|
212
|
+
|
|
213
|
+
add(files: string | readonly string[]): void;
|
|
214
|
+
delete(files: string | readonly string[]): void;
|
|
215
|
+
replace(files: readonly string[]): void;
|
|
151
216
|
reset(): void;
|
|
217
|
+
|
|
218
|
+
/** 注意: chokidar的api无法同步获取监听文件数量,这个数值不会实时的反映实际情况 */
|
|
219
|
+
readonly size: number;
|
|
220
|
+
/** 注意: chokidar的api无法同步获取监听文件数量,这个数值不会实时的反映实际情况 */
|
|
221
|
+
readonly empty: boolean;
|
|
222
|
+
|
|
223
|
+
/** 本地维护的文件列表,和实际不一定相符,比如 符号链接、文件删除自动取消监视 等情况均无法同步 */
|
|
224
|
+
readonly trackingSize: number;
|
|
225
|
+
readonly expectedWatches: string[];
|
|
226
|
+
|
|
227
|
+
listen(item: (typeof allowedEvents)[number]): void;
|
|
228
|
+
unlisten(item: (typeof allowedEvents)[number]): void;
|
|
229
|
+
|
|
152
230
|
dispose(): Promise<void>;
|
|
153
|
-
readonly watches:
|
|
231
|
+
readonly watches: string[];
|
|
154
232
|
}
|
|
155
233
|
|
|
156
234
|
type IReloadFunction = (changes: string[]) => void | Promise<void>;
|
|
235
|
+
const allowedEvents = ['add', 'addDir', 'change', 'unlink', 'unlinkDir', 'ready'] as const;
|
|
236
|
+
|
|
237
|
+
export interface IExtraOptions extends ChokidarOptions {
|
|
238
|
+
debounceMs: number;
|
|
239
|
+
watchingEvents: (typeof allowedEvents)[number][];
|
|
240
|
+
}
|
|
157
241
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
242
|
+
const defaultOptions: IExtraOptions = {
|
|
243
|
+
ignoreInitial: true,
|
|
244
|
+
debounceMs: 500,
|
|
245
|
+
watchingEvents: ['add', 'change'],
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
export function startChokidar(reload: IReloadFunction, options: Partial<IExtraOptions> = {}): IWatchHelper {
|
|
249
|
+
const opts = Object.assign({}, defaultOptions, options);
|
|
250
|
+
const watcher = new WatchHelper(new FSWatcher(opts), reload);
|
|
251
|
+
for (const item of opts.watchingEvents) {
|
|
252
|
+
watcher.listen(item);
|
|
253
|
+
}
|
|
254
|
+
return watcher;
|
|
167
255
|
}
|