.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / stylelint / node_modules / globby / index.js
1 'use strict';
2 const arrayUnion = require('array-union');
3 const glob = require('glob');
4 const pify = require('pify');
5 const dirGlob = require('dir-glob');
6 const gitignore = require('./gitignore');
7
8 const globP = pify(glob);
9 const DEFAULT_FILTER = () => false;
10
11 const isNegative = pattern => pattern[0] === '!';
12
13 const assertPatternsInput = patterns => {
14         if (!patterns.every(x => typeof x === 'string')) {
15                 throw new TypeError('Patterns must be a string or an array of strings');
16         }
17 };
18
19 const generateGlobTasks = (patterns, taskOpts) => {
20         patterns = [].concat(patterns);
21         assertPatternsInput(patterns);
22
23         const globTasks = [];
24
25         taskOpts = Object.assign({
26                 cache: Object.create(null),
27                 statCache: Object.create(null),
28                 realpathCache: Object.create(null),
29                 symlinks: Object.create(null),
30                 ignore: [],
31                 expandDirectories: true,
32                 nodir: true
33         }, taskOpts);
34
35         patterns.forEach((pattern, i) => {
36                 if (isNegative(pattern)) {
37                         return;
38                 }
39
40                 const ignore = patterns
41                         .slice(i)
42                         .filter(isNegative)
43                         .map(pattern => pattern.slice(1));
44
45                 const opts = Object.assign({}, taskOpts, {
46                         ignore: taskOpts.ignore.concat(ignore)
47                 });
48
49                 globTasks.push({pattern, opts});
50         });
51
52         return globTasks;
53 };
54
55 const globDirs = (task, fn) => {
56         if (Array.isArray(task.opts.expandDirectories)) {
57                 return fn(task.pattern, {files: task.opts.expandDirectories});
58         }
59
60         if (typeof task.opts.expandDirectories === 'object') {
61                 return fn(task.pattern, task.opts.expandDirectories);
62         }
63
64         return fn(task.pattern);
65 };
66
67 const getPattern = (task, fn) => task.opts.expandDirectories ? globDirs(task, fn) : [task.pattern];
68
69 module.exports = (patterns, opts) => {
70         let globTasks;
71
72         try {
73                 globTasks = generateGlobTasks(patterns, opts);
74         } catch (err) {
75                 return Promise.reject(err);
76         }
77
78         const getTasks = Promise.all(globTasks.map(task => Promise.resolve(getPattern(task, dirGlob))
79                 .then(globs => Promise.all(globs.map(glob => ({
80                         pattern: glob,
81                         opts: task.opts
82                 }))))
83         ))
84                 .then(tasks => arrayUnion.apply(null, tasks));
85
86         const getFilter = () => {
87                 return Promise.resolve(
88                         opts && opts.gitignore ?
89                                 gitignore({cwd: opts.cwd, ignore: opts.ignore}) :
90                                 DEFAULT_FILTER
91                 );
92         };
93
94         return getFilter()
95                 .then(filter => {
96                         return getTasks
97                                 .then(tasks => Promise.all(tasks.map(task => globP(task.pattern, task.opts))))
98                                 .then(paths => arrayUnion.apply(null, paths))
99                                 .then(paths => paths.filter(p => !filter(p)));
100                 });
101 };
102
103 module.exports.sync = (patterns, opts) => {
104         const globTasks = generateGlobTasks(patterns, opts);
105
106         const getFilter = () => {
107                 return opts && opts.gitignore ?
108                         gitignore.sync({cwd: opts.cwd, ignore: opts.ignore}) :
109                         DEFAULT_FILTER;
110         };
111
112         const tasks = globTasks.reduce((tasks, task) => {
113                 const newTask = getPattern(task, dirGlob.sync).map(glob => ({
114                         pattern: glob,
115                         opts: task.opts
116                 }));
117                 return tasks.concat(newTask);
118         }, []);
119
120         const filter = getFilter();
121
122         return tasks.reduce(
123                 (matches, task) => arrayUnion(matches, glob.sync(task.pattern, task.opts)),
124                 []
125         ).filter(p => !filter(p));
126 };
127
128 module.exports.generateGlobTasks = generateGlobTasks;
129
130 module.exports.hasMagic = (patterns, opts) => []
131         .concat(patterns)
132         .some(pattern => glob.hasMagic(pattern, opts));
133
134 module.exports.gitignore = gitignore;