diff --git a/package-lock.json b/package-lock.json index feff653a..f0b3942d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -54,6 +54,7 @@ "decimal.js": "^10.6.0", "embla-carousel-react": "^8.3.0", "esbuild-wasm": "^0.25.8", + "ignore": "^7.0.5", "input-otp": "^1.2.4", "isomorphic-git": "^1.32.1", "jszip": "^3.10.1", @@ -888,6 +889,16 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/@eslint/eslintrc/node_modules/ignore": { + "version": "5.3.2", + "resolved": "https://registry.npmjs.org/ignore/-/ignore-5.3.2.tgz", + "integrity": "sha512-hsBTNUqQTDwkWtcdYI2i06Y/nUBEsNEDJKjWdigLvegy8kDuJAS8uRlpkkcQpyEXL0Z/pjDy5HBmMjRCJ2gq+g==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">= 4" + } + }, "node_modules/@eslint/js": { "version": "9.31.0", "resolved": "https://registry.npmjs.org/@eslint/js/-/js-9.31.0.tgz", @@ -3879,6 +3890,16 @@ "typescript": ">=4.8.4 <5.9.0" } }, + "node_modules/@typescript-eslint/eslint-plugin/node_modules/ignore": { + "version": "5.3.2", + "resolved": "https://registry.npmjs.org/ignore/-/ignore-5.3.2.tgz", + "integrity": "sha512-hsBTNUqQTDwkWtcdYI2i06Y/nUBEsNEDJKjWdigLvegy8kDuJAS8uRlpkkcQpyEXL0Z/pjDy5HBmMjRCJ2gq+g==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">= 4" + } + }, "node_modules/@typescript-eslint/parser": { "version": "8.31.1", "resolved": "https://registry.npmjs.org/@typescript-eslint/parser/-/parser-8.31.1.tgz", @@ -5662,6 +5683,16 @@ "url": "https://opencollective.com/eslint" } }, + "node_modules/eslint/node_modules/ignore": { + "version": "5.3.2", + "resolved": "https://registry.npmjs.org/ignore/-/ignore-5.3.2.tgz", + "integrity": "sha512-hsBTNUqQTDwkWtcdYI2i06Y/nUBEsNEDJKjWdigLvegy8kDuJAS8uRlpkkcQpyEXL0Z/pjDy5HBmMjRCJ2gq+g==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">= 4" + } + }, "node_modules/espree": { "version": "10.4.0", "resolved": "https://registry.npmjs.org/espree/-/espree-10.4.0.tgz", @@ -6795,9 +6826,9 @@ "license": "BSD-3-Clause" }, "node_modules/ignore": { - "version": "5.3.2", - "resolved": "https://registry.npmjs.org/ignore/-/ignore-5.3.2.tgz", - "integrity": "sha512-hsBTNUqQTDwkWtcdYI2i06Y/nUBEsNEDJKjWdigLvegy8kDuJAS8uRlpkkcQpyEXL0Z/pjDy5HBmMjRCJ2gq+g==", + "version": "7.0.5", + "resolved": "https://registry.npmjs.org/ignore/-/ignore-7.0.5.tgz", + "integrity": "sha512-Hs59xBNfUIunMFgWAbGX5cq6893IbWg4KnrjbYwX3tx0ztorVgTDA6B2sxf8ejHJ4wz8BqGUMYlnzNBer5NvGg==", "license": "MIT", "engines": { "node": ">= 4" @@ -7071,6 +7102,15 @@ "node": ">=14.17" } }, + "node_modules/isomorphic-git/node_modules/ignore": { + "version": "5.3.2", + "resolved": "https://registry.npmjs.org/ignore/-/ignore-5.3.2.tgz", + "integrity": "sha512-hsBTNUqQTDwkWtcdYI2i06Y/nUBEsNEDJKjWdigLvegy8kDuJAS8uRlpkkcQpyEXL0Z/pjDy5HBmMjRCJ2gq+g==", + "license": "MIT", + "engines": { + "node": ">= 4" + } + }, "node_modules/isomorphic-git/node_modules/pify": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/pify/-/pify-4.0.1.tgz", diff --git a/package.json b/package.json index 5a404f84..c5e8da68 100644 --- a/package.json +++ b/package.json @@ -56,6 +56,7 @@ "decimal.js": "^10.6.0", "embla-carousel-react": "^8.3.0", "esbuild-wasm": "^0.25.8", + "ignore": "^7.0.5", "input-otp": "^1.2.4", "isomorphic-git": "^1.32.1", "jszip": "^3.10.1", diff --git a/src/components/Shakespeare/FileTree.tsx b/src/components/Shakespeare/FileTree.tsx index 20a3a44e..f546fcfb 100644 --- a/src/components/Shakespeare/FileTree.tsx +++ b/src/components/Shakespeare/FileTree.tsx @@ -4,6 +4,7 @@ import { useFS } from '@/hooks/useFS'; import { useGitStatus } from '@/hooks/useGitStatus'; import { ChevronRight, ChevronDown, File, Folder } from 'lucide-react'; import { cn } from '@/lib/utils'; +import { createGitignoreFilter, normalizePathForGitignore } from '@/lib/gitignore'; interface FileTreeProps { projectId: string; @@ -22,6 +23,10 @@ interface FileNode { export function FileTree({ projectId, onFileSelect, selectedFile }: FileTreeProps) { const [tree, setTree] = useState([]); const [isLoading, setIsLoading] = useState(true); + const [gitignoreFilter, setGitignoreFilter] = useState<{ + isIgnored: (path: string) => boolean; + shouldShow: (path: string) => boolean; + } | null>(null); const { fs } = useFS(); const projectsManager = useProjectsManager(); const { data: gitStatus } = useGitStatus(projectId); @@ -115,6 +120,11 @@ export function FileTree({ projectId, onFileSelect, selectedFile }: FileTreeProp const loadFileTree = useCallback(async () => { setIsLoading(true); try { + // Load gitignore filter + const projectPath = `${projectsManager['dir']}/${projectId}`; + const filter = await createGitignoreFilter(fs, projectPath); + setGitignoreFilter(filter); + const structure = await buildFileTree(projectId, ''); setTree(structure); } catch (_error) { @@ -122,7 +132,7 @@ export function FileTree({ projectId, onFileSelect, selectedFile }: FileTreeProp } finally { setIsLoading(false); } - }, [projectId, buildFileTree]); + }, [projectId, buildFileTree, fs, projectsManager]); useEffect(() => { loadFileTree(); @@ -160,9 +170,16 @@ export function FileTree({ projectId, onFileSelect, selectedFile }: FileTreeProp const gitDirStatus = node.type === 'directory' ? getDirectoryGitStatus(node.path, node.children || []) : null; const gitStatus = gitFileStatus || gitDirStatus; + // Check if this file/directory is gitignored + const normalizedPath = normalizePathForGitignore(node.path); + const isGitignored = gitignoreFilter?.isIgnored(normalizedPath) ?? false; + // Get styling classes for git status const gitStatusClasses = getGitStatusClasses(gitStatus); + // Apply muted styling for gitignored files + const gitignoreClasses = isGitignored ? 'text-muted-foreground' : ''; + return (
{node.isOpen ? ( - + ) : ( - + )} - + ) : ( <>
{/* Spacer for alignment */} - + )} - {node.name} + {node.name}
{node.type === 'directory' && node.isOpen && node.children && ( diff --git a/src/lib/gitignore.ts b/src/lib/gitignore.ts new file mode 100644 index 00000000..6137819b --- /dev/null +++ b/src/lib/gitignore.ts @@ -0,0 +1,41 @@ +import ignore from 'ignore'; +import type { JSRuntimeFS } from './JSRuntime'; + +/** + * Creates a gitignore filter function that can check if files should be ignored + */ +export async function createGitignoreFilter( + fs: JSRuntimeFS, + projectPath: string +): Promise<{ + isIgnored: (path: string) => boolean; + shouldShow: (path: string) => boolean; +}> { + const ig = ignore(); + + // Always ignore .git directory + ig.add('.git'); + + try { + // Try to read .gitignore file from the project root + const gitignorePath = `${projectPath}/.gitignore`; + const gitignoreContent = await fs.readFile(gitignorePath, 'utf8'); + ig.add(gitignoreContent); + } catch { + // If there's an error reading .gitignore, continue without it + // We'll still show all files, just won't have gitignore patterns + } + + return { + isIgnored: (path: string) => ig.ignores(path), + shouldShow: (path: string) => !ig.ignores(path) || path === '.gitignore', // Always show .gitignore itself + }; +} + +/** + * Normalizes a file path for gitignore checking by removing leading slashes + */ +export function normalizePathForGitignore(path: string): string { + // Remove leading slash if present + return path.startsWith('/') ? path.slice(1) : path; +} \ No newline at end of file