source: frontend/node_modules/tinyglobby/dist/index.mjs

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 11.8 KB
Line 
1import { readdir, readdirSync, realpath, realpathSync, stat, statSync } from "fs";
2import { isAbsolute, posix, resolve } from "path";
3import { fileURLToPath } from "url";
4import { fdir } from "fdir";
5import picomatch from "picomatch";
6//#region src/utils.ts
7const isReadonlyArray = Array.isArray;
8const BACKSLASHES = /\\/g;
9const isWin = process.platform === "win32";
10const ONLY_PARENT_DIRECTORIES = /^(\/?\.\.)+$/;
11function getPartialMatcher(patterns, options = {}) {
12 const patternsCount = patterns.length;
13 const patternsParts = Array(patternsCount);
14 const matchers = Array(patternsCount);
15 let i, j;
16 for (i = 0; i < patternsCount; i++) {
17 const parts = splitPattern(patterns[i]);
18 patternsParts[i] = parts;
19 const partsCount = parts.length;
20 const partMatchers = Array(partsCount);
21 for (j = 0; j < partsCount; j++) partMatchers[j] = picomatch(parts[j], options);
22 matchers[i] = partMatchers;
23 }
24 return (input) => {
25 const inputParts = input.split("/");
26 if (inputParts[0] === ".." && ONLY_PARENT_DIRECTORIES.test(input)) return true;
27 for (i = 0; i < patternsCount; i++) {
28 const patternParts = patternsParts[i];
29 const matcher = matchers[i];
30 const inputPatternCount = inputParts.length;
31 const minParts = Math.min(inputPatternCount, patternParts.length);
32 j = 0;
33 while (j < minParts) {
34 const part = patternParts[j];
35 if (part.includes("/")) return true;
36 if (!matcher[j](inputParts[j])) break;
37 if (!options.noglobstar && part === "**") return true;
38 j++;
39 }
40 if (j === inputPatternCount) return true;
41 }
42 return false;
43 };
44}
45/* node:coverage ignore next 2 */
46const WIN32_ROOT_DIR = /^[A-Z]:\/$/i;
47const isRoot = isWin ? (p) => WIN32_ROOT_DIR.test(p) : (p) => p === "/";
48function buildFormat(cwd, root, absolute) {
49 if (cwd === root || root.startsWith(`${cwd}/`)) {
50 if (absolute) {
51 const start = cwd.length + +!isRoot(cwd);
52 return (p, isDir) => p.slice(start, isDir ? -1 : void 0) || ".";
53 }
54 const prefix = root.slice(cwd.length + 1);
55 if (prefix) return (p, isDir) => {
56 if (p === ".") return prefix;
57 const result = `${prefix}/${p}`;
58 return isDir ? result.slice(0, -1) : result;
59 };
60 return (p, isDir) => isDir && p !== "." ? p.slice(0, -1) : p;
61 }
62 if (absolute) return (p) => posix.relative(cwd, p) || ".";
63 return (p) => posix.relative(cwd, `${root}/${p}`) || ".";
64}
65function buildRelative(cwd, root) {
66 if (root.startsWith(`${cwd}/`)) {
67 const prefix = root.slice(cwd.length + 1);
68 return (p) => `${prefix}/${p}`;
69 }
70 return (p) => {
71 const result = posix.relative(cwd, `${root}/${p}`);
72 return p[p.length - 1] === "/" && result !== "" ? `${result}/` : result || ".";
73 };
74}
75const splitPatternOptions = { parts: true };
76function splitPattern(path) {
77 var _result$parts;
78 const result = picomatch.scan(path, splitPatternOptions);
79 return ((_result$parts = result.parts) === null || _result$parts === void 0 ? void 0 : _result$parts.length) ? result.parts : [path];
80}
81const ESCAPED_WIN32_BACKSLASHES = /\\(?![()[\]{}!+@])/g;
82function convertPosixPathToPattern(path) {
83 return escapePosixPath(path);
84}
85function convertWin32PathToPattern(path) {
86 return escapeWin32Path(path).replace(ESCAPED_WIN32_BACKSLASHES, "/");
87}
88/**
89* Converts a path to a pattern depending on the platform.
90* Identical to {@link escapePath} on POSIX systems.
91* @see {@link https://superchupu.dev/tinyglobby/documentation#convertPathToPattern}
92*/
93/* node:coverage ignore next 3 */
94const convertPathToPattern = isWin ? convertWin32PathToPattern : convertPosixPathToPattern;
95const POSIX_UNESCAPED_GLOB_SYMBOLS = /(?<!\\)([()[\]{}*?|]|^!|[!+@](?=\()|\\(?![()[\]{}!*+?@|]))/g;
96const WIN32_UNESCAPED_GLOB_SYMBOLS = /(?<!\\)([()[\]{}]|^!|[!+@](?=\())/g;
97const escapePosixPath = (path) => path.replace(POSIX_UNESCAPED_GLOB_SYMBOLS, "\\$&");
98const escapeWin32Path = (path) => path.replace(WIN32_UNESCAPED_GLOB_SYMBOLS, "\\$&");
99/**
100* Escapes a path's special characters depending on the platform.
101* @see {@link https://superchupu.dev/tinyglobby/documentation#escapePath}
102*/
103/* node:coverage ignore next */
104const escapePath = isWin ? escapeWin32Path : escapePosixPath;
105/**
106* Checks if a pattern has dynamic parts.
107*
108* Has a few minor differences with [`fast-glob`](https://github.com/mrmlnc/fast-glob) for better accuracy:
109*
110* - Doesn't necessarily return `false` on patterns that include `\`.
111* - Returns `true` if the pattern includes parentheses, regardless of them representing one single pattern or not.
112* - Returns `true` for unfinished glob extensions i.e. `(h`, `+(h`.
113* - Returns `true` for unfinished brace expansions as long as they include `,` or `..`.
114*
115* @see {@link https://superchupu.dev/tinyglobby/documentation#isDynamicPattern}
116*/
117function isDynamicPattern(pattern, options) {
118 if ((options === null || options === void 0 ? void 0 : options.caseSensitiveMatch) === false) return true;
119 const scan = picomatch.scan(pattern);
120 return scan.isGlob || scan.negated;
121}
122function log(...tasks) {
123 console.log(`[tinyglobby ${(/* @__PURE__ */ new Date()).toLocaleTimeString("es")}]`, ...tasks);
124}
125function ensureStringArray(value) {
126 return typeof value === "string" ? [value] : value !== null && value !== void 0 ? value : [];
127}
128//#endregion
129//#region src/patterns.ts
130const PARENT_DIRECTORY = /^(\/?\.\.)+/;
131const ESCAPING_BACKSLASHES = /\\(?=[()[\]{}!*+?@|])/g;
132function normalizePattern(pattern, opts, props, isIgnore) {
133 var _PARENT_DIRECTORY$exe;
134 const cwd = opts.cwd;
135 let result = pattern;
136 if (pattern[pattern.length - 1] === "/") result = pattern.slice(0, -1);
137 if (result[result.length - 1] !== "*" && opts.expandDirectories) result += "/**";
138 const escapedCwd = escapePath(cwd);
139 result = isAbsolute(result.replace(ESCAPING_BACKSLASHES, "")) ? posix.relative(escapedCwd, result) : posix.normalize(result);
140 const parentDir = (_PARENT_DIRECTORY$exe = PARENT_DIRECTORY.exec(result)) === null || _PARENT_DIRECTORY$exe === void 0 ? void 0 : _PARENT_DIRECTORY$exe[0];
141 const parts = splitPattern(result);
142 if (parentDir) {
143 const n = (parentDir.length + 1) / 3;
144 let i = 0;
145 const cwdParts = escapedCwd.split("/");
146 while (i < n && parts[i + n] === cwdParts[cwdParts.length + i - n]) {
147 result = result.slice(0, (n - i - 1) * 3) + result.slice((n - i) * 3 + parts[i + n].length + 1) || ".";
148 i++;
149 }
150 const potentialRoot = posix.join(cwd, parentDir.slice(i * 3));
151 if (potentialRoot[0] !== "." && props.root.length > potentialRoot.length) {
152 props.root = potentialRoot;
153 props.depthOffset = -n + i;
154 }
155 }
156 if (!isIgnore && props.depthOffset >= 0) {
157 var _props$commonPath;
158 (_props$commonPath = props.commonPath) !== null && _props$commonPath !== void 0 || (props.commonPath = parts);
159 const newCommonPath = [];
160 const length = Math.min(props.commonPath.length, parts.length);
161 for (let i = 0; i < length; i++) {
162 const part = parts[i];
163 if (part === "**" && !parts[i + 1]) {
164 newCommonPath.pop();
165 break;
166 }
167 if (i === parts.length - 1 || part !== props.commonPath[i] || isDynamicPattern(part)) break;
168 newCommonPath.push(part);
169 }
170 props.depthOffset = newCommonPath.length;
171 props.commonPath = newCommonPath;
172 props.root = newCommonPath.length > 0 ? posix.join(cwd, ...newCommonPath) : cwd;
173 }
174 return result;
175}
176function processPatterns(options, patterns, props) {
177 const matchPatterns = [];
178 const ignorePatterns = [];
179 for (const pattern of options.ignore) {
180 if (!pattern) continue;
181 if (pattern[0] !== "!" || pattern[1] === "(") ignorePatterns.push(normalizePattern(pattern, options, props, true));
182 }
183 for (const pattern of patterns) {
184 if (!pattern) continue;
185 if (pattern[0] !== "!" || pattern[1] === "(") matchPatterns.push(normalizePattern(pattern, options, props, false));
186 else if (pattern[1] !== "!" || pattern[2] === "(") ignorePatterns.push(normalizePattern(pattern.slice(1), options, props, true));
187 }
188 return {
189 match: matchPatterns,
190 ignore: ignorePatterns
191 };
192}
193//#endregion
194//#region src/crawler.ts
195function buildCrawler(options, patterns) {
196 const cwd = options.cwd;
197 const props = {
198 root: cwd,
199 depthOffset: 0
200 };
201 const processed = processPatterns(options, patterns, props);
202 if (options.debug) log("internal processing patterns:", processed);
203 const { absolute, caseSensitiveMatch, debug, dot, followSymbolicLinks, onlyDirectories } = options;
204 const root = props.root.replace(BACKSLASHES, "");
205 const matchOptions = {
206 dot,
207 nobrace: options.braceExpansion === false,
208 nocase: !caseSensitiveMatch,
209 noextglob: options.extglob === false,
210 noglobstar: options.globstar === false,
211 posix: true
212 };
213 const matcher = picomatch(processed.match, matchOptions);
214 const ignore = picomatch(processed.ignore, matchOptions);
215 const partialMatcher = getPartialMatcher(processed.match, matchOptions);
216 const format = buildFormat(cwd, root, absolute);
217 const excludeFormatter = absolute ? format : buildFormat(cwd, root, true);
218 const excludePredicate = (_, p) => {
219 const relativePath = excludeFormatter(p, true);
220 return relativePath !== "." && !partialMatcher(relativePath) || ignore(relativePath);
221 };
222 let maxDepth;
223 if (options.deep !== void 0) maxDepth = Math.round(options.deep - props.depthOffset);
224 const crawler = new fdir({
225 filters: [debug ? (p, isDirectory) => {
226 const path = format(p, isDirectory);
227 const matches = matcher(path) && !ignore(path);
228 if (matches) log(`matched ${path}`);
229 return matches;
230 } : (p, isDirectory) => {
231 const path = format(p, isDirectory);
232 return matcher(path) && !ignore(path);
233 }],
234 exclude: debug ? (_, p) => {
235 const skipped = excludePredicate(_, p);
236 log(`${skipped ? "skipped" : "crawling"} ${p}`);
237 return skipped;
238 } : excludePredicate,
239 fs: options.fs,
240 pathSeparator: "/",
241 relativePaths: !absolute,
242 resolvePaths: absolute,
243 includeBasePath: absolute,
244 resolveSymlinks: followSymbolicLinks,
245 excludeSymlinks: !followSymbolicLinks,
246 excludeFiles: onlyDirectories,
247 includeDirs: onlyDirectories || !options.onlyFiles,
248 maxDepth,
249 signal: options.signal
250 }).crawl(root);
251 if (options.debug) log("internal properties:", {
252 ...props,
253 root
254 });
255 return [crawler, cwd !== root && !absolute && buildRelative(cwd, root)];
256}
257//#endregion
258//#region src/index.ts
259function formatPaths(paths, mapper) {
260 if (mapper) for (let i = paths.length - 1; i >= 0; i--) paths[i] = mapper(paths[i]);
261 return paths;
262}
263const defaultOptions = {
264 caseSensitiveMatch: true,
265 cwd: process.cwd(),
266 debug: !!process.env.TINYGLOBBY_DEBUG,
267 expandDirectories: true,
268 followSymbolicLinks: true,
269 onlyFiles: true
270};
271function getOptions(options) {
272 const opts = {
273 ...defaultOptions,
274 ...options
275 };
276 opts.cwd = (opts.cwd instanceof URL ? fileURLToPath(opts.cwd) : resolve(opts.cwd)).replace(BACKSLASHES, "/");
277 opts.ignore = ensureStringArray(opts.ignore);
278 opts.fs && (opts.fs = {
279 readdir: opts.fs.readdir || readdir,
280 readdirSync: opts.fs.readdirSync || readdirSync,
281 realpath: opts.fs.realpath || realpath,
282 realpathSync: opts.fs.realpathSync || realpathSync,
283 stat: opts.fs.stat || stat,
284 statSync: opts.fs.statSync || statSync
285 });
286 if (opts.debug) log("globbing with options:", opts);
287 return opts;
288}
289function getCrawler(globInput, inputOptions = {}) {
290 var _ref;
291 if (globInput && (inputOptions === null || inputOptions === void 0 ? void 0 : inputOptions.patterns)) throw new Error("Cannot pass patterns as both an argument and an option");
292 const isModern = isReadonlyArray(globInput) || typeof globInput === "string";
293 const patterns = ensureStringArray((_ref = isModern ? globInput : globInput.patterns) !== null && _ref !== void 0 ? _ref : "**/*");
294 const options = getOptions(isModern ? inputOptions : globInput);
295 return patterns.length > 0 ? buildCrawler(options, patterns) : [];
296}
297async function glob(globInput, options) {
298 const [crawler, relative] = getCrawler(globInput, options);
299 return crawler ? formatPaths(await crawler.withPromise(), relative) : [];
300}
301function globSync(globInput, options) {
302 const [crawler, relative] = getCrawler(globInput, options);
303 return crawler ? formatPaths(crawler.sync(), relative) : [];
304}
305//#endregion
306export { convertPathToPattern, escapePath, glob, globSync, isDynamicPattern };
Note: See TracBrowser for help on using the repository browser.