Match files using the patterns the shell uses, like stars and stuff.
This is a glob implementation in JavaScript. It uses the minimatch
library to do its matching.

Install with npm
npm i glob
var glob = require("glob")
// options is optional
glob("**/*.js", options, function (er, files) {
// files is an array of filenames.
// If the `nonull` option is set, and nothing
// was found, then files is ["**/*.js"]
// er is an error object or null.
})
"Globs" are the patterns you type when you do stuff like ls *.js on
the command line, or put build/* in a .gitignore file.
Before parsing the path part patterns, braced sections are expanded
into a set. Braced sections start with { and end with }, with any
number of comma-delimited sections within. Braced sections may contain
slash characters, so a{/b/c,bcd} would expand into a/b/c and abcd.
The following characters have special magic meaning when used in a
path portion:
* Matches 0 or more characters in a single path portion? Matches 1 character[...] Matches a range of characters, similar to a RegExp range.! or ^ then it matches!(pattern|pattern|pattern) Matches anything that does not match?(pattern|pattern|pattern) Matches zero or one occurrence of the+(pattern|pattern|pattern) Matches one or more occurrences of the*(a|b|c) Matches zero or more occurrences of the patterns provided@(pattern|pat*|pat?erN) Matches exactly one of the patterns** If a "globstar" is alone in a path portion, then it matchesIf a file or directory path portion has a . as the first character,
then it will not match any glob pattern unless that pattern's
corresponding path part also has a . as its first character.
For example, the pattern a/.*/c would match the file at a/.b/c.
However the pattern a/*/c would not, because * does not start with
a dot character.
You can make glob treat dots as normal characters by settingdot:true in the options.
If you set matchBase:true in the options, and the pattern has no
slashes in it, then it will seek for any file anywhere in the tree
with a matching basename. For example, *.js would matchtest/simple/basic.js.
If no matching files are found, then an empty array is returned. This
differs from the shell, where the pattern itself is returned. For
example:
$ echo a*s*d*f
a*s*d*f
To get the bash-style behavior, set the nonull:true in the options.
man shman bash (Search for "Pattern Matching")man 3 fnmatchman 5 gitignoreReturns true if there are any special characters in the pattern, andfalse otherwise.
Note that the options affect the results. If noext:true is set in
the options object, then +(a|b) will not be considered a magic
pattern. If the pattern has a brace expansion, like a/{b/c,x/y}
then that is considered magical, unless nobrace:true is set in the
options.
pattern {String} Pattern to be matchedoptions {Object}cb {Function}err {Error | null}matches {Array<String>} filenames found matching the patternPerform an asynchronous glob search.
pattern {String} Pattern to be matchedoptions {Object}{Array<String>} filenames found matching the patternPerform a synchronous glob search.
Create a Glob object by instantiating the glob.Glob class.
var Glob = require("glob").Glob
var mg = new Glob(pattern, options, cb)
It's an EventEmitter, and starts walking the filesystem to find matches
immediately.
pattern {String} pattern to search foroptions {Object}cb {Function} Called when an error occurs, or matches are founderr {Error | null}matches {Array<String>} filenames found matching the patternNote that if the sync flag is set in the options, then matches will
be immediately available on the g.found member.
minimatch The minimatch object that the glob uses.options The options object passed in.aborted Boolean which is set to true when calling abort(). Therecache Convenience object. Each field has the following possiblefalse - Path does not existtrue - Path exists'FILE' - Path exists, and is not a directory'DIR' - Path exists, and is a directory[file, entries, ...] - Path exists, is a directory, and thefs.readdirstatCache Cache of fs.stat results, to prevent statting the samesymlinks A record of which paths are symbolic links, which is** patterns.realpathCache An optional object which is passed to fs.realpathend When the matching is finished, this is emitted with all thenonull option is set, and no match was found,matches list contains the original pattern. The matchesnosort flag is set.match Every time a match is found, this is emitted with the specificerror Emitted when an unexpected error is encountered, or wheneveroptions.strict is set.abort When abort() is called, this event is raised.pause Temporarily stop the searchresume Resume the searchabort Stop the search foreverAll the options that can be passed to Minimatch can also be passed to
Glob to change pattern matching behavior. Also, some have been added,
or have glob-specific ramifications.
All options are false by default, unless otherwise noted.
All options are added to the Glob object, as well.
If you are running many glob operations, you can pass a Glob object
as the options argument to a subsequent operation to shortcut somestat and readdir calls. At the very least, you may pass in sharedsymlinks, statCache, realpathCache, and cache options, so that
parallel glob operations will be sped up by sharing information about
the filesystem.
cwd The current working directory in which to search. Defaultsprocess.cwd().root The place where patterns starting with / will be mountedpath.resolve(options.cwd, "/") (/ on UnixC:\ or some such on Windows.)dot Include .dot files in normal matches and globstar matches.nomount By default, a pattern starting with a forward-slash will bemark Add a / character to directory matches. Note that thisnosort Don't sort the results.stat Set to true to stat all results. This reduces performancereaddir is presumedsilent When an unusual error is encountered when attempting tosilent option to true to suppress these warnings.strict When an unusual error is encountered when attempting tostrict option to raise an error in thesecache See cache property above. Pass in a previously generatedstatCache A cache of results of filesystem information, to preventsymlinks A cache of known symbolic links. You may pass in asymlinks object to save lstat calls when** matches.sync DEPRECATED: use glob.sync(pattern, opts) instead.nounique In some cases, brace-expanded patterns can result in thenonull Set to never return an empty set, instead returning a setdebug Set to enable debug logging in minimatch and glob.nobrace Do not expand {a,b} and {1..3} brace sets.noglobstar Do not match ** against multiple filenames. (Ie,* instead.)noext Do not match +(a|b) "extglob" patterns.nocase Perform a case-insensitive match. Note: onstat and readdir will not raise errors.matchBase Perform a basename-only match if the pattern does not*.js would be treated as**/*.js, matching all js files in all directories.nodir Do not match directories, only files. (Note: to match/ at the end of the pattern.)ignore Add a pattern or an array of glob patterns to exclude matches.ignore patterns are always in dot:true mode, regardlessfollow Follow symlinked directories when expanding ** patterns.realpath Set to true to call fs.realpath on all of the results.absolute Set to true to always receive absolute paths for matchedrealpath, this also affects the values returned inmatch event.fs File-system object with Node's fs API. By default, the built-infs module will be used. Set to a volume provided by a library likememfs to avoid using the "real" file-system.While strict compliance with the existing standards is a worthwhile
goal, some discrepancies exist between node-glob and other
implementations, and are intentional.
The double-star character ** is supported by default, unless thenoglobstar flag is set. This is supported in the manner of bsdglob
and bash 4.3, where ** only has special significance if it is the only
thing in a path part. That is, a/**/b will match a/x/y/b, buta/**b will not.
Note that symlinked directories are not crawled as part of a **,
though their contents may match against subsequent portions of the
pattern. This prevents infinite loops and duplicates and the like.
If an escaped pattern has no matches, and the nonull flag is set,
then glob returns the pattern as-provided, rather than
interpreting the character escapes. For example,glob.match([], "\\*a\\?") will return "\\*a\\?" rather than"*a?". This is akin to setting the nullglob option in bash, except
that it does not resolve escaped pattern characters.
If brace expansion is not disabled, then it is performed before any
other interpretation of the glob pattern. Thus, a pattern like+(a|{b),c)}, which would not be valid in bash or zsh, is expanded
first into the set of +(a|b) and +(a|c), and those patterns are
checked for validity. Since those two are valid, matching proceeds.
Previously, this module let you mark a pattern as a "comment" if it
started with a # character, or a "negated" pattern if it started
with a ! character.
These options were deprecated in version 5, and removed in version 6.
To specify things that should not match, use the ignore option.
Please only use forward-slashes in glob expressions.
Though windows uses either / or \ as its path separator, only /
characters are used by this glob implementation. You must use
forward-slashes only in glob expressions. Back-slashes will always
be interpreted as escape characters, not path separators.
Results from absolute patterns such as /foo/* are mounted onto the
root setting using path.join. On windows, this will by default result
in /foo/* matching C:\foo\bar.txt.
Glob searching, by its very nature, is susceptible to race conditions,
since it relies on directory walking and such.
As a result, it is possible that a file that exists when glob looks for
it may have been deleted or modified by the time it returns the result.
As part of its internal implementation, this program caches all stat
and readdir calls that it makes, in order to cut down on system
overhead. However, this also makes it even more susceptible to races,
especially if the cache or statCache objects are reused between glob
calls.
Users are thus advised not to use a glob result as a guarantee of
filesystem state in the face of rapid changes. For the vast majority
of operations, this is never a problem.
Glob's logo was created by Tanya Brassie. Logo files can be found here.
The logo is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Any change to behavior (including bugfixes) must come with a test.
Patches that fail tests or reduce performance will be rejected.
# to run tests
npm test
# to re-generate test fixtures
npm run test-regen
# to benchmark against bash/zsh
npm run bench
# to profile javascript
npm run prof
