UNPKG

5.39 kBMarkdownView Raw
1# globby
2
3> User-friendly glob matching
4
5Based on [`fast-glob`](https://github.com/mrmlnc/fast-glob) but adds a bunch of useful features.
6
7## Features
8
9- Promise API
10- Multiple patterns
11- Negated patterns: `['foo*', '!foobar']`
12- Expands directories: `foo``foo/**/*`
13- Supports `.gitignore`
14- Supports `URL` as `cwd`
15
16## Install
17
18```
19$ npm install globby
20```
21
22## Usage
23
24```
25├── unicorn
26├── cake
27└── rainbow
28```
29
30```js
31import {globby} from 'globby';
32
33const paths = await globby(['*', '!cake']);
34
35console.log(paths);
36//=> ['unicorn', 'rainbow']
37```
38
39## API
40
41Note that glob patterns can only contain forward-slashes, not backward-slashes, so if you want to construct a glob pattern from path components, you need to use `path.posix.join()` instead of `path.join()`.
42
43### globby(patterns, options?)
44
45Returns a `Promise<string[]>` of matching paths.
46
47#### patterns
48
49Type: `string | string[]`
50
51See supported `minimatch` [patterns](https://github.com/isaacs/minimatch#usage).
52
53#### options
54
55Type: `object`
56
57See the [`fast-glob` options](https://github.com/mrmlnc/fast-glob#options-3) in addition to the ones below.
58
59##### expandDirectories
60
61Type: `boolean | string[] | object`\
62Default: `true`
63
64If set to `true`, `globby` will automatically glob directories for you. If you define an `Array` it will only glob files that matches the patterns inside the `Array`. You can also define an `object` with `files` and `extensions` like below:
65
66```js
67import {globby} from 'globby';
68
69(async () => {
70 const paths = await globby('images', {
71 expandDirectories: {
72 files: ['cat', 'unicorn', '*.jpg'],
73 extensions: ['png']
74 }
75 });
76
77 console.log(paths);
78 //=> ['cat.png', 'unicorn.png', 'cow.jpg', 'rainbow.jpg']
79})();
80```
81
82Note that if you set this option to `false`, you won't get back matched directories unless you set `onlyFiles: false`.
83
84##### gitignore
85
86Type: `boolean`\
87Default: `false`
88
89Respect ignore patterns in `.gitignore` files that apply to the globbed files.
90
91### globbySync(patterns, options?)
92
93Returns `string[]` of matching paths.
94
95### globbyStream(patterns, options?)
96
97Returns a [`stream.Readable`](https://nodejs.org/api/stream.html#stream_readable_streams) of matching paths.
98
99Since Node.js 10, [readable streams are iterable](https://nodejs.org/api/stream.html#stream_readable_symbol_asynciterator), so you can loop over glob matches in a [`for await...of` loop](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of) like this:
100
101```js
102import {globbyStream} from 'globby';
103
104(async () => {
105 for await (const path of globbyStream('*.tmp')) {
106 console.log(path);
107 }
108})();
109```
110
111### generateGlobTasks(patterns, options?)
112
113Returns an `object[]` in the format `{pattern: string, options: Object}`, which can be passed as arguments to [`fast-glob`](https://github.com/mrmlnc/fast-glob). This is useful for other globbing-related packages.
114
115Note that you should avoid running the same tasks multiple times as they contain a file system cache. Instead, run this method each time to ensure file system changes are taken into consideration.
116
117### isDynamicPattern(patterns, options?)
118
119Returns a `boolean` of whether there are any special glob characters in the `patterns`.
120
121Note that the options affect the results.
122
123This function is backed by [`fast-glob`](https://github.com/mrmlnc/fast-glob#isdynamicpatternpattern-options).
124
125### isGitIgnored(options?)
126
127Returns a `Promise<(path: URL | string) => boolean>` indicating whether a given path is ignored via a `.gitignore` file.
128
129Takes `cwd?: URL | string` and `ignore?: string[]` as options. `.gitignore` files matched by the ignore config are not used for the resulting filter function.
130
131```js
132import {isGitIgnored} from 'globby';
133
134const isIgnored = await isGitIgnored();
135
136console.log(isIgnored('some/file'));
137```
138
139### isGitIgnoredSync(options?)
140
141Returns a `(path: URL | string) => boolean` indicating whether a given path is ignored via a `.gitignore` file.
142
143Takes the same options as `isGitIgnored`.
144
145## Globbing patterns
146
147Just a quick overview.
148
149- `*` matches any number of characters, but not `/`
150- `?` matches a single character, but not `/`
151- `**` matches any number of characters, including `/`, as long as it's the only thing in a path part
152- `{}` allows for a comma-separated list of "or" expressions
153- `!` at the beginning of a pattern will negate the match
154
155[Various patterns and expected matches.](https://github.com/sindresorhus/multimatch/blob/main/test/test.js)
156
157## globby for enterprise
158
159Available as part of the Tidelift Subscription.
160
161The maintainers of globby and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/npm-globby?utm_source=npm-globby&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)
162
163## Related
164
165- [multimatch](https://github.com/sindresorhus/multimatch) - Match against a list instead of the filesystem
166- [matcher](https://github.com/sindresorhus/matcher) - Simple wildcard matching
167- [del](https://github.com/sindresorhus/del) - Delete files and directories
168- [make-dir](https://github.com/sindresorhus/make-dir) - Make a directory and its parents if needed