source: trip-planner-front/node_modules/globby/gitignore.js@ b738035

Last change on this file since b738035 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.5 KB
Line 
1'use strict';
2const {promisify} = require('util');
3const fs = require('fs');
4const path = require('path');
5const fastGlob = require('fast-glob');
6const gitIgnore = require('ignore');
7const slash = require('slash');
8
9const DEFAULT_IGNORE = [
10 '**/node_modules/**',
11 '**/flow-typed/**',
12 '**/coverage/**',
13 '**/.git'
14];
15
16const readFileP = promisify(fs.readFile);
17
18const 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
26const 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
36const 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
48const 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
61const getIsIgnoredPredecate = (ignores, cwd) => {
62 return p => ignores.ignores(slash(path.relative(cwd, ensureAbsolutePathForCwd(cwd, p.path || p))));
63};
64
65const 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
76const 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
87const normalizeOptions = ({
88 ignore = [],
89 cwd = slash(process.cwd())
90} = {}) => {
91 return {ignore, cwd};
92};
93
94module.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
108module.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};
Note: See TracBrowser for help on using the repository browser.