UNPKG

1.65 kBJavaScriptView Raw
1// @flow
2
3import type {FilePath, Glob} from '@parcel/types';
4import type {FileSystem} from '@parcel/fs';
5
6import _isGlob from 'is-glob';
7import fastGlob, {type FastGlobOptions} from 'fast-glob';
8import {isMatch} from 'micromatch';
9import {normalizeSeparators} from './path';
10
11export function isGlob(p: FilePath) {
12 return _isGlob(normalizeSeparators(p));
13}
14
15export function isGlobMatch(filePath: FilePath, glob: Glob) {
16 return isMatch(filePath, normalizeSeparators(glob));
17}
18
19export function globSync(
20 p: FilePath,
21 options: FastGlobOptions<FilePath>,
22): Array<FilePath> {
23 return fastGlob.sync(normalizeSeparators(p), options);
24}
25
26export function glob(
27 p: FilePath,
28 fs: FileSystem,
29 options: FastGlobOptions<FilePath>,
30): Promise<Array<FilePath>> {
31 // $FlowFixMe
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 // Our FileSystem interface doesn't have lstat support at the moment,
44 // but this is fine for our purposes since we follow symlinks by default.
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 // $FlowFixMe Added in Flow 0.121.0 upgrade in #4381
67 return fastGlob(normalizeSeparators(p), options);
68}