-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathconfig_ignore.ts
More file actions
66 lines (57 loc) · 2.96 KB
/
config_ignore.ts
File metadata and controls
66 lines (57 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import fastIgnore from "fast-ignore";
import fs from "node:fs/promises";
import path from "node:path";
import Known from "./known.js";
import { fastJoinedPath, fastRelativePath, isString, isUndefined, memoize, noop, someOf, zipObjectUnless } from "./utils.js";
import type { Ignore, PromiseMaybe } from "./types.js";
const getIgnoreContent = (folderPath: string, fileName: string): PromiseMaybe<string | undefined> => {
const filePath = fastJoinedPath(folderPath, fileName);
if (!Known.hasFilePath(filePath)) return;
return fs.readFile(filePath, "utf8").catch(noop);
};
const getIgnoresContent = memoize(async (folderPath: string, filesNames: string[]): Promise<string[] | undefined> => {
const contentsRaw = await Promise.all(filesNames.map((fileName) => getIgnoreContent(folderPath, fileName)));
const contents = contentsRaw.filter(isString);
if (!contents.length) return;
return contents;
});
const getIgnoresContentMap = async (foldersPaths: string[], filesNames: string[]): Promise<Partial<Record<string, string[]>>> => {
const contents = await Promise.all(foldersPaths.map((folderPath) => getIgnoresContent(folderPath, filesNames)));
const map = zipObjectUnless(foldersPaths, contents, isUndefined);
return map;
};
const getIgnoreBy = (folderPath: string, filesContents: string[]): Ignore => {
const ignore = fastIgnore(filesContents);
return (filePath: string): boolean => {
const fileRelativePath = fastRelativePath(folderPath, filePath);
return !!fileRelativePath && ignore(fileRelativePath);
};
};
const getIgnoreBys = (foldersPaths: string[], filesContents: string[][]): Ignore | undefined => {
if (!foldersPaths.length) return;
const ignores = foldersPaths.map((folderPath, index) => getIgnoreBy(folderPath, filesContents[index]));
const ignore = someOf(ignores);
return ignore;
};
const getIgnores = memoize(async (folderPath: string, filesNames: string[]): Promise<Ignore | undefined> => {
const contents = await getIgnoresContent(folderPath, filesNames);
if (!contents?.length) return;
const ignore = getIgnoreBy(folderPath, contents);
return ignore;
});
const getIgnoresUp = memoize(async (folderPath: string, filesNames: string[]): Promise<Ignore | undefined> => {
const ignore = await getIgnores(folderPath, filesNames);
const folderPathUp = path.dirname(folderPath);
const ignoreUp = folderPath !== folderPathUp ? await getIgnoresUp(folderPathUp, filesNames) : undefined;
const ignores = ignore ? (ignoreUp ? [ignore, ignoreUp] : [ignore]) : ignoreUp ? [ignoreUp] : [];
if (!ignores.length) return;
const ignoreAll = someOf(ignores);
return ignoreAll;
});
const getIgnoreResolved = async (filePath: string, filesNames: string[]): Promise<boolean> => {
const folderPath = path.dirname(filePath);
const ignore = await getIgnoresUp(folderPath, filesNames);
const ignored = !!ignore?.(filePath);
return ignored;
};
export { getIgnoresContent, getIgnoresContentMap, getIgnoreBy, getIgnoreBys, getIgnores, getIgnoresUp, getIgnoreResolved };