summaryrefslogtreecommitdiff
path: root/packages/core/src/utils/getFolderStructure.ts
diff options
context:
space:
mode:
authorAnas H. Sulaiman <[email protected]>2025-06-13 14:26:31 -0400
committerGitHub <[email protected]>2025-06-13 14:26:31 -0400
commit084b58a50edeaccc2261983fce2988e210637a0f (patch)
tree652a2b722f57f94357c6597ce24f48149ca9a601 /packages/core/src/utils/getFolderStructure.ts
parent1f63f3331f1d19e2157d87e8316828dc1c4c8b6b (diff)
reuse filtering service in getFolderStructure (#1016)
Diffstat (limited to 'packages/core/src/utils/getFolderStructure.ts')
-rw-r--r--packages/core/src/utils/getFolderStructure.ts40
1 files changed, 11 insertions, 29 deletions
diff --git a/packages/core/src/utils/getFolderStructure.ts b/packages/core/src/utils/getFolderStructure.ts
index 419a9769..7aee377b 100644
--- a/packages/core/src/utils/getFolderStructure.ts
+++ b/packages/core/src/utils/getFolderStructure.ts
@@ -8,8 +8,7 @@ import * as fs from 'fs/promises';
import { Dirent } from 'fs';
import * as path from 'path';
import { getErrorMessage, isNodeError } from './errors.js';
-import { GitIgnoreParser, GitIgnoreFilter } from './gitIgnoreParser.js';
-import { isGitRepository } from './gitUtils.js';
+import { FileDiscoveryService } from '../services/fileDiscoveryService.js';
const MAX_ITEMS = 200;
const TRUNCATION_INDICATOR = '...';
@@ -25,18 +24,16 @@ interface FolderStructureOptions {
ignoredFolders?: Set<string>;
/** Optional regex to filter included files by name. */
fileIncludePattern?: RegExp;
- /** Whether to respect .gitignore patterns. Defaults to true. */
- respectGitIgnore?: boolean;
- /** The root of the project, used for gitignore resolution. */
- projectRoot?: string;
+ /** For filtering files. */
+ fileService?: FileDiscoveryService;
}
// Define a type for the merged options where fileIncludePattern remains optional
type MergedFolderStructureOptions = Required<
- Omit<FolderStructureOptions, 'fileIncludePattern' | 'projectRoot'>
+ Omit<FolderStructureOptions, 'fileIncludePattern' | 'fileService'>
> & {
fileIncludePattern?: RegExp;
- projectRoot?: string;
+ fileService?: FileDiscoveryService;
};
/** Represents the full, unfiltered information about a folder and its contents. */
@@ -59,7 +56,6 @@ interface FullFolderInfo {
async function readFullStructure(
rootPath: string,
options: MergedFolderStructureOptions,
- gitIgnoreFilter: GitIgnoreFilter | null,
): Promise<FullFolderInfo | null> {
const rootName = path.basename(rootPath);
const rootNode: FullFolderInfo = {
@@ -128,8 +124,8 @@ async function readFullStructure(
}
const fileName = entry.name;
const filePath = path.join(currentPath, fileName);
- if (gitIgnoreFilter) {
- if (gitIgnoreFilter.isIgnored(filePath)) {
+ if (options.fileService) {
+ if (options.fileService.shouldIgnoreFile(filePath)) {
continue;
}
}
@@ -163,8 +159,8 @@ async function readFullStructure(
const subFolderPath = path.join(currentPath, subFolderName);
let isIgnoredByGit = false;
- if (gitIgnoreFilter) {
- if (gitIgnoreFilter.isIgnored(subFolderPath)) {
+ if (options?.fileService) {
+ if (options.fileService.shouldIgnoreFile(subFolderPath)) {
isIgnoredByGit = true;
}
}
@@ -296,26 +292,12 @@ export async function getFolderStructure(
maxItems: options?.maxItems ?? MAX_ITEMS,
ignoredFolders: options?.ignoredFolders ?? DEFAULT_IGNORED_FOLDERS,
fileIncludePattern: options?.fileIncludePattern,
- respectGitIgnore: options?.respectGitIgnore ?? true,
- projectRoot: options?.projectRoot ?? resolvedPath,
+ fileService: options?.fileService,
};
- let gitIgnoreFilter: GitIgnoreFilter | null = null;
- if (mergedOptions.respectGitIgnore && mergedOptions.projectRoot) {
- if (isGitRepository(mergedOptions.projectRoot)) {
- const parser = new GitIgnoreParser(mergedOptions.projectRoot);
- await parser.initialize();
- gitIgnoreFilter = parser;
- }
- }
-
try {
// 1. Read the structure using BFS, respecting maxItems
- const structureRoot = await readFullStructure(
- resolvedPath,
- mergedOptions,
- gitIgnoreFilter,
- );
+ const structureRoot = await readFullStructure(resolvedPath, mergedOptions);
if (!structureRoot) {
return `Error: Could not read directory "${resolvedPath}". Check path and permissions.`;