Elffuss BraidHub

kikocis/continue public

b334cb9dcd5e · 1.9 KiBRawPermalink
1import ignore from "ignore";
2import type { FileType, IDE } from "../";
3import { findUriInDirs, getUriPathBasename } from "../util/uri";
4import { defaultIgnoreFileAndDir, getGlobalContinueIgArray } from "./ignore";
5import { 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*/
14export 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}