-
-
Notifications
You must be signed in to change notification settings - Fork 130
/
ignore.js
109 lines (89 loc) · 2.94 KB
/
ignore.js
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import process from 'node:process';
import fs from 'node:fs';
import fsPromises from 'node:fs/promises';
import path from 'node:path';
import fastGlob from 'fast-glob';
import gitIgnore from 'ignore';
import slash from 'slash';
import {toPath} from 'unicorn-magic';
import {isNegativePattern} from './utilities.js';
const defaultIgnoredDirectories = [
'**/node_modules',
'**/flow-typed',
'**/coverage',
'**/.git',
];
const ignoreFilesGlobOptions = {
absolute: true,
dot: true,
};
export const GITIGNORE_FILES_PATTERN = '**/.gitignore';
const applyBaseToPattern = (pattern, base) => isNegativePattern(pattern)
? '!' + path.posix.join(base, pattern.slice(1))
: path.posix.join(base, pattern);
const parseIgnoreFile = (file, cwd) => {
const base = slash(path.relative(cwd, path.dirname(file.filePath)));
return file.content
.split(/\r?\n/)
.filter(line => line && !line.startsWith('#'))
.map(pattern => applyBaseToPattern(pattern, base));
};
const toRelativePath = (fileOrDirectory, cwd) => {
cwd = slash(cwd);
if (path.isAbsolute(fileOrDirectory)) {
if (slash(fileOrDirectory).startsWith(cwd)) {
return path.relative(cwd, fileOrDirectory);
}
throw new Error(`Path ${fileOrDirectory} is not in cwd ${cwd}`);
}
return fileOrDirectory;
};
const getIsIgnoredPredicate = (files, cwd) => {
const patterns = files.flatMap(file => parseIgnoreFile(file, cwd));
const ignores = gitIgnore().add(patterns);
return fileOrDirectory => {
fileOrDirectory = toPath(fileOrDirectory);
fileOrDirectory = toRelativePath(fileOrDirectory, cwd);
return fileOrDirectory ? ignores.ignores(slash(fileOrDirectory)) : false;
};
};
const normalizeOptions = (options = {}) => ({
cwd: toPath(options.cwd) ?? process.cwd(),
suppressErrors: Boolean(options.suppressErrors),
deep: typeof options.deep === 'number' ? options.deep : Number.POSITIVE_INFINITY,
ignore: [...options.ignore ?? [], ...defaultIgnoredDirectories],
});
export const isIgnoredByIgnoreFiles = async (patterns, options) => {
const {cwd, suppressErrors, deep, ignore} = normalizeOptions(options);
const paths = await fastGlob(patterns, {
cwd,
suppressErrors,
deep,
ignore,
...ignoreFilesGlobOptions,
});
const files = await Promise.all(
paths.map(async filePath => ({
filePath,
content: await fsPromises.readFile(filePath, 'utf8'),
})),
);
return getIsIgnoredPredicate(files, cwd);
};
export const isIgnoredByIgnoreFilesSync = (patterns, options) => {
const {cwd, suppressErrors, deep, ignore} = normalizeOptions(options);
const paths = fastGlob.sync(patterns, {
cwd,
suppressErrors,
deep,
ignore,
...ignoreFilesGlobOptions,
});
const files = paths.map(filePath => ({
filePath,
content: fs.readFileSync(filePath, 'utf8'),
}));
return getIsIgnoredPredicate(files, cwd);
};
export const isGitIgnored = options => isIgnoredByIgnoreFiles(GITIGNORE_FILES_PATTERN, options);
export const isGitIgnoredSync = options => isIgnoredByIgnoreFilesSync(GITIGNORE_FILES_PATTERN, options);