Globby
User-friendly glob matching
README
markdown:266
globby
User-friendly glob matching
Based on fast-glob but adds a bunch of useful features.
Features
Promise API
Multiple patterns
Negated patterns: ['foo*', '!foobar']
Expands directories: foo→ `foo/*/`
Supports .gitignoreand similar ignore config files
Supports URLas cwd
Install
- ``` shell
- npm install globby
- ```
Usage
- ``` null
- ├── unicorn
- ├── cake
- └── rainbow
- ```
- ``` js
- import {globby} from 'globby';
- const paths = await globby(['*', '!cake']);
- console.log(paths);
- //=> ['unicorn', 'rainbow']
- ```
API
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 use path.posix.join()instead of path.join().
globby(patterns, options?)
patterns
Type: string | string[]
See supported minimatchpatterns .
options
Type: object
See the fast-glob options in addition to the ones below.
expandDirectories
Type: boolean | string[] | objectDefault: true
If set to true, globbywill automatically glob directories for you. If you define an Arrayit will only glob files that matches the patterns inside the Array. You can also define an objectwith filesand extensionslike below:
- ``` js
- import {globby} from 'globby';
- const paths = await globby('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 to false, you won't get back matched directories unless you set onlyFiles: false.
gitignore
Type: booleanDefault: false
Respect ignore patterns in .gitignorefiles that apply to the globbed files.
ignoreFiles
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 the gitignoreoption, allowing you to find ignore files with a compatible syntax . For instance, this works with Babel's .babelignore, Prettier's .prettierignore, or ESLint's .eslintignorefiles.
globbySync(patterns, options?)
Returns string[]of matching paths.
globbyStream(patterns, options?)
Returns a stream.Readable of matching paths.
For example, loop over glob matches in a for await...of loop like this:
- ``` js
- import {globbyStream} from 'globby';
- for await (const path of globbyStream('*.tmp')) {
- console.log(path);
- }
- ```
convertPathToPattern(path)
Convert a path to a pattern. Learn more.