| 1 | import ignore from "ignore"; |
| 2 | import type { FileType, IDE } from "../"; |
| 3 | import { findUriInDirs, getUriPathBasename } from "../util/uri"; |
| 4 | import { defaultIgnoreFileAndDir, getGlobalContinueIgArray } from "./ignore"; |
| 5 | import { getIgnoreContext } from "./walkDir"; |
| 6 | |
| 7 | /* |
| 8 | Process: |
| 9 | 1. Check global/default ignores |
| 10 | 2. Walk UP tree from file, checking ignores at each level |
| 11 | |
| 12 | TODO there might be issues with symlinks here |
| 13 | */ |
| 14 | export async function shouldIgnore( |
| 15 | fileUri: string, |
| 16 | ide: IDE, |
| 17 | rootDirCandidates?: string[], |
| 18 | ): Promise<boolean> { |
| 19 | const rootDirUris = rootDirCandidates ?? (await ide.getWorkspaceDirs()); |
| 20 | const { foundInDir: rootDir, uri } = findUriInDirs(fileUri, rootDirUris); |
| 21 | |
| 22 | if (!rootDir) { |
| 23 | return true; |
| 24 | } |
| 25 | |
| 26 | const defaultAndGlobalIgnores = ignore() |
| 27 | .add(defaultIgnoreFileAndDir) |
| 28 | .add(getGlobalContinueIgArray()); |
| 29 | |
| 30 | let currentDir = uri; |
| 31 | let directParent = true; |
| 32 | let fileType = 1 as FileType.File as FileType; |
| 33 | while (currentDir !== rootDir) { |
| 34 | // Go to parent dir of file |
| 35 | const splitUri = currentDir.split("/"); |
| 36 | splitUri.pop(); |
| 37 | currentDir = splitUri.join("/"); |
| 38 | |
| 39 | // Get all files in the dir |
| 40 | const dirEntries = await ide.listDir(currentDir); |
| 41 | |
| 42 | // Check if the file is a symbolic link, ignore if so |
| 43 | if (directParent) { |
| 44 | directParent = false; |
| 45 | const baseName = getUriPathBasename(fileUri); |
| 46 | const entry = dirEntries.find(([name, _]) => name === baseName); |
| 47 | if (entry) { |
| 48 | fileType = entry[1]; |
| 49 | if (fileType === (64 as FileType.SymbolicLink)) { |
| 50 | return true; |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | const ignoreContext = await getIgnoreContext( |
| 56 | currentDir, |
| 57 | dirEntries, |
| 58 | ide, |
| 59 | defaultAndGlobalIgnores, |
| 60 | ); |
| 61 | |
| 62 | let relativePath = uri.substring(currentDir.length + 1); |
| 63 | if (fileType === (2 as FileType.Directory)) { |
| 64 | relativePath += "/"; |
| 65 | } |
| 66 | |
| 67 | if (ignoreContext.ignores(relativePath)) { |
| 68 | return true; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | return false; |
| 73 | } |