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