Skip to content

sindresorhus/globby

Repository files navigation

globby

User-friendly glob matching

Based onfast-globbut adds a bunch of useful features.

Features

  • Promise API
  • Multiple patterns
  • Negated patterns:['foo*', '!foobar']
  • Expands directories:foofoo/**/*
  • Supports.gitignoreand similar ignore config files
  • SupportsURLascwd

Install

npm install globby

Usage

├── unicorn
├── cake
└── rainbow
import{globby}from'globby';

constpaths=awaitglobby(['*','!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 usepath.posix.join()instead ofpath.join().

globby(patterns, options?)

Returns aPromise<string[]>of matching paths.

patterns

Type:string | string[]

See supportedminimatchpatterns.

options

Type:object

See thefast-globoptionsin addition to the ones below.

expandDirectories

Type:boolean | string[] | object
Default:true

If set totrue,globbywill automatically glob directories for you. If you define anArrayit will only glob files that matches the patterns inside theArray.You can also define anobjectwithfilesandextensionslike 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.

gitignore

Type:boolean
Default: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 thegitignoreoption, allowing you to find ignore files with acompatible syntax.For instance, this works with Babel's.babelignore,Prettier's.prettierignore,or ESLint's.eslintignorefiles.

globbySync(patterns, options?)

Returnsstring[]of matching paths.

globbyStream(patterns, options?)

Returns astream.Readableof matching paths.

For example, loop over glob matches in afor await...oflooplike this:

import{globbyStream}from'globby';

forawait(constpathofglobbyStream('*.tmp')){
console.log(path);
}

convertPathToPattern(path)

Convert a path to a pattern.Learn more.

generateGlobTasks(patterns, options?)

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.

generateGlobTasksSync(patterns, options?)

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.

isDynamicPattern(patterns, options?)

Returns abooleanof whether there are any special glob characters in thepatterns.

Note that the options affect the results.

This function is backed byfast-glob.

isGitIgnored(options?)

Returns aPromise<(path: URL | string) => boolean>indicating whether a given path is ignored via a.gitignorefile.

Takescwd?: URL | stringas options.

import{isGitIgnored}from'globby';

constisIgnored=awaitisGitIgnored();

console.log(isIgnored('some/file'));

isGitIgnoredSync(options?)

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

Takescwd?: 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