User-friendly glob matching
Based onfast-glob
but adds a bunch of useful features.
- Promise API
- Multiple patterns
- Negated patterns:
['foo*', '!foobar']
- Expands directories:
foo
→foo/**/*
- Supports
.gitignore
and similar ignore config files - Supports
URL
ascwd
npm install globby
├── unicorn
├── cake
└── rainbow
import{globby}from'globby';
constpaths=awaitglobby(['*','!cake']);
console.log(paths);
//=> ['unicorn', 'rainbow']
Note 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 usepath.posix.join()
instead ofpath.join()
.
Returns aPromise<string[]>
of matching paths.
Type:string | string[]
See supportedminimatch
patterns.
Type:object
See thefast-glob
optionsin addition to the ones below.
Type:boolean | string[] | object
Default:true
If set totrue
,globby
will automatically glob directories for you. If you define anArray
it will only glob files that matches the patterns inside theArray
.You can also define anobject
withfiles
andextensions
like below:
import{globby}from'globby';
constpaths=awaitglobby('images',{
expandDirectories:{
files:['cat','unicorn','*.jpg'],
extensions:['png']
}
});
console.log(paths);
//=> ['cat.png', 'unicorn.png', 'cow.jpg', 'rainbow.jpg']
Note that if you set this option tofalse
,you won't get back matched directories unless you setonlyFiles: false
.
Type:boolean
Default:false
Respect ignore patterns in.gitignore
files that apply to the globbed files.
Type:string | string[]
Default:undefined
Glob patterns to look for ignore files, which are then used to ignore globbed files.
This is a more generic form of thegitignore
option, allowing you to find ignore files with acompatible syntax.For instance, this works with Babel's.babelignore
,Prettier's.prettierignore
,or ESLint's.eslintignore
files.
Returnsstring[]
of matching paths.
Returns astream.Readable
of matching paths.
For example, loop over glob matches in afor await...of
looplike this:
import{globbyStream}from'globby';
forawait(constpathofglobbyStream('*.tmp')){
console.log(path);
}
Convert a path to a pattern.Learn more.
Returns anPromise<object[]>
in the format{patterns: string[], options: Object}
,which can be passed as arguments tofast-glob
.This is useful for other globbing-related packages.
Note 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.
Returns anobject[]
in the format{patterns: string[], options: Object}
,which can be passed as arguments tofast-glob
.This is useful for other globbing-related packages.
Takes the same arguments asgenerateGlobTasks
.
Returns aboolean
of whether there are any special glob characters in thepatterns
.
Note that the options affect the results.
This function is backed byfast-glob
.
Returns aPromise<(path: URL | string) => boolean>
indicating whether a given path is ignored via a.gitignore
file.
Takescwd?: URL | string
as options.
import{isGitIgnored}from'globby';
constisIgnored=awaitisGitIgnored();
console.log(isIgnored('some/file'));
Returns a(path: URL | string) => boolean
indicating whether a given path is ignored via a.gitignore
file.
Takescwd?: URL | string
as options.
Just a quick overview.
*
matches any number of characters, but not/
?
matches a single character, but not/
**
matches any number of characters, including/
,as long as it's the only thing in a path part{}
allows for a comma-separated list of "or" expressions!
at the beginning of a pattern will negate the match
Various patterns and expected matches.
- multimatch- Match against a list instead of the filesystem
- matcher- Simple wildcard matching
- del- Delete files and directories
- make-dir- Make a directory and its parents if needed