diff options
| author | Anas H. Sulaiman <[email protected]> | 2025-06-14 10:25:34 -0400 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-06-14 14:25:34 +0000 |
| commit | 4873fce7919b4d74cee183a91fa8a3af58aef993 (patch) | |
| tree | c08502c1e4592667160cb006528f868fd6283294 /packages/core/src/utils/gitIgnoreParser.ts | |
| parent | e6d54771686b3f9537a5a05c9f9101afad3ffdcd (diff) | |
centralize file filtering in `FileDiscoveryService` (#1039)
Diffstat (limited to 'packages/core/src/utils/gitIgnoreParser.ts')
| -rw-r--r-- | packages/core/src/utils/gitIgnoreParser.ts | 39 |
1 files changed, 17 insertions, 22 deletions
diff --git a/packages/core/src/utils/gitIgnoreParser.ts b/packages/core/src/utils/gitIgnoreParser.ts index 16367a4a..69797282 100644 --- a/packages/core/src/utils/gitIgnoreParser.ts +++ b/packages/core/src/utils/gitIgnoreParser.ts @@ -4,18 +4,18 @@ * SPDX-License-Identifier: Apache-2.0 */ -import * as fs from 'fs/promises'; +import * as fs from 'fs'; import * as path from 'path'; import ignore, { type Ignore } from 'ignore'; import { isGitRepository } from './gitUtils.js'; export interface GitIgnoreFilter { isIgnored(filePath: string): boolean; + getPatterns(): string[]; } export class GitIgnoreParser implements GitIgnoreFilter { private projectRoot: string; - private isGitRepo: boolean = false; private ig: Ignore = ignore(); private patterns: string[] = []; @@ -23,33 +23,28 @@ export class GitIgnoreParser implements GitIgnoreFilter { this.projectRoot = path.resolve(projectRoot); } - async initialize(patternsFileName?: string): Promise<void> { - const patternFiles = []; - if (patternsFileName && patternsFileName !== '') { - patternFiles.push(patternsFileName); - } + loadGitRepoPatterns(): void { + if (!isGitRepository(this.projectRoot)) return; - this.isGitRepo = isGitRepository(this.projectRoot); - if (this.isGitRepo) { - patternFiles.push('.gitignore'); - patternFiles.push(path.join('.git', 'info', 'exclude')); + // Always ignore .git directory regardless of .gitignore content + this.addPatterns(['.git']); - // Always ignore .git directory regardless of .gitignore content - this.addPatterns(['.git']); - } + const patternFiles = ['.gitignore', path.join('.git', 'info', 'exclude')]; for (const pf of patternFiles) { - try { - await this.loadPatterns(pf); - } catch (_error) { - // File doesn't exist or can't be read, continue silently - } + this.loadPatterns(pf); } } - async loadPatterns(patternsFileName: string): Promise<void> { + loadPatterns(patternsFileName: string): void { const patternsFilePath = path.join(this.projectRoot, patternsFileName); - const content = await fs.readFile(patternsFilePath, 'utf-8'); - const patterns = content + let content: string; + try { + content = fs.readFileSync(patternsFilePath, 'utf-8'); + } catch (_error) { + // ignore file not found + return; + } + const patterns = (content ?? '') .split('\n') .map((p) => p.trim()) .filter((p) => p !== '' && !p.startsWith('#')); |
