1
1
import { EventEmitter } from 'eventemitter3' ;
2
2
import type { EventEmitter as NodeEventEmitter } from 'node:events' ;
3
3
import type * as fs from 'node:fs' ;
4
+ import type { V_Context } from '../context.js' ;
4
5
import { ErrnoError } from '../error.js' ;
5
6
import { isStatsEqual , type Stats } from '../stats.js' ;
6
7
import { normalizePath } from '../utils.js' ;
@@ -24,7 +25,13 @@ class Watcher<TEvents extends Record<string, unknown[]> = Record<string, unknown
24
25
}
25
26
/* eslint-enable @typescript-eslint/no-explicit-any */
26
27
27
- public constructor ( public readonly path : string ) {
28
+ public constructor (
29
+ /**
30
+ * @internal
31
+ */
32
+ public readonly _context : V_Context ,
33
+ public readonly path : string
34
+ ) {
28
35
super ( ) ;
29
36
}
30
37
@@ -71,10 +78,11 @@ export class FSWatcher<T extends string | Buffer = string | Buffer>
71
78
implements fs . FSWatcher
72
79
{
73
80
public constructor (
81
+ context : V_Context ,
74
82
path : string ,
75
83
public readonly options : fs . WatchOptions
76
84
) {
77
- super ( path ) ;
85
+ super ( context , path ) ;
78
86
addWatcher ( path . toString ( ) , this ) ;
79
87
}
80
88
@@ -105,10 +113,11 @@ export class StatWatcher
105
113
private previous ?: Stats ;
106
114
107
115
public constructor (
116
+ context : V_Context ,
108
117
path : string ,
109
118
private options : { persistent ?: boolean ; interval ?: number }
110
119
) {
111
- super ( path ) ;
120
+ super ( context , path ) ;
112
121
this . start ( ) ;
113
122
}
114
123
@@ -185,10 +194,16 @@ export function emitChange(eventType: fs.WatchEventType, filename: string) {
185
194
while ( parent !== normalizedFilename ) {
186
195
normalizedFilename = parent ;
187
196
parent = dirname ( parent ) ;
188
- if ( watchers . has ( parent ) ) {
189
- for ( const watcher of watchers . get ( parent ) ! ) {
190
- watcher . emit ( 'change' , eventType , filename . slice ( parent . length + ( parent == '/' ? 0 : 1 ) ) ) ;
191
- }
197
+ if ( ! watchers . has ( parent ) ) continue ;
198
+
199
+ for ( const watcher of watchers . get ( parent ) ! ) {
200
+ // Strip the context root from the path if the watcher has a context
201
+
202
+ const root = watcher . _context ?. root ;
203
+ const contextPath = root && filename . startsWith ( root ) ? filename . slice ( root . length ) : filename ;
204
+ const relativePath = contextPath . slice ( parent . length + ( parent == '/' ? 0 : 1 ) ) ;
205
+
206
+ watcher . emit ( 'change' , eventType , relativePath ) ;
192
207
}
193
208
}
194
209
}
0 commit comments