1 | import fs from 'node:fs';
|
2 | import arrayUnion from 'array-union';
|
3 | import merge2 from 'merge2';
|
4 | import fastGlob from 'fast-glob';
|
5 | import dirGlob from 'dir-glob';
|
6 | import toPath from './to-path.js';
|
7 | import {isGitIgnored, isGitIgnoredSync} from './gitignore.js';
|
8 | import {FilterStream, UniqueStream} from './stream-utils.js';
|
9 |
|
10 | const DEFAULT_FILTER = () => false;
|
11 |
|
12 | const isNegative = pattern => pattern[0] === '!';
|
13 |
|
14 | const 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 |
|
20 | const 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 |
|
37 | const getPathString = p => p.stats instanceof fs.Stats ? p.path : p;
|
38 |
|
39 | export const generateGlobTasks = (patterns, taskOptions = {}) => {
|
40 | patterns = arrayUnion([patterns].flat());
|
41 | assertPatternsInput(patterns);
|
42 |
|
43 | const globTasks = [];
|
44 |
|
45 | taskOptions = {
|
46 | ignore: [],
|
47 | expandDirectories: true,
|
48 | ...taskOptions,
|
49 | cwd: toPath(taskOptions.cwd),
|
50 | };
|
51 |
|
52 | checkCwdOption(taskOptions);
|
53 |
|
54 | for (const [index, pattern] of patterns.entries()) {
|
55 | if (isNegative(pattern)) {
|
56 | continue;
|
57 | }
|
58 |
|
59 | const ignore = patterns
|
60 | .slice(index)
|
61 | .filter(pattern => isNegative(pattern))
|
62 | .map(pattern => pattern.slice(1));
|
63 |
|
64 | const options = {
|
65 | ...taskOptions,
|
66 | ignore: [...taskOptions.ignore, ...ignore],
|
67 | };
|
68 |
|
69 | globTasks.push({pattern, options});
|
70 | }
|
71 |
|
72 | return globTasks;
|
73 | };
|
74 |
|
75 | const globDirectories = (task, fn) => {
|
76 | let options = {};
|
77 | if (task.options.cwd) {
|
78 | options.cwd = task.options.cwd;
|
79 | }
|
80 |
|
81 | if (Array.isArray(task.options.expandDirectories)) {
|
82 | options = {
|
83 | ...options,
|
84 | files: task.options.expandDirectories,
|
85 | };
|
86 | } else if (typeof task.options.expandDirectories === 'object') {
|
87 | options = {
|
88 | ...options,
|
89 | ...task.options.expandDirectories,
|
90 | };
|
91 | }
|
92 |
|
93 | return fn(task.pattern, options);
|
94 | };
|
95 |
|
96 | const getPattern = (task, fn) => task.options.expandDirectories ? globDirectories(task, fn) : [task.pattern];
|
97 |
|
98 | const getFilterSync = options => options && options.gitignore
|
99 | ? isGitIgnoredSync({cwd: options.cwd, ignore: options.ignore})
|
100 | : DEFAULT_FILTER;
|
101 |
|
102 | const globToTask = task => async glob => {
|
103 | const {options} = task;
|
104 | if (options.ignore && Array.isArray(options.ignore) && options.expandDirectories) {
|
105 | options.ignore = await dirGlob(options.ignore);
|
106 | }
|
107 |
|
108 | return {
|
109 | pattern: glob,
|
110 | options,
|
111 | };
|
112 | };
|
113 |
|
114 | const globToTaskSync = task => glob => {
|
115 | const {options} = task;
|
116 | if (options.ignore && Array.isArray(options.ignore) && options.expandDirectories) {
|
117 | options.ignore = dirGlob.sync(options.ignore);
|
118 | }
|
119 |
|
120 | return {
|
121 | pattern: glob,
|
122 | options,
|
123 | };
|
124 | };
|
125 |
|
126 | export const globby = async (patterns, options) => {
|
127 | const globTasks = generateGlobTasks(patterns, options);
|
128 |
|
129 | const getFilter = async () => options && options.gitignore
|
130 | ? isGitIgnored({cwd: options.cwd, ignore: options.ignore})
|
131 | : DEFAULT_FILTER;
|
132 |
|
133 | const getTasks = async () => {
|
134 | const tasks = await Promise.all(globTasks.map(async task => {
|
135 | const globs = await getPattern(task, dirGlob);
|
136 | return Promise.all(globs.map(globToTask(task)));
|
137 | }));
|
138 |
|
139 | return arrayUnion(...tasks);
|
140 | };
|
141 |
|
142 | const [filter, tasks] = await Promise.all([getFilter(), getTasks()]);
|
143 | const paths = await Promise.all(tasks.map(task => fastGlob(task.pattern, task.options)));
|
144 |
|
145 | return arrayUnion(...paths).filter(path_ => !filter(getPathString(path_)));
|
146 | };
|
147 |
|
148 | export const globbySync = (patterns, options) => {
|
149 | const globTasks = generateGlobTasks(patterns, options);
|
150 |
|
151 | const tasks = [];
|
152 | for (const task of globTasks) {
|
153 | const newTask = getPattern(task, dirGlob.sync).map(globToTaskSync(task));
|
154 | tasks.push(...newTask);
|
155 | }
|
156 |
|
157 | const filter = getFilterSync(options);
|
158 |
|
159 | let matches = [];
|
160 | for (const task of tasks) {
|
161 | matches = arrayUnion(matches, fastGlob.sync(task.pattern, task.options));
|
162 | }
|
163 |
|
164 | return matches.filter(path_ => !filter(path_));
|
165 | };
|
166 |
|
167 | export const globbyStream = (patterns, options) => {
|
168 | const globTasks = generateGlobTasks(patterns, options);
|
169 |
|
170 | const tasks = [];
|
171 | for (const task of globTasks) {
|
172 | const newTask = getPattern(task, dirGlob.sync).map(globToTaskSync(task));
|
173 | tasks.push(...newTask);
|
174 | }
|
175 |
|
176 | const filter = getFilterSync(options);
|
177 | const filterStream = new FilterStream(p => !filter(p));
|
178 | const uniqueStream = new UniqueStream();
|
179 |
|
180 | return merge2(tasks.map(task => fastGlob.stream(task.pattern, task.options)))
|
181 | .pipe(filterStream)
|
182 | .pipe(uniqueStream);
|
183 | };
|
184 |
|
185 | export const isDynamicPattern = (patterns, options = {}) => {
|
186 | options = {
|
187 | ...options,
|
188 | cwd: toPath(options.cwd),
|
189 | };
|
190 |
|
191 | return [patterns].flat().some(pattern => fastGlob.isDynamicPattern(pattern, options));
|
192 | };
|
193 |
|
194 | export {
|
195 | isGitIgnored,
|
196 | isGitIgnoredSync,
|
197 | } from './gitignore.js';
|