1 ;(function (require, exports, module, platform) {
3 if (module) module.exports = minimatch
4 else exports.minimatch = minimatch
7 require = function (id) {
9 case "sigmund": return function sigmund (obj) {
10 return JSON.stringify(obj)
12 case "path": return { basename: function (f) {
18 case "lru-cache": return function LRUCache () {
19 // not quite an LRU, but still space-limited.
22 this.set = function (k, v) {
24 if (cnt >= 100) cache = {}
27 this.get = function (k) { return cache[k] }
33 minimatch.Minimatch = Minimatch
35 var LRU = require("lru-cache")
36 , cache = minimatch.cache = new LRU({max: 100})
37 , GLOBSTAR = minimatch.GLOBSTAR = Minimatch.GLOBSTAR = {}
38 , sigmund = require("sigmund")
40 var path = require("path")
41 // any single thing other than /
42 // don't need to escape / when using new RegExp()
45 // * => any number of characters
48 // ** when dots are allowed. Anything goes, except .. and .
49 // not (^ or / followed by one or two dots followed by $ or /),
50 // followed by anything, any number of times.
51 , twoStarDot = "(?:(?!(?:\\\/|^)(?:\\.{1,2})($|\\\/)).)*?"
53 // not a ^ or / followed by a dot,
54 // followed by anything, any number of times.
55 , twoStarNoDot = "(?:(?!(?:\\\/|^)\\.).)*?"
57 // characters that need to be escaped in RegExp.
58 , reSpecials = charSet("().*{}+?[]^$\\!")
60 // "abc" -> { a:true, b:true, c:true }
61 function charSet (s) {
62 return s.split("").reduce(function (set, c) {
68 // normalizes slashes.
69 var slashSplit = /\/+/
71 minimatch.filter = filter
72 function filter (pattern, options) {
73 options = options || {}
74 return function (p, i, list) {
75 return minimatch(p, pattern, options)
83 Object.keys(b).forEach(function (k) {
86 Object.keys(a).forEach(function (k) {
92 minimatch.defaults = function (def) {
93 if (!def || !Object.keys(def).length) return minimatch
97 var m = function minimatch (p, pattern, options) {
98 return orig.minimatch(p, pattern, ext(def, options))
101 m.Minimatch = function Minimatch (pattern, options) {
102 return new orig.Minimatch(pattern, ext(def, options))
108 Minimatch.defaults = function (def) {
109 if (!def || !Object.keys(def).length) return Minimatch
110 return minimatch.defaults(def).Minimatch
114 function minimatch (p, pattern, options) {
115 if (typeof pattern !== "string") {
116 throw new TypeError("glob pattern string required")
119 if (!options) options = {}
121 // shortcut: comments match nothing.
122 if (!options.nocomment && pattern.charAt(0) === "#") {
126 // "" only matches ""
127 if (pattern.trim() === "") return p === ""
129 return new Minimatch(pattern, options).match(p)
132 function Minimatch (pattern, options) {
133 if (!(this instanceof Minimatch)) {
134 return new Minimatch(pattern, options, cache)
137 if (typeof pattern !== "string") {
138 throw new TypeError("glob pattern string required")
141 if (!options) options = {}
142 pattern = pattern.trim()
144 // windows: need to use /, not \
145 // On other platforms, \ is a valid (albeit bad) filename char.
146 if (platform === "win32") {
147 pattern = pattern.split("\\").join("/")
151 // these things aren't particularly big, but walking down the string
152 // and turning it into a regexp can get pretty costly.
153 var cacheKey = pattern + "\n" + sigmund(options)
154 var cached = minimatch.cache.get(cacheKey)
155 if (cached) return cached
156 minimatch.cache.set(cacheKey, this)
158 this.options = options
160 this.pattern = pattern
166 // make the set of regexps etc.
170 Minimatch.prototype.debug = function() {}
172 Minimatch.prototype.make = make
174 // don't do it more than once.
175 if (this._made) return
177 var pattern = this.pattern
178 var options = this.options
180 // empty patterns and comments match nothing.
181 if (!options.nocomment && pattern.charAt(0) === "#") {
190 // step 1: figure out negation, etc.
193 // step 2: expand braces
194 var set = this.globSet = this.braceExpand()
196 if (options.debug) this.debug = console.error
198 this.debug(this.pattern, set)
200 // step 3: now we have a set, so turn each one into a series of path-portion
201 // matching patterns.
202 // These will be regexps, except in the case of "**", which is
203 // set to the GLOBSTAR object for globstar behavior,
204 // and will not contain any / characters
205 set = this.globParts = set.map(function (s) {
206 return s.split(slashSplit)
209 this.debug(this.pattern, set)
212 set = set.map(function (s, si, set) {
213 return s.map(this.parse, this)
216 this.debug(this.pattern, set)
218 // filter out everything that didn't compile properly.
219 set = set.filter(function (s) {
220 return -1 === s.indexOf(false)
223 this.debug(this.pattern, set)
228 Minimatch.prototype.parseNegate = parseNegate
229 function parseNegate () {
230 var pattern = this.pattern
232 , options = this.options
235 if (options.nonegate) return
237 for ( var i = 0, l = pattern.length
238 ; i < l && pattern.charAt(i) === "!"
244 if (negateOffset) this.pattern = pattern.substr(negateOffset)
249 // a{b,c}d -> abd acd
251 // a{0..3}d -> a0d a1d a2d a3d
252 // a{b,c{d,e}f}g -> abg acdfg acefg
253 // a{b,c}d{e,f}g -> abdeg acdeg abdeg abdfg
255 // Invalid sets are not expanded.
256 // a{2..}b -> a{2..}b
258 minimatch.braceExpand = function (pattern, options) {
259 return new Minimatch(pattern, options).braceExpand()
262 Minimatch.prototype.braceExpand = braceExpand
264 function pad(n, width, z) {
267 return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
270 function braceExpand (pattern, options) {
271 options = options || this.options
272 pattern = typeof pattern === "undefined"
273 ? this.pattern : pattern
275 if (typeof pattern === "undefined") {
276 throw new Error("undefined pattern")
279 if (options.nobrace ||
280 !pattern.match(/\{.*\}/)) {
281 // shortcut. no need to expand.
287 // examples and comments refer to this crazy pattern:
288 // a{b,c{d,e},{f,g}h}x{y,z}
301 // everything before the first \{ is just a prefix.
302 // So, we pluck that off, and work with the rest,
303 // and then prepend it to everything we find.
304 if (pattern.charAt(0) !== "{") {
307 for (var i = 0, l = pattern.length; i < l; i ++) {
308 var c = pattern.charAt(i)
312 } else if (c === "{" && !escaping) {
313 prefix = pattern.substr(0, i)
318 // actually no sets, all { were escaped.
319 if (prefix === null) {
320 this.debug("no sets")
324 var tail = braceExpand.call(this, pattern.substr(i), options)
325 return tail.map(function (t) {
330 // now we have something like:
331 // {b,c{d,e},{f,g}h}x{y,z}
332 // walk through the set, expanding each part, until
333 // the set ends. then, we'll expand the suffix.
334 // If the set only has a single member, then'll put the {} back
336 // first, handle numeric sets, since they're easier
337 var numset = pattern.match(/^\{(-?[0-9]+)\.\.(-?[0-9]+)\}/)
339 this.debug("numset", numset[1], numset[2])
340 var suf = braceExpand.call(this, pattern.substr(numset[0].length), options)
342 , needPadding = numset[1][0] === '0'
343 , startWidth = numset[1].length
346 , inc = start > end ? -1 : 1
349 for (var i = start; i != (end + inc); i += inc) {
350 padded = needPadding ? pad(i, startWidth) : i + ''
351 // append all the suffixes
352 for (var ii = 0, ll = suf.length; ii < ll; ii ++) {
353 set.push(padded + suf[ii])
359 // ok, walk through the set
360 // We hope, somewhat optimistically, that there
361 // will be a } at the end.
362 // If the closing brace isn't found, then the pattern is
363 // interpreted as braceExpand("\\" + pattern) so that
364 // the leading \{ will be interpreted literally.
365 var i = 1 // skip the \{
372 function addMember () {
377 this.debug("Entering for")
378 FOR: for (i = 1, l = pattern.length; i < l; i ++) {
379 var c = pattern.charAt(i)
398 // if this closes the actual set, then we're done
401 // pluck off the close-brace
424 // now we've either finished the set, and the suffix is
425 // pattern.substr(i), or we have *not* closed the set,
426 // and need to escape the leading brace
428 this.debug("didn't close", pattern)
429 return braceExpand.call(this, "\\" + pattern, options)
432 // x{y,z} -> ["xy", "xz"]
433 this.debug("set", set)
434 this.debug("suffix", pattern.substr(i))
435 var suf = braceExpand.call(this, pattern.substr(i), options)
436 // ["b", "c{d,e}","{f,g}h"] ->
437 // [["b"], ["cd", "ce"], ["fh", "gh"]]
438 var addBraces = set.length === 1
439 this.debug("set pre-expanded", set)
440 set = set.map(function (p) {
441 return braceExpand.call(this, p, options)
443 this.debug("set expanded", set)
446 // [["b"], ["cd", "ce"], ["fh", "gh"]] ->
447 // ["b", "cd", "ce", "fh", "gh"]
448 set = set.reduce(function (l, r) {
453 set = set.map(function (s) {
458 // now attach the suffixes.
460 for (var i = 0, l = set.length; i < l; i ++) {
461 for (var ii = 0, ll = suf.length; ii < ll; ii ++) {
462 ret.push(set[i] + suf[ii])
468 // parse a component of the expanded set.
469 // At this point, no pattern may contain "/" in it
470 // so we're going to return a 2d array, where each entry is the full
471 // pattern, split on '/', and then turned into a regular expression.
472 // A regexp is made at the end which joins each array with an
473 // escaped /, and another full one which joins each regexp with |.
475 // Following the lead of Bash 4.1, note that "**" only has special meaning
476 // when it is the *only* thing in a path portion. Otherwise, any series
477 // of * is equivalent to a single *. Globstar behavior is enabled by
478 // default, and can be disabled by setting options.noglobstar.
479 Minimatch.prototype.parse = parse
481 function parse (pattern, isSub) {
482 var options = this.options
485 if (!options.noglobstar && pattern === "**") return GLOBSTAR
486 if (pattern === "") return ""
489 , hasMagic = !!options.nocase
491 // ? => one single character
492 , patternListStack = []
498 // . and .. never match anything that doesn't start with .,
499 // even when options.dot is set.
500 , patternStart = pattern.charAt(0) === "." ? "" // anything
501 // not (start or / followed by . or .. followed by / or end)
502 : options.dot ? "(?!(?:^|\\\/)\\.{1,2}(?:$|\\\/))"
506 function clearStateChar () {
508 // we had some state-tracking character
509 // that wasn't consumed by this pass.
523 self.debug('clearStateChar %j %j', stateChar, re)
528 for ( var i = 0, len = pattern.length, c
529 ; (i < len) && (c = pattern.charAt(i))
532 this.debug("%s\t%s %s %j", pattern, i, re, c)
534 // skip over any that are escaped.
535 if (escaping && reSpecials[c]) {
543 // completely not allowed, even escaped.
544 // Should already be path-split by now.
552 // the various stateChar values
553 // for the "extglob" stuff.
559 this.debug("%s\t%s %s %j <-- stateChar", pattern, i, re, c)
561 // all of those are literals inside a class, except that
562 // the glob [!a] means [^a] in regexp
564 this.debug(' in class')
565 if (c === "!" && i === classStart + 1) c = "^"
570 // if we already have a stateChar, then it means
571 // that there was something like ** or +? in there.
572 // Handle the stateChar, then proceed with this one.
573 self.debug('call clearStateChar %j', stateChar)
576 // if extglob is disabled, then +(asdf|foo) isn't a thing.
577 // just clear the statechar *now*, rather than even diving into
578 // the patternList stuff.
579 if (options.noext) clearStateChar()
594 patternListStack.push({ type: plType
596 , reStart: re.length })
597 // negation is (?:(?!js)[^/]*)
598 re += stateChar === "!" ? "(?:(?!" : "(?:"
599 this.debug('plType %j %j', stateChar, re)
604 if (inClass || !patternListStack.length) {
612 plType = patternListStack.pop().type
613 // negation is (?:(?!js)[^/]*)
614 // The others are (?:<pattern>)<type>
621 case "*": re += plType
622 case "@": break // the default anyway
627 if (inClass || !patternListStack.length || escaping) {
637 // these are mostly the same in regexp and glob
639 // swallow any state-tracking char before the [
649 reClassStart = re.length
654 // a right bracket shall lose its special
655 // meaning and represent itself in
656 // a bracket expression if it occurs
657 // first in the list. -- POSIX.2 2.8.3.2
658 if (i === classStart + 1 || !inClass) {
664 // finish up the class.
671 // swallow any state char that wasn't consumed
677 } else if (reSpecials[c]
678 && !(c === "^" && inClass)) {
688 // handle the case where we left a class open.
689 // "[abc" is valid, equivalent to "\[abc"
691 // split where the last [ was, and escape it
692 // this is a huge pita. We now have to re-walk
693 // the contents of the would-be class to re-translate
694 // any characters that were passed through as-is
695 var cs = pattern.substr(classStart + 1)
696 , sp = this.parse(cs, SUBPARSE)
697 re = re.substr(0, reClassStart) + "\\[" + sp[0]
698 hasMagic = hasMagic || sp[1]
701 // handle the case where we had a +( thing at the *end*
703 // each pattern list stack adds 3 chars, and we need to go through
704 // and escape any | chars that were passed through as-is for the regexp.
705 // Go through and escape them, taking care not to double-escape any
706 // | chars that were already escaped.
708 while (pl = patternListStack.pop()) {
709 var tail = re.slice(pl.reStart + 3)
710 // maybe some even number of \, then maybe 1 \, followed by a |
711 tail = tail.replace(/((?:\\{2})*)(\\?)\|/g, function (_, $1, $2) {
713 // the | isn't already escaped, so escape it.
717 // need to escape all those slashes *again*, without escaping the
718 // one that we need for escaping the | character. As it works out,
719 // escaping an even number of slashes can be done by simply repeating
720 // it exactly after itself. That's why this trick works.
722 // I am sorry that you have to see this.
723 return $1 + $1 + $2 + "|"
726 this.debug("tail=%j\n %s", tail, tail)
727 var t = pl.type === "*" ? star
728 : pl.type === "?" ? qmark
732 re = re.slice(0, pl.reStart)
737 // handle trailing things that only matter at the very end.
744 // only need to apply the nodot start if the re starts with
745 // something that could conceivably capture a dot
746 var addPatternStart = false
747 switch (re.charAt(0)) {
750 case "(": addPatternStart = true
753 // if the re is not "" at this point, then we need to make sure
754 // it doesn't match against an empty path part.
755 // Otherwise a/* will match a/, which it should not.
756 if (re !== "" && hasMagic) re = "(?=.)" + re
758 if (addPatternStart) re = patternStart + re
760 // parsing just a piece of a larger pattern.
761 if (isSub === SUBPARSE) {
762 return [ re, hasMagic ]
765 // skip the regexp for non-magical patterns
766 // unescape anything in it, though, so that it'll be
767 // an exact match against a file etc.
769 return globUnescape(pattern)
772 var flags = options.nocase ? "i" : ""
773 , regExp = new RegExp("^" + re + "$", flags)
775 regExp._glob = pattern
781 minimatch.makeRe = function (pattern, options) {
782 return new Minimatch(pattern, options || {}).makeRe()
785 Minimatch.prototype.makeRe = makeRe
787 if (this.regexp || this.regexp === false) return this.regexp
789 // at this point, this.set is a 2d array of partial
790 // pattern strings, or "**".
792 // It's better to use .match(). This function shouldn't
793 // be used, really, but it's pretty convenient sometimes,
794 // when you just want to work with a regex.
797 if (!set.length) return this.regexp = false
798 var options = this.options
800 var twoStar = options.noglobstar ? star
801 : options.dot ? twoStarDot
803 , flags = options.nocase ? "i" : ""
805 var re = set.map(function (pattern) {
806 return pattern.map(function (p) {
807 return (p === GLOBSTAR) ? twoStar
808 : (typeof p === "string") ? regExpEscape(p)
813 // must match entire pattern
814 // ending in a * or ** will make it less strict.
815 re = "^(?:" + re + ")$"
817 // can match anything, as long as it's not this.
818 if (this.negate) re = "^(?!" + re + ").*$"
821 return this.regexp = new RegExp(re, flags)
823 return this.regexp = false
827 minimatch.match = function (list, pattern, options) {
828 options = options || {}
829 var mm = new Minimatch(pattern, options)
830 list = list.filter(function (f) {
833 if (mm.options.nonull && !list.length) {
839 Minimatch.prototype.match = match
840 function match (f, partial) {
841 this.debug("match", f, this.pattern)
842 // short-circuit in the case of busted things.
844 if (this.comment) return false
845 if (this.empty) return f === ""
847 if (f === "/" && partial) return true
849 var options = this.options
851 // windows: need to use /, not \
852 // On other platforms, \ is a valid (albeit bad) filename char.
853 if (platform === "win32") {
854 f = f.split("\\").join("/")
857 // treat the test path as a set of pathparts.
858 f = f.split(slashSplit)
859 this.debug(this.pattern, "split", f)
861 // just ONE of the pattern sets in this.set needs to match
862 // in order for it to be valid. If negating, then just one
863 // match means that we have failed.
864 // Either way, return on the first hit.
867 this.debug(this.pattern, "set", set)
869 // Find the basename of the path by looking for the last non-empty segment
871 for (var i = f.length - 1; i >= 0; i--) {
876 for (var i = 0, l = set.length; i < l; i ++) {
877 var pattern = set[i], file = f
878 if (options.matchBase && pattern.length === 1) {
881 var hit = this.matchOne(file, pattern, partial)
883 if (options.flipNegate) return true
888 // didn't get any hits. this is success if it's a negative
889 // pattern, failure otherwise.
890 if (options.flipNegate) return false
894 // set partial to true to test if, for example,
895 // "/a/b" matches the start of "/*/b/*/d"
896 // Partial means, if you run out of file before you run
897 // out of pattern, then that's fine, as long as all
899 Minimatch.prototype.matchOne = function (file, pattern, partial) {
900 var options = this.options
902 this.debug("matchOne",
905 , pattern: pattern })
907 this.debug("matchOne", file.length, pattern.length)
912 , pl = pattern.length
913 ; (fi < fl) && (pi < pl)
916 this.debug("matchOne loop")
920 this.debug(pattern, p, f)
922 // should be impossible.
923 // some invalid regexp stuff in the set.
924 if (p === false) return false
926 if (p === GLOBSTAR) {
927 this.debug('GLOBSTAR', [pattern, p, f])
930 // a/**/b/**/c would match the following:
935 // To do this, take the rest of the pattern after
936 // the **, and see if it would match the file remainder.
937 // If so, return success.
938 // If not, the ** "swallows" a segment, and try again.
939 // This is recursively awful.
941 // a/**/b/**/c matching a/b/x/y/z/c
944 // - matchOne(b/x/y/z/c, b/**/c)
947 // - matchOne(x/y/z/c, c) -> no
948 // - matchOne(y/z/c, c) -> no
949 // - matchOne(z/c, c) -> no
950 // - matchOne(c, c) yes, hit
954 this.debug('** at the end')
955 // a ** at the end will just swallow the rest.
956 // We have found a match.
957 // however, it will not swallow /.x, unless
958 // options.dot is set.
959 // . and .. are *never* matched by **, for explosively
960 // exponential reasons.
961 for ( ; fi < fl; fi ++) {
962 if (file[fi] === "." || file[fi] === ".." ||
963 (!options.dot && file[fi].charAt(0) === ".")) return false
968 // ok, let's see if we can swallow whatever we can.
969 WHILE: while (fr < fl) {
970 var swallowee = file[fr]
972 this.debug('\nglobstar while',
973 file, fr, pattern, pr, swallowee)
975 // XXX remove this slice. Just pass the start index.
976 if (this.matchOne(file.slice(fr), pattern.slice(pr), partial)) {
977 this.debug('globstar found match!', fr, fl, swallowee)
981 // can't swallow "." or ".." ever.
982 // can only swallow ".foo" when explicitly asked.
983 if (swallowee === "." || swallowee === ".." ||
984 (!options.dot && swallowee.charAt(0) === ".")) {
985 this.debug("dot detected!", file, fr, pattern, pr)
989 // ** swallows a segment, and continue.
990 this.debug('globstar swallow a segment, and continue')
994 // no match was found.
995 // However, in partial mode, we can't say this is necessarily over.
996 // If there's more *pattern* left, then
999 this.debug("\n>>> no match, partial?", file, fr, pattern, pr)
1000 if (fr === fl) return true
1005 // something other than **
1006 // non-magic patterns just have to match exactly
1007 // patterns with magic have been turned into regexps.
1009 if (typeof p === "string") {
1010 if (options.nocase) {
1011 hit = f.toLowerCase() === p.toLowerCase()
1015 this.debug("string match", p, f, hit)
1018 this.debug("pattern match", p, f, hit)
1021 if (!hit) return false
1024 // Note: ending in / means that we'll get a final ""
1025 // at the end of the pattern. This can only match a
1026 // corresponding "" at the end of the file.
1027 // If the file ends in /, then it can only match a
1028 // a pattern that ends in /, unless the pattern just
1029 // doesn't have any more for it. But, a/b/ should *not*
1030 // match "a/b/*", even though "" matches against the
1031 // [^/]*? pattern, except in partial mode, where it might
1032 // simply not be reached yet.
1033 // However, a/b/ should still satisfy a/*
1035 // now either we fell off the end of the pattern, or we're done.
1036 if (fi === fl && pi === pl) {
1037 // ran out of pattern and filename at the same time.
1040 } else if (fi === fl) {
1041 // ran out of file, but still had pattern left.
1042 // this is ok if we're doing the match as part of
1043 // a glob fs traversal.
1045 } else if (pi === pl) {
1046 // ran out of pattern, still have file left.
1047 // this is only acceptable if we're on the very last
1048 // empty segment of a file with a trailing slash.
1049 // a/* should match a/b/
1050 var emptyFileEnd = (fi === fl - 1) && (file[fi] === "")
1054 // should be unreachable.
1055 throw new Error("wtf?")
1059 // replace stuff like \* with *
1060 function globUnescape (s) {
1061 return s.replace(/\\(.)/g, "$1")
1065 function regExpEscape (s) {
1066 return s.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&")
1069 })( typeof require === "function" ? require : null,
1071 typeof module === "object" ? module : null,
1072 typeof process === "object" ? process.platform : "win32"