/
gitignore.js
100 lines (78 loc) · 2.32 KB
/
gitignore.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
import process from 'node:process';
import fs from 'node:fs';
import path from 'node:path';
import fastGlob from 'fast-glob';
import gitIgnore from 'ignore';
import slash from 'slash';
import {toPath} from './utilities.js';
const gitignoreGlobOptions = {
ignore: [
'**/node_modules',
'**/flow-typed',
'**/coverage',
'**/.git',
],
absolute: true,
};
const mapGitIgnorePatternTo = base => ignore => {
if (ignore.startsWith('!')) {
return '!' + path.posix.join(base, ignore.slice(1));
}
return path.posix.join(base, ignore);
};
const parseGitIgnore = (content, options) => {
const base = slash(path.relative(options.cwd, path.dirname(options.fileName)));
return content
.split(/\r?\n/)
.filter(Boolean)
.filter(line => !line.startsWith('#'))
.map(mapGitIgnorePatternTo(base));
};
const reduceIgnore = files => {
const ignores = gitIgnore();
for (const file of files) {
ignores.add(parseGitIgnore(file.content, {
cwd: file.cwd,
fileName: file.filePath,
}));
}
return ignores;
};
const ensureAbsolutePathForCwd = (cwd, p) => {
cwd = slash(cwd);
if (path.isAbsolute(p)) {
if (slash(p).startsWith(cwd)) {
return p;
}
throw new Error(`Path ${p} is not in cwd ${cwd}`);
}
return path.join(cwd, p);
};
const getIsIgnoredPredicate = (ignores, cwd) => p => ignores.ignores(slash(path.relative(cwd, ensureAbsolutePathForCwd(cwd, toPath(p)))));
const getFile = async (filePath, cwd) => ({
cwd,
filePath,
content: await fs.promises.readFile(filePath, 'utf8'),
});
const getFileSync = (filePath, cwd) => ({
cwd,
filePath,
content: fs.readFileSync(filePath, 'utf8'),
});
const normalizeOptions = (options = {}) => ({
cwd: toPath(options.cwd) || slash(process.cwd()),
});
export const isGitIgnored = async options => {
const {cwd} = normalizeOptions(options);
const paths = await fastGlob('**/.gitignore', {cwd, ...gitignoreGlobOptions});
const files = await Promise.all(paths.map(file => getFile(file, cwd)));
const ignores = reduceIgnore(files);
return getIsIgnoredPredicate(ignores, cwd);
};
export const isGitIgnoredSync = options => {
const {cwd} = normalizeOptions(options);
const paths = fastGlob.sync('**/.gitignore', {cwd, ...gitignoreGlobOptions});
const files = paths.map(file => getFileSync(file, cwd));
const ignores = reduceIgnore(files);
return getIsIgnoredPredicate(ignores, cwd);
};