blob: 5329507e03785a06102ce3a45cee72f1f51104db (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { GitIgnoreParser, GitIgnoreFilter } from '../utils/gitIgnoreParser.js';
import { isGitRepository } from '../utils/gitUtils.js';
import * as path from 'path';
export interface FileDiscoveryOptions {
respectGitIgnore?: boolean;
includeBuildArtifacts?: boolean;
isGitRepo?: boolean;
}
export class FileDiscoveryService {
private gitIgnoreFilter: GitIgnoreFilter | null = null;
private projectRoot: string;
constructor(projectRoot: string) {
this.projectRoot = path.resolve(projectRoot);
}
async initialize(options: FileDiscoveryOptions = {}): Promise<void> {
const isGitRepo = options.isGitRepo ?? isGitRepository(this.projectRoot);
if (options.respectGitIgnore !== false && isGitRepo) {
const parser = new GitIgnoreParser(this.projectRoot);
await parser.initialize();
this.gitIgnoreFilter = parser;
}
}
/**
* Filters a list of file paths based on git ignore rules
*/
filterFiles(
filePaths: string[],
options: FileDiscoveryOptions = {},
): string[] {
return filePaths.filter((filePath) => {
// Always respect git ignore unless explicitly disabled
if (options.respectGitIgnore !== false && this.gitIgnoreFilter) {
if (this.gitIgnoreFilter.isIgnored(filePath)) {
return false;
}
}
return true;
});
}
/**
* Gets patterns that would be ignored for debugging/transparency
*/
getIgnoreInfo(): { gitIgnored: string[] } {
return {
gitIgnored: this.gitIgnoreFilter?.getIgnoredPatterns() || [],
};
}
/**
* Checks if a single file should be ignored
*/
shouldIgnoreFile(
filePath: string,
options: FileDiscoveryOptions = {},
): boolean {
const isGitRepo = options.isGitRepo ?? isGitRepository(this.projectRoot);
if (
options.respectGitIgnore !== false &&
isGitRepo &&
this.gitIgnoreFilter
) {
return this.gitIgnoreFilter.isIgnored(filePath);
}
return false;
}
/**
* Returns whether the project is a git repository
*/
isGitRepository(options: FileDiscoveryOptions = {}): boolean {
return options.isGitRepo ?? isGitRepository(this.projectRoot);
}
}
|