.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / stylelint / node_modules / globby / gitignore.js
1 'use strict';
2 const fs = require('fs');
3 const path = require('path');
4 const glob = require('glob');
5 const gitIgnore = require('ignore');
6 const pify = require('pify');
7 const slash = require('slash');
8
9 const globP = pify(glob);
10 const readFileP = pify(fs.readFile);
11
12 const mapGitIgnorePatternTo = base => ignore => {
13         if (ignore.startsWith('!')) {
14                 return '!' + path.posix.join(base, ignore.substr(1));
15         }
16
17         return path.posix.join(base, ignore);
18 };
19
20 const parseGitIgnore = (content, opts) => {
21         const base = slash(path.relative(opts.cwd, path.dirname(opts.fileName)));
22
23         return content
24                 .split(/\r?\n/)
25                 .filter(Boolean)
26                 .filter(l => l.charAt(0) !== '#')
27                 .map(mapGitIgnorePatternTo(base));
28 };
29
30 const reduceIgnore = files => {
31         return files.reduce((ignores, file) => {
32                 ignores.add(parseGitIgnore(file.content, {
33                         cwd: file.cwd,
34                         fileName: file.filePath
35                 }));
36                 return ignores;
37         }, gitIgnore());
38 };
39
40 const getIsIgnoredPredecate = (ignores, cwd) => {
41         return p => ignores.ignores(slash(path.relative(cwd, p)));
42 };
43
44 const getFile = (file, cwd) => {
45         const filePath = path.join(cwd, file);
46         return readFileP(filePath, 'utf8')
47                 .then(content => ({
48                         content,
49                         cwd,
50                         filePath
51                 }));
52 };
53
54 const getFileSync = (file, cwd) => {
55         const filePath = path.join(cwd, file);
56         const content = fs.readFileSync(filePath, 'utf8');
57
58         return {
59                 content,
60                 cwd,
61                 filePath
62         };
63 };
64
65 const normalizeOpts = opts => {
66         opts = opts || {};
67         const ignore = opts.ignore || [];
68         const cwd = opts.cwd || process.cwd();
69         return {ignore, cwd};
70 };
71
72 module.exports = o => {
73         const opts = normalizeOpts(o);
74
75         return globP('**/.gitignore', {ignore: opts.ignore, cwd: opts.cwd})
76                 .then(paths => Promise.all(paths.map(file => getFile(file, opts.cwd))))
77                 .then(files => reduceIgnore(files))
78                 .then(ignores => getIsIgnoredPredecate(ignores, opts.cwd));
79 };
80
81 module.exports.sync = o => {
82         const opts = normalizeOpts(o);
83
84         const paths = glob.sync('**/.gitignore', {ignore: opts.ignore, cwd: opts.cwd});
85         const files = paths.map(file => getFileSync(file, opts.cwd));
86         const ignores = reduceIgnore(files);
87         return getIsIgnoredPredecate(ignores, opts.cwd);
88 };