1
1
/// <reference path="core.ts"/>
2
2
3
3
namespace ts {
4
- export type FileWatcherCallback = ( path : string , removed ?: boolean ) => void ;
5
- export type DirectoryWatcherCallback = ( path : string ) => void ;
4
+ export type FileWatcherCallback = ( fileName : string , removed ?: boolean ) => void ;
5
+ export type DirectoryWatcherCallback = ( directoryName : string ) => void ;
6
6
7
7
export interface System {
8
8
args : string [ ] ;
@@ -11,7 +11,7 @@ namespace ts {
11
11
write ( s : string ) : void ;
12
12
readFile ( path : string , encoding ?: string ) : string ;
13
13
writeFile ( path : string , data : string , writeByteOrderMark ?: boolean ) : void ;
14
- watchFile ?( path : Path , callback : FileWatcherCallback ) : FileWatcher ;
14
+ watchFile ?( path : string , callback : FileWatcherCallback ) : FileWatcher ;
15
15
watchDirectory ?( path : string , callback : DirectoryWatcherCallback , recursive ?: boolean ) : FileWatcher ;
16
16
resolvePath ( path : string ) : string ;
17
17
fileExists ( path : string ) : boolean ;
@@ -25,7 +25,7 @@ namespace ts {
25
25
}
26
26
27
27
interface WatchedFile {
28
- filePath : Path ;
28
+ fileName : string ;
29
29
callback : FileWatcherCallback ;
30
30
mtime ?: Date ;
31
31
}
@@ -35,7 +35,7 @@ namespace ts {
35
35
}
36
36
37
37
export interface DirectoryWatcher extends FileWatcher {
38
- directoryPath : Path ;
38
+ directoryName : string ;
39
39
referenceCount : number ;
40
40
}
41
41
@@ -244,13 +244,13 @@ namespace ts {
244
244
return ;
245
245
}
246
246
247
- _fs . stat ( watchedFile . filePath , ( err : any , stats : any ) => {
247
+ _fs . stat ( watchedFile . fileName , ( err : any , stats : any ) => {
248
248
if ( err ) {
249
- watchedFile . callback ( watchedFile . filePath ) ;
249
+ watchedFile . callback ( watchedFile . fileName ) ;
250
250
}
251
251
else if ( watchedFile . mtime . getTime ( ) !== stats . mtime . getTime ( ) ) {
252
- watchedFile . mtime = getModifiedTime ( watchedFile . filePath ) ;
253
- watchedFile . callback ( watchedFile . filePath , watchedFile . mtime . getTime ( ) === 0 ) ;
252
+ watchedFile . mtime = getModifiedTime ( watchedFile . fileName ) ;
253
+ watchedFile . callback ( watchedFile . fileName , watchedFile . mtime . getTime ( ) === 0 ) ;
254
254
}
255
255
} ) ;
256
256
}
@@ -278,11 +278,11 @@ namespace ts {
278
278
} , interval ) ;
279
279
}
280
280
281
- function addFile ( filePath : Path , callback : FileWatcherCallback ) : WatchedFile {
281
+ function addFile ( fileName : string , callback : FileWatcherCallback ) : WatchedFile {
282
282
const file : WatchedFile = {
283
- filePath ,
283
+ fileName ,
284
284
callback,
285
- mtime : getModifiedTime ( filePath )
285
+ mtime : getModifiedTime ( fileName )
286
286
} ;
287
287
288
288
watchedFiles . push ( file ) ;
@@ -306,26 +306,26 @@ namespace ts {
306
306
}
307
307
308
308
function createWatchedFileSet ( ) {
309
- const dirWatchers = createFileMap < DirectoryWatcher > ( ) ;
309
+ const dirWatchers : Map < DirectoryWatcher > = { } ;
310
310
// One file can have multiple watchers
311
- const fileWatcherCallbacks = createFileMap < FileWatcherCallback [ ] > ( ) ;
311
+ const fileWatcherCallbacks : Map < FileWatcherCallback [ ] > = { } ;
312
312
return { addFile, removeFile } ;
313
313
314
- function reduceDirWatcherRefCountForFile ( filePath : Path ) {
315
- const dirPath = getDirectoryPath ( filePath ) ;
316
- if ( dirWatchers . contains ( dirPath ) ) {
317
- const watcher = dirWatchers . get ( dirPath ) ;
314
+ function reduceDirWatcherRefCountForFile ( fileName : string ) {
315
+ const dirName = getDirectoryPath ( fileName ) ;
316
+ if ( hasProperty ( dirWatchers , dirName ) ) {
317
+ const watcher = dirWatchers [ dirName ] ;
318
318
watcher . referenceCount -= 1 ;
319
319
if ( watcher . referenceCount <= 0 ) {
320
320
watcher . close ( ) ;
321
- dirWatchers . remove ( dirPath ) ;
321
+ delete dirWatchers [ dirName ] ;
322
322
}
323
323
}
324
324
}
325
325
326
- function addDirWatcher ( dirPath : Path ) : void {
327
- if ( dirWatchers . contains ( dirPath ) ) {
328
- const watcher = dirWatchers . get ( dirPath ) ;
326
+ function addDirWatcher ( dirPath : string ) : void {
327
+ if ( hasProperty ( dirWatchers , dirPath ) ) {
328
+ const watcher = dirWatchers [ dirPath ] ;
329
329
watcher . referenceCount += 1 ;
330
330
return ;
331
331
}
@@ -336,52 +336,52 @@ namespace ts {
336
336
( eventName : string , relativeFileName : string ) => fileEventHandler ( eventName , relativeFileName , dirPath )
337
337
) ;
338
338
watcher . referenceCount = 1 ;
339
- dirWatchers . set ( dirPath , watcher ) ;
339
+ dirWatchers [ dirPath ] = watcher ;
340
340
return ;
341
341
}
342
342
343
- function addFileWatcherCallback ( filePath : Path , callback : FileWatcherCallback ) : void {
344
- if ( fileWatcherCallbacks . contains ( filePath ) ) {
345
- fileWatcherCallbacks . get ( filePath ) . push ( callback ) ;
343
+ function addFileWatcherCallback ( filePath : string , callback : FileWatcherCallback ) : void {
344
+ if ( hasProperty ( fileWatcherCallbacks , filePath ) ) {
345
+ fileWatcherCallbacks [ filePath ] . push ( callback ) ;
346
346
}
347
347
else {
348
- fileWatcherCallbacks . set ( filePath , [ callback ] ) ;
348
+ fileWatcherCallbacks [ filePath ] = [ callback ] ;
349
349
}
350
350
}
351
351
352
- function addFile ( filePath : Path , callback : FileWatcherCallback ) : WatchedFile {
353
- addFileWatcherCallback ( filePath , callback ) ;
354
- addDirWatcher ( getDirectoryPath ( filePath ) ) ;
352
+ function addFile ( fileName : string , callback : FileWatcherCallback ) : WatchedFile {
353
+ addFileWatcherCallback ( fileName , callback ) ;
354
+ addDirWatcher ( getDirectoryPath ( fileName ) ) ;
355
355
356
- return { filePath , callback } ;
356
+ return { fileName , callback } ;
357
357
}
358
358
359
359
function removeFile ( watchedFile : WatchedFile ) {
360
- removeFileWatcherCallback ( watchedFile . filePath , watchedFile . callback ) ;
361
- reduceDirWatcherRefCountForFile ( watchedFile . filePath ) ;
360
+ removeFileWatcherCallback ( watchedFile . fileName , watchedFile . callback ) ;
361
+ reduceDirWatcherRefCountForFile ( watchedFile . fileName ) ;
362
362
}
363
363
364
- function removeFileWatcherCallback ( filePath : Path , callback : FileWatcherCallback ) {
365
- if ( fileWatcherCallbacks . contains ( filePath ) ) {
366
- const newCallbacks = copyListRemovingItem ( callback , fileWatcherCallbacks . get ( filePath ) ) ;
364
+ function removeFileWatcherCallback ( filePath : string , callback : FileWatcherCallback ) {
365
+ if ( hasProperty ( fileWatcherCallbacks , filePath ) ) {
366
+ const newCallbacks = copyListRemovingItem ( callback , fileWatcherCallbacks [ filePath ] ) ;
367
367
if ( newCallbacks . length === 0 ) {
368
- fileWatcherCallbacks . remove ( filePath ) ;
368
+ delete fileWatcherCallbacks [ filePath ] ;
369
369
}
370
370
else {
371
- fileWatcherCallbacks . set ( filePath , newCallbacks ) ;
371
+ fileWatcherCallbacks [ filePath ] = newCallbacks ;
372
372
}
373
373
}
374
374
}
375
375
376
- function fileEventHandler ( eventName : string , relativeFileName : string , baseDirPath : Path ) {
376
+ function fileEventHandler ( eventName : string , relativeFileName : string , baseDirPath : string ) {
377
377
// When files are deleted from disk, the triggered "rename" event would have a relativefileName of "undefined"
378
- const filePath = typeof relativeFileName !== "string"
378
+ const fileName = typeof relativeFileName !== "string"
379
379
? undefined
380
- : toPath ( relativeFileName , baseDirPath , createGetCanonicalFileName ( sys . useCaseSensitiveFileNames ) ) ;
380
+ : ts . getNormalizedAbsolutePath ( relativeFileName , baseDirPath ) ;
381
381
// Some applications save a working file via rename operations
382
- if ( ( eventName === "change" || eventName === "rename" ) && fileWatcherCallbacks . contains ( filePath ) ) {
383
- for ( const fileCallback of fileWatcherCallbacks . get ( filePath ) ) {
384
- fileCallback ( filePath ) ;
382
+ if ( ( eventName === "change" || eventName === "rename" ) && hasProperty ( fileWatcherCallbacks , fileName ) ) {
383
+ for ( const fileCallback of fileWatcherCallbacks [ fileName ] ) {
384
+ fileCallback ( fileName ) ;
385
385
}
386
386
}
387
387
}
@@ -526,18 +526,18 @@ namespace ts {
526
526
} ,
527
527
readFile,
528
528
writeFile,
529
- watchFile : ( filePath , callback ) => {
529
+ watchFile : ( fileName , callback ) => {
530
530
// Node 4.0 stabilized the `fs.watch` function on Windows which avoids polling
531
531
// and is more efficient than `fs.watchFile` (ref: https://github.com/nodejs/node/pull/2649
532
532
// and https://github.com/Microsoft/TypeScript/issues/4643), therefore
533
533
// if the current node.js version is newer than 4, use `fs.watch` instead.
534
534
const watchSet = isNode4OrLater ( ) ? watchedFileSet : pollingWatchedFileSet ;
535
- const watchedFile = watchSet . addFile ( filePath , callback ) ;
535
+ const watchedFile = watchSet . addFile ( fileName , callback ) ;
536
536
return {
537
537
close : ( ) => watchSet . removeFile ( watchedFile )
538
538
} ;
539
539
} ,
540
- watchDirectory : ( path , callback , recursive ) => {
540
+ watchDirectory : ( directoryName , callback , recursive ) => {
541
541
// Node 4.0 `fs.watch` function supports the "recursive" option on both OSX and Windows
542
542
// (ref: https://github.com/nodejs/node/pull/2649 and https://github.com/Microsoft/TypeScript/issues/4643)
543
543
let options : any ;
@@ -549,15 +549,15 @@ namespace ts {
549
549
}
550
550
551
551
return _fs . watch (
552
- path ,
552
+ directoryName ,
553
553
options ,
554
554
( eventName : string , relativeFileName : string ) => {
555
555
// In watchDirectory we only care about adding and removing files (when event name is
556
556
// "rename"); changes made within files are handled by corresponding fileWatchers (when
557
557
// event name is "change")
558
558
if ( eventName === "rename" ) {
559
559
// When deleting a file, the passed baseFileName is null
560
- callback ( ! relativeFileName ? relativeFileName : normalizePath ( combinePaths ( path , relativeFileName ) ) ) ;
560
+ callback ( ! relativeFileName ? relativeFileName : normalizePath ( combinePaths ( directoryName , relativeFileName ) ) ) ;
561
561
} ;
562
562
}
563
563
) ;
0 commit comments