Skip to content

Commit df6b766

Browse files
isabellahochstainless-app[bot]
authored andcommitted
clean up helpers + glob logic (#11)
* simplify file glob logic * linting
1 parent a3789f6 commit df6b766

File tree

1 file changed

+45
-31
lines changed

1 file changed

+45
-31
lines changed

src/lib/helpers.ts

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -31,47 +31,61 @@ const DEFAULT_IGNORE = [
3131
'**/.env*',
3232
];
3333

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[] = [];
4036

4137
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 });
5239

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);
5443

55-
const files: FileData[] = [];
44+
if (ignore.some((ignorePattern) => minimatch(relativePath, ignorePattern))) {
45+
continue;
46+
}
5647

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);
6553
}
66-
} catch {
67-
continue;
6854
}
6955
}
56+
} catch {
57+
// Skip directories we can't read
58+
}
59+
60+
return files;
61+
}
7062

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+
}
7486
}
87+
88+
return files;
7589
}
7690

7791
export function applyChanges({

0 commit comments

Comments
 (0)