fix 'fileExists' check by using stat directly
This commit is contained in:
parent
20f7b18d99
commit
354e650341
1 changed files with 28 additions and 7 deletions
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue