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


  1. ``` shell
  2. npm install globby
  3. ```

Usage


  1. ``` null
  2. unicorn
  3. cake
  4. rainbow

  5. ```

  1. ``` js
  2. import {globby} from 'globby';

  3. const paths = await globby(['*', '!cake']);

  4. console.log(paths);
  5. //=> ['unicorn', 'rainbow']
  6. ```

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?)


Returns a `Promise`of matching paths.

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:

  1. ``` js
  2. import {globby} from 'globby';

  3. const paths = await globby('images', {
  4. expandDirectories: {
  5.   files: ['cat', 'unicorn', '*.jpg'],
  6.   extensions: ['png']
  7. }
  8. });

  9. console.log(paths);
  10. //=> ['cat.png', 'unicorn.png', 'cow.jpg', 'rainbow.jpg']
  11. ```

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:

  1. ``` js
  2. import {globbyStream} from 'globby';

  3. for await (const path of globbyStream('*.tmp')) {
  4. console.log(path);
  5. }
  6. ```

convertPathToPattern(path)


Convert a path to a pattern. Learn more.

generateGlobTasks(patterns, options?)


Returns an `Promise`in the format `{patterns: string[], options: Object}`, which can be passed as arguments to [fast-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.

generateGlobTasksSync(patterns, options?)


Returns an object]in the format {patterns: string[], options: Object}, which can be passed as arguments to [fast-glob . This is useful for other globbing-related packages.

Takes the same arguments as generateGlobTasks.

isDynamicPattern(patterns, options?)


Returns a booleanof whether there are any special glob characters in the patterns.

Note that the options affect the results.

This function is backed by fast-glob .

isGitIgnored(options?)


Returns a `Promise<(path: URL | string) => boolean>`indicating whether a given path is ignored via a `.gitignore`file.

Takes cwd?: URL | stringas options.

  1. ``` js
  2. import {isGitIgnored} from 'globby';

  3. const isIgnored = await isGitIgnored();

  4. console.log(isIgnored('some/file'));
  5. ```

isGitIgnoredSync(options?)


Returns a (path: URL | string) => booleanindicating whether a given path is ignored via a .gitignorefile.

Takes cwd?: URL | stringas options.

Globbing patterns


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.

Related


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