Initial commit
[yaffs-website] / node_modules / glob-stream / node_modules / glob / README.md
1 [![Build Status](https://travis-ci.org/isaacs/node-glob.svg?branch=master)](https://travis-ci.org/isaacs/node-glob/) [![Dependency Status](https://david-dm.org/isaacs/node-glob.svg)](https://david-dm.org/isaacs/node-glob) [![devDependency Status](https://david-dm.org/isaacs/node-glob/dev-status.svg)](https://david-dm.org/isaacs/node-glob#info=devDependencies) [![optionalDependency Status](https://david-dm.org/isaacs/node-glob/optional-status.svg)](https://david-dm.org/isaacs/node-glob#info=optionalDependencies)
2
3 # Glob
4
5 Match files using the patterns the shell uses, like stars and stuff.
6
7 This is a glob implementation in JavaScript.  It uses the `minimatch`
8 library to do its matching.
9
10 ![](oh-my-glob.gif)
11
12 ## Usage
13
14 ```javascript
15 var glob = require("glob")
16
17 // options is optional
18 glob("**/*.js", options, function (er, files) {
19   // files is an array of filenames.
20   // If the `nonull` option is set, and nothing
21   // was found, then files is ["**/*.js"]
22   // er is an error object or null.
23 })
24 ```
25
26 ## Glob Primer
27
28 "Globs" are the patterns you type when you do stuff like `ls *.js` on
29 the command line, or put `build/*` in a `.gitignore` file.
30
31 Before parsing the path part patterns, braced sections are expanded
32 into a set.  Braced sections start with `{` and end with `}`, with any
33 number of comma-delimited sections within.  Braced sections may contain
34 slash characters, so `a{/b/c,bcd}` would expand into `a/b/c` and `abcd`.
35
36 The following characters have special magic meaning when used in a
37 path portion:
38
39 * `*` Matches 0 or more characters in a single path portion
40 * `?` Matches 1 character
41 * `[...]` Matches a range of characters, similar to a RegExp range.
42   If the first character of the range is `!` or `^` then it matches
43   any character not in the range.
44 * `!(pattern|pattern|pattern)` Matches anything that does not match
45   any of the patterns provided.
46 * `?(pattern|pattern|pattern)` Matches zero or one occurrence of the
47   patterns provided.
48 * `+(pattern|pattern|pattern)` Matches one or more occurrences of the
49   patterns provided.
50 * `*(a|b|c)` Matches zero or more occurrences of the patterns provided
51 * `@(pattern|pat*|pat?erN)` Matches exactly one of the patterns
52   provided
53 * `**` If a "globstar" is alone in a path portion, then it matches
54   zero or more directories and subdirectories searching for matches.
55   It does not crawl symlinked directories.
56
57 ### Dots
58
59 If a file or directory path portion has a `.` as the first character,
60 then it will not match any glob pattern unless that pattern's
61 corresponding path part also has a `.` as its first character.
62
63 For example, the pattern `a/.*/c` would match the file at `a/.b/c`.
64 However the pattern `a/*/c` would not, because `*` does not start with
65 a dot character.
66
67 You can make glob treat dots as normal characters by setting
68 `dot:true` in the options.
69
70 ### Basename Matching
71
72 If you set `matchBase:true` in the options, and the pattern has no
73 slashes in it, then it will seek for any file anywhere in the tree
74 with a matching basename.  For example, `*.js` would match
75 `test/simple/basic.js`.
76
77 ### Negation
78
79 The intent for negation would be for a pattern starting with `!` to
80 match everything that *doesn't* match the supplied pattern.  However,
81 the implementation is weird, and for the time being, this should be
82 avoided.  The behavior will change or be deprecated in version 5.
83
84 ### Empty Sets
85
86 If no matching files are found, then an empty array is returned.  This
87 differs from the shell, where the pattern itself is returned.  For
88 example:
89
90     $ echo a*s*d*f
91     a*s*d*f
92
93 To get the bash-style behavior, set the `nonull:true` in the options.
94
95 ### See Also:
96
97 * `man sh`
98 * `man bash` (Search for "Pattern Matching")
99 * `man 3 fnmatch`
100 * `man 5 gitignore`
101 * [minimatch documentation](https://github.com/isaacs/minimatch)
102
103 ## glob.hasMagic(pattern, [options])
104
105 Returns `true` if there are any special characters in the pattern, and
106 `false` otherwise.
107
108 Note that the options affect the results.  If `noext:true` is set in
109 the options object, then `+(a|b)` will not be considered a magic
110 pattern.  If the pattern has a brace expansion, like `a/{b/c,x/y}`
111 then that is considered magical, unless `nobrace:true` is set in the
112 options.
113
114 ## glob(pattern, [options], cb)
115
116 * `pattern` {String} Pattern to be matched
117 * `options` {Object}
118 * `cb` {Function}
119   * `err` {Error | null}
120   * `matches` {Array<String>} filenames found matching the pattern
121
122 Perform an asynchronous glob search.
123
124 ## glob.sync(pattern, [options])
125
126 * `pattern` {String} Pattern to be matched
127 * `options` {Object}
128 * return: {Array<String>} filenames found matching the pattern
129
130 Perform a synchronous glob search.
131
132 ## Class: glob.Glob
133
134 Create a Glob object by instantiating the `glob.Glob` class.
135
136 ```javascript
137 var Glob = require("glob").Glob
138 var mg = new Glob(pattern, options, cb)
139 ```
140
141 It's an EventEmitter, and starts walking the filesystem to find matches
142 immediately.
143
144 ### new glob.Glob(pattern, [options], [cb])
145
146 * `pattern` {String} pattern to search for
147 * `options` {Object}
148 * `cb` {Function} Called when an error occurs, or matches are found
149   * `err` {Error | null}
150   * `matches` {Array<String>} filenames found matching the pattern
151
152 Note that if the `sync` flag is set in the options, then matches will
153 be immediately available on the `g.found` member.
154
155 ### Properties
156
157 * `minimatch` The minimatch object that the glob uses.
158 * `options` The options object passed in.
159 * `aborted` Boolean which is set to true when calling `abort()`.  There
160   is no way at this time to continue a glob search after aborting, but
161   you can re-use the statCache to avoid having to duplicate syscalls.
162 * `statCache` Collection of all the stat results the glob search
163   performed.
164 * `cache` Convenience object.  Each field has the following possible
165   values:
166   * `false` - Path does not exist
167   * `true` - Path exists
168   * `'DIR'` - Path exists, and is not a directory
169   * `'FILE'` - Path exists, and is a directory
170   * `[file, entries, ...]` - Path exists, is a directory, and the
171     array value is the results of `fs.readdir`
172 * `statCache` Cache of `fs.stat` results, to prevent statting the same
173   path multiple times.
174 * `symlinks` A record of which paths are symbolic links, which is
175   relevant in resolving `**` patterns.
176 * `realpathCache` An optional object which is passed to `fs.realpath`
177   to minimize unnecessary syscalls.  It is stored on the instantiated
178   Glob object, and may be re-used.
179
180 ### Events
181
182 * `end` When the matching is finished, this is emitted with all the
183   matches found.  If the `nonull` option is set, and no match was found,
184   then the `matches` list contains the original pattern.  The matches
185   are sorted, unless the `nosort` flag is set.
186 * `match` Every time a match is found, this is emitted with the matched.
187 * `error` Emitted when an unexpected error is encountered, or whenever
188   any fs error occurs if `options.strict` is set.
189 * `abort` When `abort()` is called, this event is raised.
190
191 ### Methods
192
193 * `pause` Temporarily stop the search
194 * `resume` Resume the search
195 * `abort` Stop the search forever
196
197 ### Options
198
199 All the options that can be passed to Minimatch can also be passed to
200 Glob to change pattern matching behavior.  Also, some have been added,
201 or have glob-specific ramifications.
202
203 All options are false by default, unless otherwise noted.
204
205 All options are added to the Glob object, as well.
206
207 If you are running many `glob` operations, you can pass a Glob object
208 as the `options` argument to a subsequent operation to shortcut some
209 `stat` and `readdir` calls.  At the very least, you may pass in shared
210 `symlinks`, `statCache`, `realpathCache`, and `cache` options, so that
211 parallel glob operations will be sped up by sharing information about
212 the filesystem.
213
214 * `cwd` The current working directory in which to search.  Defaults
215   to `process.cwd()`.
216 * `root` The place where patterns starting with `/` will be mounted
217   onto.  Defaults to `path.resolve(options.cwd, "/")` (`/` on Unix
218   systems, and `C:\` or some such on Windows.)
219 * `dot` Include `.dot` files in normal matches and `globstar` matches.
220   Note that an explicit dot in a portion of the pattern will always
221   match dot files.
222 * `nomount` By default, a pattern starting with a forward-slash will be
223   "mounted" onto the root setting, so that a valid filesystem path is
224   returned.  Set this flag to disable that behavior.
225 * `mark` Add a `/` character to directory matches.  Note that this
226   requires additional stat calls.
227 * `nosort` Don't sort the results.
228 * `stat` Set to true to stat *all* results.  This reduces performance
229   somewhat, and is completely unnecessary, unless `readdir` is presumed
230   to be an untrustworthy indicator of file existence.
231 * `silent` When an unusual error is encountered when attempting to
232   read a directory, a warning will be printed to stderr.  Set the
233   `silent` option to true to suppress these warnings.
234 * `strict` When an unusual error is encountered when attempting to
235   read a directory, the process will just continue on in search of
236   other matches.  Set the `strict` option to raise an error in these
237   cases.
238 * `cache` See `cache` property above.  Pass in a previously generated
239   cache object to save some fs calls.
240 * `statCache` A cache of results of filesystem information, to prevent
241   unnecessary stat calls.  While it should not normally be necessary
242   to set this, you may pass the statCache from one glob() call to the
243   options object of another, if you know that the filesystem will not
244   change between calls.  (See "Race Conditions" below.)
245 * `symlinks` A cache of known symbolic links.  You may pass in a
246   previously generated `symlinks` object to save `lstat` calls when
247   resolving `**` matches.
248 * `sync` DEPRECATED: use `glob.sync(pattern, opts)` instead.
249 * `nounique` In some cases, brace-expanded patterns can result in the
250   same file showing up multiple times in the result set.  By default,
251   this implementation prevents duplicates in the result set.  Set this
252   flag to disable that behavior.
253 * `nonull` Set to never return an empty set, instead returning a set
254   containing the pattern itself.  This is the default in glob(3).
255 * `debug` Set to enable debug logging in minimatch and glob.
256 * `nobrace` Do not expand `{a,b}` and `{1..3}` brace sets.
257 * `noglobstar` Do not match `**` against multiple filenames.  (Ie,
258   treat it as a normal `*` instead.)
259 * `noext` Do not match `+(a|b)` "extglob" patterns.
260 * `nocase` Perform a case-insensitive match.  Note: on
261   case-insensitive filesystems, non-magic patterns will match by
262   default, since `stat` and `readdir` will not raise errors.
263 * `matchBase` Perform a basename-only match if the pattern does not
264   contain any slash characters.  That is, `*.js` would be treated as
265   equivalent to `**/*.js`, matching all js files in all directories.
266 * `nonegate` Suppress `negate` behavior.  (See below.)
267 * `nocomment` Suppress `comment` behavior.  (See below.)
268 * `nonull` Return the pattern when no matches are found.
269 * `nodir` Do not match directories, only files.  (Note: to match
270   *only* directories, simply put a `/` at the end of the pattern.)
271 * `ignore` Add a pattern or an array of patterns to exclude matches.
272 * `follow` Follow symlinked directories when expanding `**` patterns.
273   Note that this can result in a lot of duplicate references in the
274   presence of cyclic links.
275 * `realpath` Set to true to call `fs.realpath` on all of the results.
276   In the case of a symlink that cannot be resolved, the full absolute
277   path to the matched entry is returned (though it will usually be a
278   broken symlink)
279
280 ## Comparisons to other fnmatch/glob implementations
281
282 While strict compliance with the existing standards is a worthwhile
283 goal, some discrepancies exist between node-glob and other
284 implementations, and are intentional.
285
286 If the pattern starts with a `!` character, then it is negated.  Set the
287 `nonegate` flag to suppress this behavior, and treat leading `!`
288 characters normally.  This is perhaps relevant if you wish to start the
289 pattern with a negative extglob pattern like `!(a|B)`.  Multiple `!`
290 characters at the start of a pattern will negate the pattern multiple
291 times.
292
293 If a pattern starts with `#`, then it is treated as a comment, and
294 will not match anything.  Use `\#` to match a literal `#` at the
295 start of a line, or set the `nocomment` flag to suppress this behavior.
296
297 The double-star character `**` is supported by default, unless the
298 `noglobstar` flag is set.  This is supported in the manner of bsdglob
299 and bash 4.3, where `**` only has special significance if it is the only
300 thing in a path part.  That is, `a/**/b` will match `a/x/y/b`, but
301 `a/**b` will not.
302
303 Note that symlinked directories are not crawled as part of a `**`,
304 though their contents may match against subsequent portions of the
305 pattern.  This prevents infinite loops and duplicates and the like.
306
307 If an escaped pattern has no matches, and the `nonull` flag is set,
308 then glob returns the pattern as-provided, rather than
309 interpreting the character escapes.  For example,
310 `glob.match([], "\\*a\\?")` will return `"\\*a\\?"` rather than
311 `"*a?"`.  This is akin to setting the `nullglob` option in bash, except
312 that it does not resolve escaped pattern characters.
313
314 If brace expansion is not disabled, then it is performed before any
315 other interpretation of the glob pattern.  Thus, a pattern like
316 `+(a|{b),c)}`, which would not be valid in bash or zsh, is expanded
317 **first** into the set of `+(a|b)` and `+(a|c)`, and those patterns are
318 checked for validity.  Since those two are valid, matching proceeds.
319
320 ## Windows
321
322 **Please only use forward-slashes in glob expressions.**
323
324 Though windows uses either `/` or `\` as its path separator, only `/`
325 characters are used by this glob implementation.  You must use
326 forward-slashes **only** in glob expressions.  Back-slashes will always
327 be interpreted as escape characters, not path separators.
328
329 Results from absolute patterns such as `/foo/*` are mounted onto the
330 root setting using `path.join`.  On windows, this will by default result
331 in `/foo/*` matching `C:\foo\bar.txt`.
332
333 ## Race Conditions
334
335 Glob searching, by its very nature, is susceptible to race conditions,
336 since it relies on directory walking and such.
337
338 As a result, it is possible that a file that exists when glob looks for
339 it may have been deleted or modified by the time it returns the result.
340
341 As part of its internal implementation, this program caches all stat
342 and readdir calls that it makes, in order to cut down on system
343 overhead.  However, this also makes it even more susceptible to races,
344 especially if the cache or statCache objects are reused between glob
345 calls.
346
347 Users are thus advised not to use a glob result as a guarantee of
348 filesystem state in the face of rapid changes.  For the vast majority
349 of operations, this is never a problem.
350
351 ## Contributing
352
353 Any change to behavior (including bugfixes) must come with a test.
354
355 Patches that fail tests or reduce performance will be rejected.
356
357 ```
358 # to run tests
359 npm test
360
361 # to re-generate test fixtures
362 npm run test-regen
363
364 # to benchmark against bash/zsh
365 npm run bench
366
367 # to profile javascript
368 npm run prof
369 ```