Skip to content

Commit 178b2da

Browse files
author
Zhengbo Li
committed
Add type alias for filewatching callbacks
1 parent 602cde4 commit 178b2da

2 files changed

Lines changed: 20 additions & 12 deletions

File tree

src/compiler/core.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,9 @@ namespace ts {
612612
return path.substr(0, rootLength) + normalized.join(directorySeparator);
613613
}
614614

615-
export function getDirectoryPath(path: string) {
615+
export function getDirectoryPath(path: Path): Path;
616+
export function getDirectoryPath(path: string): string;
617+
export function getDirectoryPath(path: string): any {
616618
return path.substr(0, Math.max(getRootLength(path), path.lastIndexOf(directorySeparator)));
617619
}
618620

src/compiler/sys.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
/// <reference path="core.ts"/>
22

33
namespace ts {
4+
export type CallbackForWatchedFile = (path: string, removed?: boolean) => void;
5+
export type CallbackForWatchedDirectory = (path: string) => void;
6+
47
export interface System {
58
args: string[];
69
newLine: string;
710
useCaseSensitiveFileNames: boolean;
811
write(s: string): void;
912
readFile(path: string, encoding?: string): string;
1013
writeFile(path: string, data: string, writeByteOrderMark?: boolean): void;
11-
watchFile?(path: string, callback: (path: string, removed?: boolean) => void): FileWatcher;
12-
watchDirectory?(path: string, callback: (path: string) => void, recursive?: boolean): FileWatcher;
14+
watchFile?(path: string, callback: CallbackForWatchedFile): FileWatcher;
15+
watchDirectory?(path: string, callback: CallbackForWatchedDirectory, recursive?: boolean): FileWatcher;
1316
resolvePath(path: string): string;
1417
fileExists(path: string): boolean;
1518
directoryExists(path: string): boolean;
@@ -23,7 +26,7 @@ namespace ts {
2326

2427
interface WatchedFile {
2528
fileName: string;
26-
callback: (fileName: string, removed?: boolean) => void;
29+
callback: CallbackForWatchedFile;
2730
mtime?: Date;
2831
}
2932

@@ -62,8 +65,8 @@ namespace ts {
6265
readFile(path: string): string;
6366
writeFile(path: string, contents: string): void;
6467
readDirectory(path: string, extension?: string, exclude?: string[]): string[];
65-
watchFile?(path: string, callback: (path: string, removed?: boolean) => void): FileWatcher;
66-
watchDirectory?(path: string, callback: (path: string) => void, recursive?: boolean): FileWatcher;
68+
watchFile?(path: string, callback: CallbackForWatchedFile): FileWatcher;
69+
watchDirectory?(path: string, callback: CallbackForWatchedDirectory, recursive?: boolean): FileWatcher;
6770
};
6871

6972
export var sys: System = (function () {
@@ -271,7 +274,7 @@ namespace ts {
271274
}, interval);
272275
}
273276

274-
function addFile(fileName: string, callback: (fileName: string, removed?: boolean) => void): WatchedFile {
277+
function addFile(fileName: string, callback: CallbackForWatchedFile): WatchedFile {
275278
const file: WatchedFile = {
276279
fileName,
277280
callback,
@@ -298,16 +301,18 @@ namespace ts {
298301
};
299302
}
300303

304+
305+
301306
function createWatchedFileSet() {
302307
const watchedDirectories = createFileMap<FileWatcher>();
303-
const watchedFiles = createFileMap<(fileName: string, removed?: boolean) => void>();
308+
const watchedFiles = createFileMap<CallbackForWatchedFile>();
304309
const currentDirectory = process.cwd();
305310

306311
return { addFile, removeFile };
307312

308-
function addFile(fileName: string, callback: (fileName: string, removed?: boolean) => void): WatchedFile {
313+
function addFile(fileName: string, callback: CallbackForWatchedFile): WatchedFile {
309314
const path = toPath(fileName, currentDirectory, getCanonicalPath);
310-
const parentDirPath = toPath(ts.getDirectoryPath(fileName), currentDirectory, getCanonicalPath);
315+
const parentDirPath = getDirectoryPath(path);
311316

312317
if (!watchedDirectories.contains(parentDirPath)) {
313318
watchedDirectories.set(parentDirPath, _fs.watch(
@@ -323,7 +328,7 @@ namespace ts {
323328
const path = toPath(file.fileName, currentDirectory, getCanonicalPath);
324329
watchedFiles.remove(path);
325330

326-
const parentDirPath = toPath(ts.getDirectoryPath(path), currentDirectory, getCanonicalPath);
331+
const parentDirPath = getDirectoryPath(path);
327332
if (watchedDirectories.contains(parentDirPath)) {
328333
let hasWatchedChildren = false;
329334
watchedFiles.forEachValue((key, _) => {
@@ -474,9 +479,10 @@ namespace ts {
474479
watchDirectory: (path, callback, recursive) => {
475480
// Node 4.0 `fs.watch` function supports the "recursive" option on both OSX and Windows
476481
// (ref: https://github.com/nodejs/node/pull/2649 and https://github.com/Microsoft/TypeScript/issues/4643)
482+
const options = isNode4OrLater() ? { persistent: true } : { persistent: true, recursive: !!recursive };
477483
return _fs.watch(
478484
path,
479-
{ persistent: true, recursive: !!recursive },
485+
options,
480486
(eventName: string, relativeFileName: string) => {
481487
// In watchDirectory we only care about adding and removing files (when event name is
482488
// "rename"); changes made within files are handled by corresponding fileWatchers (when

0 commit comments

Comments
 (0)