Skip to content

Commit 354e650

Browse files
committed
fix 'fileExists' check by using stat directly
1 parent 20f7b18 commit 354e650

1 file changed

Lines changed: 28 additions & 7 deletions

File tree

src/compiler/sys.ts

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ namespace ts {
411411
const useCaseSensitiveFileNames = platform !== "win32" && platform !== "win64" && platform !== "darwin";
412412

413413
function readFile(fileName: string, encoding?: string): string {
414-
if (!_fs.existsSync(fileName)) {
414+
if (!fileExists(fileName)) {
415415
return undefined;
416416
}
417417
const buffer = _fs.readFileSync(fileName);
@@ -462,6 +462,31 @@ namespace ts {
462462
return useCaseSensitiveFileNames ? path : path.toLowerCase();
463463
}
464464

465+
const enum FileSystemEntryKind {
466+
File,
467+
Directory
468+
}
469+
function fileSystemEntryExists(path: string, entryKind: FileSystemEntryKind): boolean {
470+
try {
471+
const stat = _fs.statSync(path);
472+
switch (entryKind) {
473+
case FileSystemEntryKind.File: return stat.isFile();
474+
case FileSystemEntryKind.Directory: return stat.isDirectory();
475+
}
476+
}
477+
catch (e) {
478+
return false;
479+
}
480+
}
481+
482+
function fileExists(path: string): boolean {
483+
return fileSystemEntryExists(path, FileSystemEntryKind.File);
484+
}
485+
486+
function directoryExists(path: string): boolean {
487+
return fileSystemEntryExists(path, FileSystemEntryKind.Directory);
488+
}
489+
465490
function readDirectory(path: string, extension?: string, exclude?: string[]): string[] {
466491
const result: string[] = [];
467492
exclude = map(exclude, s => getCanonicalPath(combinePaths(path, s)));
@@ -538,12 +563,8 @@ namespace ts {
538563
resolvePath: function (path: string): string {
539564
return _path.resolve(path);
540565
},
541-
fileExists(path: string): boolean {
542-
return _fs.existsSync(path);
543-
},
544-
directoryExists(path: string) {
545-
return _fs.existsSync(path) && _fs.statSync(path).isDirectory();
546-
},
566+
fileExists,
567+
directoryExists,
547568
createDirectory(directoryName: string) {
548569
if (!this.directoryExists(directoryName)) {
549570
_fs.mkdirSync(directoryName);

0 commit comments

Comments
 (0)