@@ -31,47 +31,61 @@ const DEFAULT_IGNORE = [
31
31
'**/.env*' ,
32
32
] ;
33
33
34
- export async function collectFiles ( {
35
- basePath = process . cwd ( ) ,
36
- patterns = DEFAULT_PATTERNS ,
37
- ignore = DEFAULT_IGNORE ,
38
- } : { basePath ?: string ; patterns ?: string [ ] ; ignore ?: string [ ] } = { } ) : Promise < FileData [ ] > {
39
- const originalCwd = process . cwd ( ) ;
34
+ function walkDirectory ( dir : string , basePath : string , patterns : string [ ] , ignore : string [ ] ) : string [ ] {
35
+ const files : string [ ] = [ ] ;
40
36
41
37
try {
42
- process . chdir ( basePath ) ;
43
-
44
- const allFilePaths : string [ ] = [ ] ;
45
- for ( const pattern of patterns ) {
46
- const globResult = glob . sync ( pattern , { nodir : true } ) ;
47
- const filtered = globResult . filter (
48
- ( match : string ) => ! ignore . some ( ( ignorePattern ) => minimatch ( match , ignorePattern ) ) ,
49
- ) ;
50
- allFilePaths . push ( ...filtered ) ;
51
- }
38
+ const entries = fs . readdirSync ( dir , { withFileTypes : true } ) ;
52
39
53
- const filePaths = [ ...new Set ( allFilePaths ) ] ;
40
+ for ( const entry of entries ) {
41
+ const fullPath = path . join ( dir , entry . name ) ;
42
+ const relativePath = path . relative ( basePath , fullPath ) ;
54
43
55
- const files : FileData [ ] = [ ] ;
44
+ if ( ignore . some ( ( ignorePattern ) => minimatch ( relativePath , ignorePattern ) ) ) {
45
+ continue ;
46
+ }
56
47
57
- for ( const filePath of filePaths ) {
58
- try {
59
- const contents = fs . readFileSync ( path . resolve ( basePath , filePath ) , 'utf8' ) ;
60
- if ( contents . trim ( ) ) {
61
- files . push ( {
62
- path : filePath ,
63
- contents,
64
- } ) ;
48
+ if ( entry . isDirectory ( ) ) {
49
+ files . push ( ...walkDirectory ( fullPath , basePath , patterns , ignore ) ) ;
50
+ } else if ( entry . isFile ( ) ) {
51
+ if ( patterns . some ( ( pattern ) => minimatch ( relativePath , pattern ) ) ) {
52
+ files . push ( relativePath ) ;
65
53
}
66
- } catch {
67
- continue ;
68
54
}
69
55
}
56
+ } catch {
57
+ // Skip directories we can't read
58
+ }
59
+
60
+ return files ;
61
+ }
70
62
71
- return files ;
72
- } finally {
73
- process . chdir ( originalCwd ) ;
63
+ export async function collectFiles ( {
64
+ basePath = process . cwd ( ) ,
65
+ patterns = DEFAULT_PATTERNS ,
66
+ ignore = DEFAULT_IGNORE ,
67
+ } : { basePath ?: string ; patterns ?: string [ ] ; ignore ?: string [ ] } = { } ) : Promise < FileData [ ] > {
68
+ const resolvedBasePath = path . resolve ( basePath ) ;
69
+ const filePaths = walkDirectory ( resolvedBasePath , resolvedBasePath , patterns , ignore ) ;
70
+
71
+ const files : FileData [ ] = [ ] ;
72
+
73
+ for ( const filePath of filePaths ) {
74
+ try {
75
+ const fullPath = path . join ( resolvedBasePath , filePath ) ;
76
+ const contents = fs . readFileSync ( fullPath , 'utf8' ) ;
77
+ if ( contents . trim ( ) ) {
78
+ files . push ( {
79
+ path : filePath ,
80
+ contents,
81
+ } ) ;
82
+ }
83
+ } catch {
84
+ continue ;
85
+ }
74
86
}
87
+
88
+ return files ;
75
89
}
76
90
77
91
export function applyChanges ( {
0 commit comments