Is a faster
node-glob
alternative.
- π Fast by using Streams and Promises. Used readdir-enhanced and micromatch.
- π° User-friendly by supports multiple and negated patterns (
['*', '!*.md']
). - π¦ Rational, because it doesn't read excluded directories (
!**/node_modules
). - βοΈ Universal, because it supports Synchronous, Promise and Stream API.
- πΈ Economy, because it provides
fs.Stats
for matched path if you wanted.
If you want to thank me, or promote your Issue.
Sorry, but I have work and support for packages requires some time after work. I will be glad of your support and PR's.
$ npm install --save fast-glob
const fg = require('fast-glob');
fg(['src/**/*.js', '!src/**/*.spec.js']).then((entries) => console.log(entries));
fg.async(['src/**/*.js', '!src/**/*.spec.js']).then((entries) => console.log(entries));
const fg = require('fast-glob');
const entries = fg.sync(['src/**/*.js', '!src/**/*.spec.js']);
console.log(entries);
const fg = require('fast-glob');
const stream = fg.stream(['src/**/*.js', '!src/**/*.spec.js']);
const entries = [];
stream.on('data', (entry) => entries.push(entry));
stream.once('error', console.log);
stream.once('end', () => console.log(entries));
Returns a Promise<Array>
of matching entries.
- Type:
string|string[]
- Type:
Object
See options section for more detailed information.
Returns a Array
of matching entries.
Returns a ReadableStream
.
- Type:
string
- Default:
process.cwd()
The current working directory in which to search.
- Type:
number|boolean
- Default:
true
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.
- Type:
string[]
- Default:
[]
An array of glob patterns to exclude matches.
- Type:
boolean
- Default:
false
Allow patterns to match filenames starting with a period (files & directories), even if the pattern does not explicitly have a period in that spot.
- Type:
number|boolean
- Default:
false
Return fs.Stats
with path
property instead of file path.
- Type:
boolean
- Default:
true
Return only files.
- Type:
boolean
- Default:
false
Return only directories.
- Type:
Function
- Default:
null
Allows you to transform a path or fs.Stats
object before sending to the array.
const fg = require('fast-glob');
const entries1 = fg.sync(['**/*.scss']);
const entries2 = fg.sync(['**/*.scss'], { transform: (entry) => '_' + entry });
console.log(entries1); // ['a.scss', 'b.scss']
console.log(entries2); // ['_a.scss', '_b.scss']
If you are using TypeScript, you probably want to specify your own type of the returned array.
import * as fg from 'fast-glob';
interface IEntry {
path: string;
}
const entries: IEntry[] = fg.sync<IEntry>(['*.md'], {
transform: (entry) => typeof entry === 'string' ? { path: entry } : { path: entry.path }
// Will throw compilation error for non-IEntry types (boolean, for example)
});
You can use a negative pattern like this: !**/node_modules
. Also you can use ignore
option. Just look at the example below.
- first/
- second/
- file.md
- file.md
- second/
If you don't want to read the second
directory, you must write the following pattern: !**/second
.
fg.sync(['**/*.md', '!**/second']); // ['first/file.txt']
fg.sync(['**/*.md'], { ignore: '**/second' }); // ['first/file.txt']
β οΈ When you write!**/second/**
it means that the directory will be read, but all the entries will not be included in the results.
You have to understand that if you write the pattern to exclude directories, then the directory will not be read under any circumstances. But⦠you can specify a more meaningful pattern, which will be launched in parallel with the first.
fg.sync(['**/*.txt', '!**/second', 'first/second/**/*.txt']); // ['first/file.txt', 'first/second/file.txt']
However, be aware that it may not work as you expect in case where inside the second
directory there is a directory matching to the pattern for exluding directory. Yes, sounds complicated. Simpler: the second
directory inside the second
directory.
Not fully, because fast-glob
not implements all options of node-glob
. See table below.
node-glob | fast-glob |
---|---|
cwd |
cwd |
root |
β |
dot |
dot |
nomount |
β |
nosort |
β |
nounique |
β |
nobrace |
β |
noglobstar |
β |
noext |
β |
nocase |
β |
matchBase |
β |
nodir |
onlyFiles |
ignore |
ignore |
follow |
by default |
realpath |
β |
absolute |
β |
Tech specs:
- Processor: 2 β Ή E5-2660 (32 core)
- RAM: 64GB
- Disk: RAMDisk
You can see results here for latest release.
- readdir-enhanced β Fast functional replacement for
fs.readdir()
. - globby β User-friendly glob matching.
- node-glob β Β«StandardΒ» glob functionality for Node.js
- bash-glob β Bash-powered globbing for node.js.
- glob-stream β A Readable Stream interface over node-glob that used in the gulpjs.
See the Releases section of our GitHub project for changelogs for each release version.
This software is released under the terms of the MIT license.