import { readdirSync, statSync } from "node:fs"; import path from "node:path"; import type { ContentProvider } from "./types.js"; import { createFileProvider } from "./default-provider.js"; const SUPPORTED_EXTENSIONS = new Set([ "md", "mdx", "markdown", "csv", "tsv", "diff", "patch", "png", "jpg", "jpeg", "gif", "webp", "ts", "tsx", "js", "jsx", "json", "txt", "css", "html", "yaml", "yml", "toml", "sh", "bash", "zsh", "go", "rs", "py", "rb", "c", "cpp", "h", "hpp", "java", "kt", "swift", "sql", ]); export interface DirectoryEntry { name: string; path: string; } export const listDirectoryFiles = function listDirectoryFiles(dirPath: string): DirectoryEntry[] { const entries = readdirSync(dirPath); const files: DirectoryEntry[] = []; for (const entry of entries) { if (entry.startsWith(".")) { continue; } const fullPath = path.join(dirPath, entry); try { const stat = statSync(fullPath); if (!stat.isFile()) { continue; } } catch { continue; } const ext = entry.split(".").pop()?.toLowerCase(); if (ext === undefined || ext === "" || !SUPPORTED_EXTENSIONS.has(ext)) { continue; } files.push({ name: entry, path: fullPath }); } files.sort((a, b) => a.name.localeCompare(b.name)); return files; }; export const createDirectoryFileProvider = function createDirectoryFileProvider( filePath: string, ): ContentProvider { return createFileProvider(filePath); };