1- import fs from 'fs'
21import path from 'path'
32
43import * as ignore from 'ignore'
@@ -7,14 +6,20 @@ import { sortBy } from 'lodash'
76import { DEFAULT_IGNORED_PATHS } from './old-constants'
87import { isValidProjectRoot } from './util/file'
98
9+ import type { CodebuffFileSystem } from './types/filesystem'
1010import type { DirectoryNode , FileTreeNode } from './util/file'
1111
1212export const DEFAULT_MAX_FILES = 10_000
1313
14- export function getProjectFileTree (
15- projectRoot : string ,
16- { maxFiles = DEFAULT_MAX_FILES } : { maxFiles ?: number } = { } ,
17- ) : FileTreeNode [ ] {
14+ export function getProjectFileTree ( params : {
15+ projectRoot : string
16+ maxFiles ?: number
17+ fs : CodebuffFileSystem
18+ } ) : FileTreeNode [ ] {
19+ const withDefaults = { maxFiles : DEFAULT_MAX_FILES , ...params }
20+ const { projectRoot, fs } = withDefaults
21+ let { maxFiles } = withDefaults
22+
1823 const start = Date . now ( )
1924 const defaultIgnore = ignore . default ( )
2025 for ( const pattern of DEFAULT_IGNORED_PATHS ) {
@@ -50,7 +55,7 @@ export function getProjectFileTree(
5055 const mergedIgnore = ignore
5156 . default ( )
5257 . add ( currentIgnore )
53- . add ( parseGitignore ( fullPath , projectRoot ) )
58+ . add ( parseGitignore ( { fullDirPath : fullPath , projectRoot, fs } ) )
5459
5560 try {
5661 const files = fs . readdirSync ( fullPath )
@@ -146,10 +151,13 @@ function rebaseGitignorePattern(
146151 return isNegated ? `!${ rebased } ` : rebased
147152}
148153
149- export function parseGitignore (
150- fullDirPath : string ,
151- projectRoot : string ,
152- ) : ignore . Ignore {
154+ export function parseGitignore ( params : {
155+ fullDirPath : string
156+ projectRoot : string
157+ fs : CodebuffFileSystem
158+ } ) : ignore . Ignore {
159+ const { fullDirPath, projectRoot, fs } = params
160+
153161 const ig = ignore . default ( )
154162 const relativeDirPath = path . relative ( projectRoot , fullDirPath )
155163 const ignoreFiles = [
@@ -210,7 +218,13 @@ export function getLastReadFilePaths(
210218 . map ( ( node ) => node . filePath )
211219}
212220
213- export function isFileIgnored ( filePath : string , projectRoot : string ) : boolean {
221+ export function isFileIgnored ( params : {
222+ filePath : string
223+ projectRoot : string
224+ fs : CodebuffFileSystem
225+ } ) : boolean {
226+ const { filePath, projectRoot, fs } = params
227+
214228 const defaultIgnore = ignore . default ( )
215229 for ( const pattern of DEFAULT_IGNORED_PATHS ) {
216230 defaultIgnore . add ( pattern )
@@ -226,7 +240,9 @@ export function isFileIgnored(filePath: string, projectRoot: string): boolean {
226240 const mergedIgnore = ignore . default ( ) . add ( defaultIgnore )
227241 let currentDir = dirPath
228242 while ( currentDir . startsWith ( projectRoot ) ) {
229- mergedIgnore . add ( parseGitignore ( currentDir , projectRoot ) )
243+ mergedIgnore . add (
244+ parseGitignore ( { fullDirPath : currentDir , projectRoot, fs } ) ,
245+ )
230246 currentDir = path . dirname ( currentDir )
231247 }
232248
0 commit comments