import { FileUtils , StandardizedFilePath , TransactionalFileSystem } from "../fileSystem" ; import { FileSystemEntries , getFileMatcherPatterns , matchFiles } from "../typescript" ; export function readDirectory ( fileSystemWrapper , useCaseSensitiveFileNames , rootDir , extensions , excludes , includes , depth ? ) { const currentDir = fileSystemWrapper . getCurrentDirectory ( ) ; const directories = [ ] ; const regexFlag = useCaseSensitiveFileNames ? "" : "i" ; const patterns = getFileMatcherPatterns ( rootDir , excludes || [ ] , includes , useCaseSensitiveFileNames , currentDir ) ; const includeDirectoryRegex = patterns . includeDirectoryPattern && new RegExp ( patterns . includeDirectoryPattern , regexFlag ) ; const excludeRegex = patterns . excludePattern && new RegExp ( patterns . excludePattern , regexFlag ) ; return { files : matchFiles ( rootDir , extensions , excludes || [ ] , includes , useCaseSensitiveFileNames , currentDir , depth , path => { const includeDir = dirPathMatches ( path ) ; const standardizedPath = fileSystemWrapper . getStandardizedAbsolutePath ( path ) ; if ( includeDir ) directories . push ( standardizedPath ) ; return getFileSystemEntries ( standardizedPath , fileSystemWrapper ) ; } , path => fileSystemWrapper . realpathSync ( fileSystemWrapper . getStandardizedAbsolutePath ( path ) ) , path => fileSystemWrapper . directoryExistsSync ( fileSystemWrapper . getStandardizedAbsolutePath ( path ) ) ) , directories , } ; function dirPathMatches ( absoluteName ) { if ( absoluteName [ absoluteName . length - 1 ] !== "/" ) absoluteName += "/" ; return ( ! includeDirectoryRegex || includeDirectoryRegex . test ( absoluteName ) ) && ( ! excludeRegex || ! excludeRegex . test ( absoluteName ) ) ; } } function getFileSystemEntries ( path , fileSystemWrapper ) { const files = [ ] ; const directories = [ ] ; try { const entries = fileSystemWrapper . readDirSync ( path ) ; for ( const entry of entries ) { if ( entry . isFile ) files . push ( entry . path ) ; else if ( entry . isDirectory ) directories . push ( entry . path ) ; } } catch ( err ) { if ( ! FileUtils . isNotExistsError ( err ) ) throw err ; } return { files , directories } ; }