diff --git a/src/compiler/sys.ts b/src/compiler/sys.ts index 0a637981636..57dbd7044af 100644 --- a/src/compiler/sys.ts +++ b/src/compiler/sys.ts @@ -411,7 +411,7 @@ namespace ts { const useCaseSensitiveFileNames = platform !== "win32" && platform !== "win64" && platform !== "darwin"; function readFile(fileName: string, encoding?: string): string { - if (!_fs.existsSync(fileName)) { + if (!fileExists(fileName)) { return undefined; } const buffer = _fs.readFileSync(fileName); @@ -462,6 +462,31 @@ namespace ts { return useCaseSensitiveFileNames ? path : path.toLowerCase(); } + const enum FileSystemEntryKind { + File, + Directory + } + function fileSystemEntryExists(path: string, entryKind: FileSystemEntryKind): boolean { + try { + const stat = _fs.statSync(path); + switch (entryKind) { + case FileSystemEntryKind.File: return stat.isFile(); + case FileSystemEntryKind.Directory: return stat.isDirectory(); + } + } + catch (e) { + return false; + } + } + + function fileExists(path: string): boolean { + return fileSystemEntryExists(path, FileSystemEntryKind.File); + } + + function directoryExists(path: string): boolean { + return fileSystemEntryExists(path, FileSystemEntryKind.Directory); + } + function readDirectory(path: string, extension?: string, exclude?: string[]): string[] { const result: string[] = []; exclude = map(exclude, s => getCanonicalPath(combinePaths(path, s))); @@ -538,12 +563,8 @@ namespace ts { resolvePath: function (path: string): string { return _path.resolve(path); }, - fileExists(path: string): boolean { - return _fs.existsSync(path); - }, - directoryExists(path: string) { - return _fs.existsSync(path) && _fs.statSync(path).isDirectory(); - }, + fileExists, + directoryExists, createDirectory(directoryName: string) { if (!this.directoryExists(directoryName)) { _fs.mkdirSync(directoryName);