1 |
|
2 |
|
3 | import type {FilePath, Glob} from '@parcel/types';
|
4 | import type {FileSystem} from '@parcel/fs';
|
5 |
|
6 | import _isGlob from 'is-glob';
|
7 | import fastGlob, {type FastGlobOptions} from 'fast-glob';
|
8 | import {isMatch} from 'micromatch';
|
9 | import {normalizeSeparators} from './path';
|
10 |
|
11 | export function isGlob(p: FilePath) {
|
12 | return _isGlob(normalizeSeparators(p));
|
13 | }
|
14 |
|
15 | export function isGlobMatch(filePath: FilePath, glob: Glob) {
|
16 | return isMatch(filePath, normalizeSeparators(glob));
|
17 | }
|
18 |
|
19 | export function globSync(
|
20 | p: FilePath,
|
21 | options: FastGlobOptions<FilePath>,
|
22 | ): Array<FilePath> {
|
23 | return fastGlob.sync(normalizeSeparators(p), options);
|
24 | }
|
25 |
|
26 | export function glob(
|
27 | p: FilePath,
|
28 | fs: FileSystem,
|
29 | options: FastGlobOptions<FilePath>,
|
30 | ): Promise<Array<FilePath>> {
|
31 |
|
32 | options = {
|
33 | ...options,
|
34 | fs: {
|
35 | stat: async (p, cb) => {
|
36 | try {
|
37 | cb(null, await fs.stat(p));
|
38 | } catch (err) {
|
39 | cb(err);
|
40 | }
|
41 | },
|
42 | lstat: async (p, cb) => {
|
43 |
|
44 |
|
45 | try {
|
46 | cb(null, await fs.stat(p));
|
47 | } catch (err) {
|
48 | cb(err);
|
49 | }
|
50 | },
|
51 | readdir: async (p, opts, cb) => {
|
52 | if (typeof opts === 'function') {
|
53 | cb = opts;
|
54 | opts = null;
|
55 | }
|
56 |
|
57 | try {
|
58 | cb(null, await fs.readdir(p, opts));
|
59 | } catch (err) {
|
60 | cb(err);
|
61 | }
|
62 | },
|
63 | },
|
64 | };
|
65 |
|
66 |
|
67 | return fastGlob(normalizeSeparators(p), options);
|
68 | }
|