blob: f117813da73593099424eae99ebb4fe6b75fa386 (
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
88
89
90
|
/**
* @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';
import fg from 'fast-glob';
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;
}
}
async glob(
pattern: string | string[],
options: fg.Options = {},
): Promise<string[]> {
const files = await fg(pattern, {
...options,
caseSensitiveMatch: false,
});
return this.filterFiles(files);
}
/**
* 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;
});
}
/**
* 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);
}
}
|