source: trip-planner-front/node_modules/globby/index.js@ 571e0df

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

initial commit

  • Property mode set to 100644
File size: 4.4 KB
Line 
1'use strict';
2const fs = require('fs');
3const arrayUnion = require('array-union');
4const merge2 = require('merge2');
5const fastGlob = require('fast-glob');
6const dirGlob = require('dir-glob');
7const gitignore = require('./gitignore');
8const {FilterStream, UniqueStream} = require('./stream-utils');
9
10const DEFAULT_FILTER = () => false;
11
12const isNegative = pattern => pattern[0] === '!';
13
14const assertPatternsInput = patterns => {
15 if (!patterns.every(pattern => typeof pattern === 'string')) {
16 throw new TypeError('Patterns must be a string or an array of strings');
17 }
18};
19
20const checkCwdOption = (options = {}) => {
21 if (!options.cwd) {
22 return;
23 }
24
25 let stat;
26 try {
27 stat = fs.statSync(options.cwd);
28 } catch {
29 return;
30 }
31
32 if (!stat.isDirectory()) {
33 throw new Error('The `cwd` option must be a path to a directory');
34 }
35};
36
37const getPathString = p => p.stats instanceof fs.Stats ? p.path : p;
38
39const generateGlobTasks = (patterns, taskOptions) => {
40 patterns = arrayUnion([].concat(patterns));
41 assertPatternsInput(patterns);
42 checkCwdOption(taskOptions);
43
44 const globTasks = [];
45
46 taskOptions = {
47 ignore: [],
48 expandDirectories: true,
49 ...taskOptions
50 };
51
52 for (const [index, pattern] of patterns.entries()) {
53 if (isNegative(pattern)) {
54 continue;
55 }
56
57 const ignore = patterns
58 .slice(index)
59 .filter(pattern => isNegative(pattern))
60 .map(pattern => pattern.slice(1));
61
62 const options = {
63 ...taskOptions,
64 ignore: taskOptions.ignore.concat(ignore)
65 };
66
67 globTasks.push({pattern, options});
68 }
69
70 return globTasks;
71};
72
73const globDirs = (task, fn) => {
74 let options = {};
75 if (task.options.cwd) {
76 options.cwd = task.options.cwd;
77 }
78
79 if (Array.isArray(task.options.expandDirectories)) {
80 options = {
81 ...options,
82 files: task.options.expandDirectories
83 };
84 } else if (typeof task.options.expandDirectories === 'object') {
85 options = {
86 ...options,
87 ...task.options.expandDirectories
88 };
89 }
90
91 return fn(task.pattern, options);
92};
93
94const getPattern = (task, fn) => task.options.expandDirectories ? globDirs(task, fn) : [task.pattern];
95
96const getFilterSync = options => {
97 return options && options.gitignore ?
98 gitignore.sync({cwd: options.cwd, ignore: options.ignore}) :
99 DEFAULT_FILTER;
100};
101
102const globToTask = task => glob => {
103 const {options} = task;
104 if (options.ignore && Array.isArray(options.ignore) && options.expandDirectories) {
105 options.ignore = dirGlob.sync(options.ignore);
106 }
107
108 return {
109 pattern: glob,
110 options
111 };
112};
113
114module.exports = async (patterns, options) => {
115 const globTasks = generateGlobTasks(patterns, options);
116
117 const getFilter = async () => {
118 return options && options.gitignore ?
119 gitignore({cwd: options.cwd, ignore: options.ignore}) :
120 DEFAULT_FILTER;
121 };
122
123 const getTasks = async () => {
124 const tasks = await Promise.all(globTasks.map(async task => {
125 const globs = await getPattern(task, dirGlob);
126 return Promise.all(globs.map(globToTask(task)));
127 }));
128
129 return arrayUnion(...tasks);
130 };
131
132 const [filter, tasks] = await Promise.all([getFilter(), getTasks()]);
133 const paths = await Promise.all(tasks.map(task => fastGlob(task.pattern, task.options)));
134
135 return arrayUnion(...paths).filter(path_ => !filter(getPathString(path_)));
136};
137
138module.exports.sync = (patterns, options) => {
139 const globTasks = generateGlobTasks(patterns, options);
140
141 const tasks = [];
142 for (const task of globTasks) {
143 const newTask = getPattern(task, dirGlob.sync).map(globToTask(task));
144 tasks.push(...newTask);
145 }
146
147 const filter = getFilterSync(options);
148
149 let matches = [];
150 for (const task of tasks) {
151 matches = arrayUnion(matches, fastGlob.sync(task.pattern, task.options));
152 }
153
154 return matches.filter(path_ => !filter(path_));
155};
156
157module.exports.stream = (patterns, options) => {
158 const globTasks = generateGlobTasks(patterns, options);
159
160 const tasks = [];
161 for (const task of globTasks) {
162 const newTask = getPattern(task, dirGlob.sync).map(globToTask(task));
163 tasks.push(...newTask);
164 }
165
166 const filter = getFilterSync(options);
167 const filterStream = new FilterStream(p => !filter(p));
168 const uniqueStream = new UniqueStream();
169
170 return merge2(tasks.map(task => fastGlob.stream(task.pattern, task.options)))
171 .pipe(filterStream)
172 .pipe(uniqueStream);
173};
174
175module.exports.generateGlobTasks = generateGlobTasks;
176
177module.exports.hasMagic = (patterns, options) => []
178 .concat(patterns)
179 .some(pattern => fastGlob.isDynamicPattern(pattern, options));
180
181module.exports.gitignore = gitignore;
Note: See TracBrowser for help on using the repository browser.