Is a faster
node-glob
alternative.
- π Fast by using Streams and Promises. Used readdir-enhanced and micromatch.
- π° User-friendly, since it 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
with an array of matching entries.
Returns an array of matching entries.
Returns a ReadableStream
when the data
event will be emitted with Entry
.
- Type:
string|string[]
This package does not respect the order of patterns. First, all the negative patterns are applied, and only then the positive patterns.
- Type:
Object
See options section for more detailed information.
Return a set of tasks based on provided patterns. All tasks satisfy the Task
interface:
interface Task {
/**
* Parent directory for all patterns inside this task.
*/
base: string;
/**
* Dynamic or static patterns are in this task.
*/
dynamic: boolean;
/**
* All patterns.
*/
patterns: string[];
/**
* Only positive patterns.
*/
positive: string[];
/**
* Only negative patterns without ! symbol.
*/
negative: string[];
}
The entry which can be a string
if the stats
option is disabled, otherwise fs.Stats
with two additional path
and depth
properties.
- Type:
number
- Default:
Infinity
The maximum number of concurrent calls to fs.readdir
.
See more more detailed description in the fs.walk
repository.
- 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.
For example, you have the following tree:
test
βββ one
βββ two
βββ index.js
π 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.
fg('test/**', { onlyFiles: false, deep: 0 });
// -> ['test/one']
fg('test/**', { onlyFiles: false, deep: 1 });
// -> ['test/one', 'test/one/two']
fg('**', { onlyFiles: false, cwd: 'test', deep: 0 });
// -> ['one']
fg('**', { onlyFiles: false, cwd: 'test', deep: 1 });
// -> ['one', 'one/two']
- 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:
boolean
- Default:
false
Return fs.Stats
with two additional path
and depth
properties instead of a string
.
- Type:
boolean
- Default:
true
Return only files.
- Type:
boolean
- Default:
false
Return only directories.
- Type:
boolean
- Default:
true
Indicates whether to traverse descendants of symbolic link directories.
π Also, if the
stats
option is specified, it tries to getfs.Stats
for symbolic link file.
- Type:
boolean
- Default:
true
Throw an error when symbolic link is broken if true
or safely return lstat
call if false
. Always false
when the stats
option is disabled.
π This option has no effect on errors when reading the symbolic link directory.
- Type:
boolean
- Default:
true
Prevent duplicate results.
- Type:
boolean
- Default:
false
Add a /
character to directory entries.
- Type:
boolean
- Default:
false
Return absolute paths for matched entries.
π Note that you need to use this option if you want to use absolute negative patterns like
${__dirname}/*.md
.
- Type:
boolean
- Default:
true
Enable expansion of brace patterns.
- Type:
boolean
- Default:
true
Enable matching with globstars (**
).
- Type:
boolean
- Default:
true
Enable extglob support, so that extglobs are regarded as literal characters.
- Type:
boolean
- Default:
true
Enable a case-sensitive mode for matching files.
- File System:
test/file.md
,test/File.md
- Case-sensitive for
test/file.*
pattern:test/file.md
- Case-insensitive for
test/file.*
pattern:test/file.md
,test/File.md
- Type:
boolean
- Default:
false
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
.
- Type:
boolean
- Default:
false
Suppress any errors from reader. Works only with Node.js 10.10+. Can be useful when the directory has entries with a special level of access.
- 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 IMyOwnEntry {
path: string;
}
const entries: IMyOwnEntry[] = fg.sync<IMyOwnEntry>(['*.md'], {
transform: (entry) => typeof entry === 'string' ? { path: entry } : { path: entry.path }
// Will throw compilation error for non-IMyOwnEntry types (boolean, for example)
});
- Type:
FileSystemAdapter
- Default:
fs.*
Custom implementation of methods for working with the file system.
interface FileSystemAdapter {
lstat: typeof fs.lstat;
stat: typeof fs.stat;
lstatSync: typeof fs.lstatSync;
statSync: typeof fs.statSync;
readdir: typeof fs.readdir;
readdirSync: typeof fs.readdirSync;
}
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.
first/
βββ file.md
βββ second
βββ file.txt
If you don't want to read the second
directory, you must write the following pattern: !**/second
or !**/second/**
.
fg.sync(['**/*.md', '!**/second']); // ['first/file.md']
fg.sync(['**/*.md'], { ignore: ['**/second/**'] }); // ['first/file.md']
β οΈ 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.
fg.sync(['(special-*file).txt']) // β [] for files: ['(special-*file).txt']
Refers to Bash. You need to escape special characters:
fg.sync(['\\(special-*file\\).txt']) // β ['(special-*file).txt']
Read more about Β«Matching special characters as literalsΒ».
Always use forward-slashes in glob expressions (patterns and ignore
option). Use backslashes for escaping characters. With the cwd
option use a convenient format.
Bad
[
'directory\\*',
path.join(process.cwd(), '**')
]
Good
[
'directory/*',
path.join(process.cwd(), '**').replace(/\\/g, '/')
]
π Use the
normalize-path
or theunixify
package to convert Windows-style path to a Unix-style path.
Read more about matching with backslashes.
You cannot use UNC paths as patterns (due to syntax), but you can use them as cwd
directory.
fg.sync('*', { cwd: '\\\\?\\C:\\Python27' /* or //?/C:/Python27 */ });
fg.sync('Python27/*', { cwd: '\\\\?\\C:\\' /* or //?/C:/ */ });
Not fully, because fast-glob
does not implement all options of node-glob
. See table below.
node-glob | fast-glob |
---|---|
cwd |
cwd |
root |
β |
dot |
dot |
nomount |
β |
mark |
markDirectories |
nosort |
β |
nounique |
unique |
nobrace |
braceExpansion |
noglobstar |
globstar |
noext |
extglob |
nocase |
caseSensitiveMatch |
matchBase |
matchbase |
nodir |
onlyFiles |
ignore |
ignore |
follow |
followSymbolicLinks |
realpath |
β |
absolute |
absolute |
Tech specs:
Server: Vultr Bare Metal
- Processor: E3-1270v6 (8 CPU)
- RAM: 32GB
- Disk: SSD
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.
- tiny-glob β Tiny and extremely fast library to match files and folders using glob patterns.
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.