UNPKG

1.14 kBPlain TextView Raw
1import { Glob } from 'glob';
2import * as minimatch from 'minimatch';
3
4export interface IGlobOptions {
5 nodir?: boolean;
6 dot?: boolean;
7 ignore?: string;
8}
9
10/**
11 * Matches the given glob pattern as a promise.
12 * See:
13 * https://www.npmjs.com/package/glob
14 */
15export 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 // tslint:disable-line
22 if (err) {
23 reject(err);
24 } else {
25 resolve(matches);
26 }
27 });
28 });
29}
30
31export type IGlobMatchOptions = {
32 debug?: boolean;
33};
34
35/**
36 * Determines if the given path matches a glob pattern.
37 */
38export 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 */
49export 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}