@@ -26,7 +26,7 @@ namespace ts {
2626 getExecutingFilePath ( ) : string ;
2727 getCurrentDirectory ( ) : string ;
2828 getDirectories ( path : string ) : string [ ] ;
29- readDirectory ( path : string , extension ?: string , exclude ?: string [ ] ) : string [ ] ;
29+ readDirectory ( path : string , extensions ?: string [ ] , exclude ?: string [ ] , include ?: string [ ] ) : string [ ] ;
3030 getModifiedTime ?( path : string ) : Date ;
3131 createHash ?( data : string ) : string ;
3232 getMemoryUsage ?( ) : number ;
@@ -74,7 +74,7 @@ namespace ts {
7474 readFile ( path : string ) : string ;
7575 writeFile ( path : string , contents : string ) : void ;
7676 getDirectories ( path : string ) : string [ ] ;
77- readDirectory ( path : string , extension ?: string , exclude ?: string [ ] ) : string [ ] ;
77+ readDirectory ( path : string , extensions ?: string [ ] , basePaths ?: string [ ] , excludeEx ?: string , includeFileEx ?: string , includeDirEx ?: string ) : string [ ] ;
7878 watchFile ?( path : string , callback : FileWatcherCallback ) : FileWatcher ;
7979 watchDirectory ?( path : string , callback : DirectoryWatcherCallback , recursive ?: boolean ) : FileWatcher ;
8080 realpath ( path : string ) : string ;
@@ -85,6 +85,7 @@ namespace ts {
8585 function getWScriptSystem ( ) : System {
8686
8787 const fso = new ActiveXObject ( "Scripting.FileSystemObject" ) ;
88+ const shell = new ActiveXObject ( "WScript.Shell" ) ;
8889
8990 const fileStream = new ActiveXObject ( "ADODB.Stream" ) ;
9091 fileStream . Type = 2 /*text*/ ;
@@ -152,10 +153,6 @@ namespace ts {
152153 }
153154 }
154155
155- function getCanonicalPath ( path : string ) : string {
156- return path . toLowerCase ( ) ;
157- }
158-
159156 function getNames ( collection : any ) : string [ ] {
160157 const result : string [ ] = [ ] ;
161158 for ( let e = new Enumerator ( collection ) ; ! e . atEnd ( ) ; e . moveNext ( ) ) {
@@ -169,30 +166,22 @@ namespace ts {
169166 return getNames ( folder . subfolders ) ;
170167 }
171168
172- function readDirectory ( path : string , extension ?: string , exclude ?: string [ ] ) : string [ ] {
173- const result : string [ ] = [ ] ;
174- exclude = map ( exclude , s => getCanonicalPath ( combinePaths ( path , s ) ) ) ;
175- visitDirectory ( path ) ;
176- return result ;
177- function visitDirectory ( path : string ) {
169+ function getAccessibleFileSystemEntries ( path : string ) : FileSystemEntries {
170+ try {
178171 const folder = fso . GetFolder ( path || "." ) ;
179172 const files = getNames ( folder . files ) ;
180- for ( const current of files ) {
181- const name = combinePaths ( path , current ) ;
182- if ( ( ! extension || fileExtensionIs ( name , extension ) ) && ! contains ( exclude , getCanonicalPath ( name ) ) ) {
183- result . push ( name ) ;
184- }
185- }
186- const subfolders = getNames ( folder . subfolders ) ;
187- for ( const current of subfolders ) {
188- const name = combinePaths ( path , current ) ;
189- if ( ! contains ( exclude , getCanonicalPath ( name ) ) ) {
190- visitDirectory ( name ) ;
191- }
192- }
173+ const directories = getNames ( folder . subfolders ) ;
174+ return { files, directories } ;
175+ }
176+ catch ( e ) {
177+ return { files : [ ] , directories : [ ] } ;
193178 }
194179 }
195180
181+ function readDirectory ( path : string , extensions ?: string [ ] , excludes ?: string [ ] , includes ?: string [ ] ) : string [ ] {
182+ return matchFiles ( path , extensions , excludes , includes , /*useCaseSensitiveFileNames*/ false , shell . CurrentDirectory , getAccessibleFileSystemEntries ) ;
183+ }
184+
196185 return {
197186 args,
198187 newLine : "\r\n" ,
@@ -220,7 +209,7 @@ namespace ts {
220209 return WScript . ScriptFullName ;
221210 } ,
222211 getCurrentDirectory ( ) {
223- return new ActiveXObject ( "WScript.Shell" ) . CurrentDirectory ;
212+ return shell . CurrentDirectory ;
224213 } ,
225214 getDirectories,
226215 readDirectory,
@@ -381,8 +370,43 @@ namespace ts {
381370 }
382371 }
383372
384- function getCanonicalPath ( path : string ) : string {
385- return useCaseSensitiveFileNames ? path : path . toLowerCase ( ) ;
373+ function getAccessibleFileSystemEntries ( path : string ) : FileSystemEntries {
374+ try {
375+ const entries = _fs . readdirSync ( path || "." ) . sort ( ) ;
376+ const files : string [ ] = [ ] ;
377+ const directories : string [ ] = [ ] ;
378+ for ( const entry of entries ) {
379+ // This is necessary because on some file system node fails to exclude
380+ // "." and "..". See https://github.com/nodejs/node/issues/4002
381+ if ( entry === "." || entry === ".." ) {
382+ continue ;
383+ }
384+ const name = combinePaths ( path , entry ) ;
385+
386+ let stat : any ;
387+ try {
388+ stat = _fs . statSync ( name ) ;
389+ }
390+ catch ( e ) {
391+ continue ;
392+ }
393+
394+ if ( stat . isFile ( ) ) {
395+ files . push ( entry ) ;
396+ }
397+ else if ( stat . isDirectory ( ) ) {
398+ directories . push ( entry ) ;
399+ }
400+ }
401+ return { files, directories } ;
402+ }
403+ catch ( e ) {
404+ return { files : [ ] , directories : [ ] } ;
405+ }
406+ }
407+
408+ function readDirectory ( path : string , extensions ?: string [ ] , excludes ?: string [ ] , includes ?: string [ ] ) : string [ ] {
409+ return matchFiles ( path , extensions , excludes , includes , useCaseSensitiveFileNames , process . cwd ( ) , getAccessibleFileSystemEntries ) ;
386410 }
387411
388412 const enum FileSystemEntryKind {
@@ -415,39 +439,6 @@ namespace ts {
415439 return filter < string > ( _fs . readdirSync ( path ) , p => fileSystemEntryExists ( combinePaths ( path , p ) , FileSystemEntryKind . Directory ) ) ;
416440 }
417441
418- function readDirectory ( path : string , extension ?: string , exclude ?: string [ ] ) : string [ ] {
419- const result : string [ ] = [ ] ;
420- exclude = map ( exclude , s => getCanonicalPath ( combinePaths ( path , s ) ) ) ;
421- visitDirectory ( path ) ;
422- return result ;
423- function visitDirectory ( path : string ) {
424- const files = _fs . readdirSync ( path || "." ) . sort ( ) ;
425- const directories : string [ ] = [ ] ;
426- for ( const current of files ) {
427- // This is necessary because on some file system node fails to exclude
428- // "." and "..". See https://github.com/nodejs/node/issues/4002
429- if ( current === "." || current === ".." ) {
430- continue ;
431- }
432- const name = combinePaths ( path , current ) ;
433- if ( ! contains ( exclude , getCanonicalPath ( name ) ) ) {
434- const stat = _fs . statSync ( name ) ;
435- if ( stat . isFile ( ) ) {
436- if ( ! extension || fileExtensionIs ( name , extension ) ) {
437- result . push ( name ) ;
438- }
439- }
440- else if ( stat . isDirectory ( ) ) {
441- directories . push ( name ) ;
442- }
443- }
444- }
445- for ( const current of directories ) {
446- visitDirectory ( current ) ;
447- }
448- }
449- }
450-
451442 return {
452443 args : process . argv . slice ( 2 ) ,
453444 newLine : _os . EOL ,
@@ -586,7 +577,10 @@ namespace ts {
586577 getExecutingFilePath : ( ) => ChakraHost . executingFile ,
587578 getCurrentDirectory : ( ) => ChakraHost . currentDirectory ,
588579 getDirectories : ChakraHost . getDirectories ,
589- readDirectory : ChakraHost . readDirectory ,
580+ readDirectory : ( path : string , extensions ?: string [ ] , excludes ?: string [ ] , includes ?: string [ ] ) => {
581+ const pattern = getFileMatcherPatterns ( path , extensions , excludes , includes , ! ! ChakraHost . useCaseSensitiveFileNames , ChakraHost . currentDirectory ) ;
582+ return ChakraHost . readDirectory ( path , extensions , pattern . basePaths , pattern . excludePattern , pattern . includeFilePattern , pattern . includeDirectoryPattern ) ;
583+ } ,
590584 exit : ChakraHost . quit ,
591585 realpath
592586 } ;
0 commit comments