/
glob.js
99 lines (89 loc) 路 2.32 KB
/
glob.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// @flow
import type {FilePath, Glob} from '@parcel/types';
import type {FileSystem} from '@parcel/fs';
import _isGlob from 'is-glob';
import fastGlob, {type FastGlobOptions} from 'fast-glob';
import {isMatch, makeRe, type Options} from 'micromatch';
import {normalizeSeparators} from './path';
export function isGlob(p: FilePath): any {
return _isGlob(normalizeSeparators(p));
}
export function isGlobMatch(
filePath: FilePath,
glob: Glob | Array<Glob>,
opts?: Options,
): any {
glob = Array.isArray(glob)
? glob.map(normalizeSeparators)
: normalizeSeparators(glob);
return isMatch(filePath, glob, opts);
}
export function globToRegex(glob: Glob, opts?: Options): RegExp {
return makeRe(glob, opts);
}
export function globSync(
p: FilePath,
fs: FileSystem,
options?: FastGlobOptions<FilePath>,
): Array<FilePath> {
// $FlowFixMe
options = {
...options,
fs: {
statSync: p => {
return fs.statSync(p);
},
lstatSync: p => {
// Our FileSystem interface doesn't have lstat support at the moment,
// but this is fine for our purposes since we follow symlinks by default.
return fs.statSync(p);
},
readdirSync: (p, opts) => {
return fs.readdirSync(p, opts);
},
},
};
// $FlowFixMe
return fastGlob.sync(normalizeSeparators(p), options);
}
export function glob(
p: FilePath,
fs: FileSystem,
options: FastGlobOptions<FilePath>,
): Promise<Array<FilePath>> {
// $FlowFixMe
options = {
...options,
fs: {
stat: async (p, cb) => {
try {
cb(null, await fs.stat(p));
} catch (err) {
cb(err);
}
},
lstat: async (p, cb) => {
// Our FileSystem interface doesn't have lstat support at the moment,
// but this is fine for our purposes since we follow symlinks by default.
try {
cb(null, await fs.stat(p));
} catch (err) {
cb(err);
}
},
readdir: async (p, opts, cb) => {
if (typeof opts === 'function') {
cb = opts;
opts = null;
}
try {
cb(null, await fs.readdir(p, opts));
} catch (err) {
cb(err);
}
},
},
};
// $FlowFixMe Added in Flow 0.121.0 upgrade in #4381
return fastGlob(normalizeSeparators(p), options);
}