1 | 'use strict';
|
---|
2 | const {promisify} = require('util');
|
---|
3 | const fs = require('fs');
|
---|
4 | const path = require('path');
|
---|
5 | const fastGlob = require('fast-glob');
|
---|
6 | const gitIgnore = require('ignore');
|
---|
7 | const slash = require('slash');
|
---|
8 |
|
---|
9 | const DEFAULT_IGNORE = [
|
---|
10 | '**/node_modules/**',
|
---|
11 | '**/flow-typed/**',
|
---|
12 | '**/coverage/**',
|
---|
13 | '**/.git'
|
---|
14 | ];
|
---|
15 |
|
---|
16 | const readFileP = promisify(fs.readFile);
|
---|
17 |
|
---|
18 | const mapGitIgnorePatternTo = base => ignore => {
|
---|
19 | if (ignore.startsWith('!')) {
|
---|
20 | return '!' + path.posix.join(base, ignore.slice(1));
|
---|
21 | }
|
---|
22 |
|
---|
23 | return path.posix.join(base, ignore);
|
---|
24 | };
|
---|
25 |
|
---|
26 | const parseGitIgnore = (content, options) => {
|
---|
27 | const base = slash(path.relative(options.cwd, path.dirname(options.fileName)));
|
---|
28 |
|
---|
29 | return content
|
---|
30 | .split(/\r?\n/)
|
---|
31 | .filter(Boolean)
|
---|
32 | .filter(line => !line.startsWith('#'))
|
---|
33 | .map(mapGitIgnorePatternTo(base));
|
---|
34 | };
|
---|
35 |
|
---|
36 | const reduceIgnore = files => {
|
---|
37 | const ignores = gitIgnore();
|
---|
38 | for (const file of files) {
|
---|
39 | ignores.add(parseGitIgnore(file.content, {
|
---|
40 | cwd: file.cwd,
|
---|
41 | fileName: file.filePath
|
---|
42 | }));
|
---|
43 | }
|
---|
44 |
|
---|
45 | return ignores;
|
---|
46 | };
|
---|
47 |
|
---|
48 | const ensureAbsolutePathForCwd = (cwd, p) => {
|
---|
49 | cwd = slash(cwd);
|
---|
50 | if (path.isAbsolute(p)) {
|
---|
51 | if (slash(p).startsWith(cwd)) {
|
---|
52 | return p;
|
---|
53 | }
|
---|
54 |
|
---|
55 | throw new Error(`Path ${p} is not in cwd ${cwd}`);
|
---|
56 | }
|
---|
57 |
|
---|
58 | return path.join(cwd, p);
|
---|
59 | };
|
---|
60 |
|
---|
61 | const getIsIgnoredPredecate = (ignores, cwd) => {
|
---|
62 | return p => ignores.ignores(slash(path.relative(cwd, ensureAbsolutePathForCwd(cwd, p.path || p))));
|
---|
63 | };
|
---|
64 |
|
---|
65 | const getFile = async (file, cwd) => {
|
---|
66 | const filePath = path.join(cwd, file);
|
---|
67 | const content = await readFileP(filePath, 'utf8');
|
---|
68 |
|
---|
69 | return {
|
---|
70 | cwd,
|
---|
71 | filePath,
|
---|
72 | content
|
---|
73 | };
|
---|
74 | };
|
---|
75 |
|
---|
76 | const getFileSync = (file, cwd) => {
|
---|
77 | const filePath = path.join(cwd, file);
|
---|
78 | const content = fs.readFileSync(filePath, 'utf8');
|
---|
79 |
|
---|
80 | return {
|
---|
81 | cwd,
|
---|
82 | filePath,
|
---|
83 | content
|
---|
84 | };
|
---|
85 | };
|
---|
86 |
|
---|
87 | const normalizeOptions = ({
|
---|
88 | ignore = [],
|
---|
89 | cwd = slash(process.cwd())
|
---|
90 | } = {}) => {
|
---|
91 | return {ignore, cwd};
|
---|
92 | };
|
---|
93 |
|
---|
94 | module.exports = async options => {
|
---|
95 | options = normalizeOptions(options);
|
---|
96 |
|
---|
97 | const paths = await fastGlob('**/.gitignore', {
|
---|
98 | ignore: DEFAULT_IGNORE.concat(options.ignore),
|
---|
99 | cwd: options.cwd
|
---|
100 | });
|
---|
101 |
|
---|
102 | const files = await Promise.all(paths.map(file => getFile(file, options.cwd)));
|
---|
103 | const ignores = reduceIgnore(files);
|
---|
104 |
|
---|
105 | return getIsIgnoredPredecate(ignores, options.cwd);
|
---|
106 | };
|
---|
107 |
|
---|
108 | module.exports.sync = options => {
|
---|
109 | options = normalizeOptions(options);
|
---|
110 |
|
---|
111 | const paths = fastGlob.sync('**/.gitignore', {
|
---|
112 | ignore: DEFAULT_IGNORE.concat(options.ignore),
|
---|
113 | cwd: options.cwd
|
---|
114 | });
|
---|
115 |
|
---|
116 | const files = paths.map(file => getFileSync(file, options.cwd));
|
---|
117 | const ignores = reduceIgnore(files);
|
---|
118 |
|
---|
119 | return getIsIgnoredPredecate(ignores, options.cwd);
|
---|
120 | };
|
---|