1- import { Tree } from "@angular-devkit/schematics" ;
1+ import { DirEntry , FileEntry , Tree } from "@angular-devkit/schematics" ;
22import { App , FS_TOKEN , FS_TYPE_TOKEN , FsTypes , IFileSystem } from "@igniteui/cli-core" ;
3+ import { minimatch } from 'minimatch' ;
4+ import * as path from "path" ;
35
46export class NgTreeFileSystem implements IFileSystem {
57 constructor ( private tree : Tree ) { }
68 public fileExists ( filePath : string ) : boolean {
79 return this . tree . exists ( filePath ) ;
810 }
911
10- public readFile ( filePath : string , encoding ?: string ) : string {
12+ public readFile ( filePath : string , _encoding ?: string ) : string {
1113 return ( this . tree . read ( filePath ) || "" ) . toString ( ) ;
1214 }
1315
@@ -23,24 +25,42 @@ export class NgTreeFileSystem implements IFileSystem {
2325 /**
2426 * Returns a list of file paths under a directory based on a match pattern
2527 * @param dirPath Root dir to search in
26- * @param pattern Supports only recursive wildcard ' \*\*\/\*'
27- * @param ignorePattern Optional pattern to ignore
28+ * @param pattern Supports only recursive wildcard ` \*\*\/\*`
29+ * @param ignorePatterns Optional patterns to ignore for each subdirectory
2830 */
29- public glob ( dirPath : string , pattern : string , ignorePattern ?: string ) : string [ ] {
31+ public glob ( dirPath : string , pattern : string , ignorePatterns ?: string [ ] ) : string [ ] {
3032 const dir = this . tree . getDir ( dirPath ) ;
3133 const entries : string [ ] = [ ] ;
32- pattern = pattern . split ( "**/*" ) . pop ( ) || pattern ;
3334
34- dir . visit ( ( _fullPath , entry ) => {
35- if ( ignorePattern && entry ?. path . includes ( ignorePattern ) ) {
36- return ;
37- }
38-
39- if ( entry ?. path . endsWith ( pattern ) ) {
35+ const visitor = ( _fullPath : string , entry ?: Readonly < FileEntry > ) : void => {
36+ if ( entry && minimatch ( entry . path , pattern ) ) {
4037 entries . push ( entry . path ) ;
4138 }
42- } ) ;
39+ } ;
40+
41+ if ( ignorePatterns ?. length ) {
42+ const recurse = ( dir : DirEntry ) : void => {
43+ for ( const subdirPath of dir . subdirs ) {
44+ if ( ignorePatterns . every ( p => ! minimatch ( subdirPath , p ) ) ) {
45+ const subDir = dir . dir ( subdirPath ) ;
46+ if ( subDir . subdirs . length ) {
47+ recurse ( subDir ) ;
48+ continue ;
49+ }
50+ for ( const file of dir . subfiles ) {
51+ if ( minimatch ( file , pattern ) && ignorePatterns . every ( p => ! minimatch ( file , p ) ) ) {
52+ entries . push ( path . posix . normalize ( `${ dir . path } /${ file } ` ) ) ;
53+ }
54+ }
55+ }
56+ }
57+ } ;
58+
59+ recurse ( dir ) ;
60+ return entries ;
61+ }
4362
63+ dir . visit ( visitor ) ;
4464 return entries ;
4565 }
4666}
0 commit comments