1 | import { Glob } from 'glob';
|
2 | import * as minimatch from 'minimatch';
|
3 |
|
4 | export interface IGlobOptions {
|
5 | nodir?: boolean;
|
6 | dot?: boolean;
|
7 | ignore?: string;
|
8 | }
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | export function find(
|
16 | pattern: string,
|
17 | options: IGlobOptions = {},
|
18 | ): Promise<string[]> {
|
19 | return new Promise<string[]>((resolve, reject) => {
|
20 | new Glob(pattern, options, (err, matches) => {
|
21 |
|
22 | if (err) {
|
23 | reject(err);
|
24 | } else {
|
25 | resolve(matches);
|
26 | }
|
27 | });
|
28 | });
|
29 | }
|
30 |
|
31 | export type IGlobMatchOptions = {
|
32 | debug?: boolean;
|
33 | };
|
34 |
|
35 | /**
|
36 | * Determines if the given path matches a glob pattern.
|
37 | */
|
38 | export function isMatch(
|
39 | pattern: string,
|
40 | path: string,
|
41 | options: IGlobMatchOptions = {},
|
42 | ) {
|
43 | return minimatch(path, pattern, options);
|
44 | }
|
45 |
|
46 | /**
|
47 | * Returns a glob pattern matcher
|
48 | */
|
49 | export function matcher(pattern: string, options: IGlobMatchOptions = {}) {
|
50 | const args = options;
|
51 | return (path: string, options: IGlobMatchOptions = {}) => {
|
52 | return isMatch(pattern, path, { ...args, ...options });
|
53 | };
|
54 | }
|