3 > Is a faster [`node-glob`](https://github.com/isaacs/node-glob) alternative.
7 * :rocket: Fast by using Streams and Promises. Used [readdir-enhanced](https://github.com/BigstickCarpet/readdir-enhanced) and [micromatch](https://github.com/jonschlinkert/micromatch).
8 * :beginner: User-friendly, since it supports multiple and negated patterns (`['*', '!*.md']`).
9 * :vertical_traffic_light: Rational, because it doesn't read excluded directories (`!**/node_modules/**`).
10 * :gear: Universal, because it supports Synchronous, Promise and Stream API.
11 * :money_with_wings: Economy, because it provides `fs.Stats` for matched path if you wanted.
15 If you want to thank me, or promote your Issue.
17 [![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://paypal.me/mrmlnc)
19 > Sorry, but I have work and support for packages requires some time after work. I will be glad of your support and PR's.
24 $ npm install --save fast-glob
32 const fg = require('fast-glob');
34 fg(['src/**/*.js', '!src/**/*.spec.js']).then((entries) => console.log(entries));
35 fg.async(['src/**/*.js', '!src/**/*.spec.js']).then((entries) => console.log(entries));
41 const fg = require('fast-glob');
43 const entries = fg.sync(['src/**/*.js', '!src/**/*.spec.js']);
50 const fg = require('fast-glob');
52 const stream = fg.stream(['src/**/*.js', '!src/**/*.spec.js']);
56 stream.on('data', (entry) => entries.push(entry));
57 stream.once('error', console.log);
58 stream.once('end', () => console.log(entries));
63 ### fg(patterns, [options])
64 ### fg.async(patterns, [options])
66 Returns a `Promise` with an array of matching [entries](#entry).
68 ### fg.sync(patterns, [options])
70 Returns an array of matching [entries](#entry).
72 ### fg.stream(patterns, [options])
74 Returns a [`ReadableStream`](https://nodejs.org/api/stream.html#stream_readable_streams) when the `data` event will be emitted with [`Entry`](#entry).
78 * Type: `string|string[]`
80 This package does not respect the order of patterns. First, all the negative patterns are applied, and only then the positive patterns.
86 See [options](#options-1) section for more detailed information.
88 ### fg.generateTasks(patterns, [options])
90 Return a set of tasks based on provided patterns. All tasks satisfy the `Task` interface:
95 * Parent directory for all patterns inside this task.
99 * Dynamic or static patterns are in this task.
107 * Only positive patterns.
111 * Only negative patterns without ! symbol.
119 The entry which can be a `string` if the [`stats`](#stats) option is disabled, otherwise `fs.Stats` with two additional `path` and `depth` properties.
126 * Default: `process.cwd()`
128 The current working directory in which to search.
132 * Type: `number|boolean`
135 The deep option can be set to `true` to traverse the entire directory structure, or it can be set to a *number* to only traverse that many levels deep.
137 For example, you have the following tree:
146 > :book: If you specify a pattern with some base directory, this directory will not participate in the calculation of the depth of the found directories. Think of it as a `cwd` option.
149 fg('test/**', { onlyFiles: false, deep: 0 });
151 fg('test/**', { onlyFiles: false, deep: 1 });
152 // -> ['test/one', 'test/one/two']
154 fg('**', { onlyFiles: false, cwd: 'test', deep: 0 });
156 fg('**', { onlyFiles: false, cwd: 'test', deep: 1 });
157 // -> ['one', 'one/two']
165 An array of glob patterns to exclude matches.
172 Allow patterns to match filenames starting with a period (files & directories), even if the pattern does not explicitly have a period in that spot.
179 Return `fs.Stats` with two additional `path` and `depth` properties instead of a `string`.
193 Return only directories.
195 #### followSymlinkedDirectories
200 Follow symlinked directories when expanding `**` patterns.
207 Prevent duplicate results.
214 Add a `/` character to directory entries.
221 Return absolute paths for matched entries.
223 > :book: Note that you need to use this option if you want to use absolute negative patterns like `${__dirname}/*.md`.
230 Disable expansion of brace patterns (`{a,b}`, `{1..3}`).
237 The [`nobrace`](#nobrace) option without double-negation. This option has a higher priority then `nobrace`.
244 Disable matching with globstars (`**`).
251 The [`noglobstar`](#noglobstar) option without double-negation. This option has a higher priority then `noglobstar`.
258 Disable extglob support (patterns like `+(a|b)`), so that extglobs are regarded as literal characters.
265 The [`noext`](#noext) option without double-negation. This option has a higher priority then `noext`.
272 Disable a [case-sensitive](https://en.wikipedia.org/wiki/Case_sensitivity) mode for matching files.
276 * File System: `test/file.md`, `test/File.md`
277 * Case-sensitive for `test/file.*` pattern (`false`): `test/file.md`
278 * Case-insensitive for `test/file.*` pattern (`true`): `test/file.md`, `test/File.md`
285 The [`nocase`](#nocase) option without double-negation. This option has a higher priority then `nocase`.
292 Allow glob patterns without slashes to match a file path based on its basename. For example, `a?b` would match the path `/xyz/123/acb`, but not `/xyz/acb/123`.
299 Allows you to transform a path or `fs.Stats` object before sending to the array.
302 const fg = require('fast-glob');
304 const entries1 = fg.sync(['**/*.scss']);
305 const entries2 = fg.sync(['**/*.scss'], { transform: (entry) => '_' + entry });
307 console.log(entries1); // ['a.scss', 'b.scss']
308 console.log(entries2); // ['_a.scss', '_b.scss']
311 If you are using **TypeScript**, you probably want to specify your own type of the returned array.
314 import * as fg from 'fast-glob';
316 interface IMyOwnEntry {
320 const entries: IMyOwnEntry[] = fg.sync<IMyOwnEntry>(['*.md'], {
321 transform: (entry) => typeof entry === 'string' ? { path: entry } : { path: entry.path }
322 // Will throw compilation error for non-IMyOwnEntry types (boolean, for example)
326 ## How to exclude directory from reading?
328 You can use a negative pattern like this: `!**/node_modules` or `!**/node_modules/**`. Also you can use `ignore` option. Just look at the example below.
337 If you don't want to read the `second` directory, you must write the following pattern: `!**/second` or `!**/second/**`.
340 fg.sync(['**/*.md', '!**/second']); // ['first/file.txt']
341 fg.sync(['**/*.md'], { ignore: '**/second/**' }); // ['first/file.txt']
344 > :warning: When you write `!**/second/**/*` it means that the directory will be **read**, but all the entries will not be included in the results.
346 You have to understand that if you write the pattern to exclude directories, then the directory will not be read under any circumstances.
348 ## How to use UNC path?
350 You cannot use UNC paths as patterns (due to syntax), but you can use them as `cwd` directory.
353 fg.sync('*', { cwd: '\\\\?\\C:\\Python27' /* or //?/C:/Python27 */ });
354 fg.sync('Python27/*', { cwd: '\\\\?\\C:\\' /* or //?/C:/ */ });
357 ## Compatible with `node-glob`?
359 Not fully, because `fast-glob` does not implement all options of `node-glob`. See table below.
361 | node-glob | fast-glob |
362 | :----------: | :-------: |
363 | `cwd` | [`cwd`](#cwd) |
365 | `dot` | [`dot`](#dot) |
367 | `mark` | [`markDirectories`](#markdirectories) |
369 | `nounique` | [`unique`](#unique) |
370 | `nobrace` | [`nobrace`](#nobrace) or [`brace`](#brace) |
371 | `noglobstar` | [`noglobstar`](#noglobstar) or [`globstar`](#globstar) |
372 | `noext` | [`noext`](#noext) or [`extension`](#extension) |
373 | `nocase` | [`nocase`](#nocase) or [`case`](#case) |
374 | `matchBase` | [`matchbase`](#matchbase) |
375 | `nodir` | [`onlyFiles`](#onlyfiles) |
376 | `ignore` | [`ignore`](#ignore) |
377 | `follow` | [`followSymlinkedDirectories`](#followsymlinkeddirectories) |
379 | `absolute` | [`absolute`](#absolute) |
385 Server: [Vultr Bare Metal](https://www.vultr.com/pricing/baremetal)
387 * Processor: E3-1270v6 (8 CPU)
391 You can see results [here](https://gist.github.com/mrmlnc/f06246b197f53c356895fa35355a367c) for latest release.
395 * [readdir-enhanced](https://github.com/BigstickCarpet/readdir-enhanced) – Fast functional replacement for `fs.readdir()`.
396 * [globby](https://github.com/sindresorhus/globby) – User-friendly glob matching.
397 * [node-glob](https://github.com/isaacs/node-glob) – «Standard» glob functionality for Node.js
398 * [bash-glob](https://github.com/micromatch/bash-glob) – Bash-powered globbing for node.js.
399 * [glob-stream](https://github.com/gulpjs/glob-stream) – A Readable Stream interface over node-glob that used in the [gulpjs](https://github.com/gulpjs/gulp).
400 * [tiny-glob](https://github.com/terkelg/tiny-glob) – Tiny and extremely fast library to match files and folders using glob patterns.
404 See the [Releases section of our GitHub project](https://github.com/mrmlnc/fast-glob/releases) for changelogs for each release version.
408 This software is released under the terms of the MIT license.