Elffuss BraidHub

kikocis/continue public

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