UNPKG

6.06 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` and similar ignore config files
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##### ignoreFiles
92
93Type: `string | string[]`\
94Default: `undefined`
95
96Glob patterns to look for ignore files, which are then used to ignore globbed files.
97
98This is a more generic form of the `gitignore` option, allowing you to find ignore files with a [compatible syntax](http://git-scm.com/docs/gitignore). For instance, this works with Babel's `.babelignore`, Prettier's `.prettierignore`, or ESLint's `.eslintignore` files.
99
100### globbySync(patterns, options?)
101
102Returns `string[]` of matching paths.
103
104### globbyStream(patterns, options?)
105
106Returns a [`stream.Readable`](https://nodejs.org/api/stream.html#stream_readable_streams) of matching paths.
107
108Since 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:
109
110```js
111import {globbyStream} from 'globby';
112
113(async () => {
114 for await (const path of globbyStream('*.tmp')) {
115 console.log(path);
116 }
117})();
118```
119
120### generateGlobTasks(patterns, options?)
121
122Returns an `Promise<object[]>` in the format `{patterns: 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.
123
124Note 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.
125
126### generateGlobTasksSync(patterns, options?)
127
128Returns an `object[]` in the format `{patterns: 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.
129
130Takes the same arguments as `generateGlobTasks`.
131
132### isDynamicPattern(patterns, options?)
133
134Returns a `boolean` of whether there are any special glob characters in the `patterns`.
135
136Note that the options affect the results.
137
138This function is backed by [`fast-glob`](https://github.com/mrmlnc/fast-glob#isdynamicpatternpattern-options).
139
140### isGitIgnored(options?)
141
142Returns a `Promise<(path: URL | string) => boolean>` indicating whether a given path is ignored via a `.gitignore` file.
143
144Takes `cwd?: URL | string` as options.
145
146```js
147import {isGitIgnored} from 'globby';
148
149const isIgnored = await isGitIgnored();
150
151console.log(isIgnored('some/file'));
152```
153
154### isGitIgnoredSync(options?)
155
156Returns a `(path: URL | string) => boolean` indicating whether a given path is ignored via a `.gitignore` file.
157
158Takes `cwd?: URL | string` as options.
159
160## Globbing patterns
161
162Just a quick overview.
163
164- `*` matches any number of characters, but not `/`
165- `?` matches a single character, but not `/`
166- `**` matches any number of characters, including `/`, as long as it's the only thing in a path part
167- `{}` allows for a comma-separated list of "or" expressions
168- `!` at the beginning of a pattern will negate the match
169
170[Various patterns and expected matches.](https://github.com/sindresorhus/multimatch/blob/main/test/test.js)
171
172## globby for enterprise
173
174Available as part of the Tidelift Subscription.
175
176The 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)
177
178## Related
179
180- [multimatch](https://github.com/sindresorhus/multimatch) - Match against a list instead of the filesystem
181- [matcher](https://github.com/sindresorhus/matcher) - Simple wildcard matching
182- [del](https://github.com/sindresorhus/del) - Delete files and directories
183- [make-dir](https://github.com/sindresorhus/make-dir) - Make a directory and its parents if needed