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.monkeyPatch = monkeyPatch
72 function monkeyPatch () {
73 var desc = Object.getOwnPropertyDescriptor(String.prototype, "match")
75 desc.value = function (p) {
76 if (p instanceof Minimatch) return p.match(this)
77 return orig.call(this, p)
79 Object.defineProperty(String.prototype, desc)
82 minimatch.filter = filter
83 function filter (pattern, options) {
84 options = options || {}
85 return function (p, i, list) {
86 return minimatch(p, pattern, options)
94 Object.keys(b).forEach(function (k) {
97 Object.keys(a).forEach(function (k) {
103 minimatch.defaults = function (def) {
104 if (!def || !Object.keys(def).length) return minimatch
108 var m = function minimatch (p, pattern, options) {
109 return orig.minimatch(p, pattern, ext(def, options))
112 m.Minimatch = function Minimatch (pattern, options) {
113 return new orig.Minimatch(pattern, ext(def, options))
119 Minimatch.defaults = function (def) {
120 if (!def || !Object.keys(def).length) return Minimatch
121 return minimatch.defaults(def).Minimatch
125 function minimatch (p, pattern, options) {
126 if (typeof pattern !== "string") {
127 throw new TypeError("glob pattern string required")
130 if (!options) options = {}
132 // shortcut: comments match nothing.
133 if (!options.nocomment && pattern.charAt(0) === "#") {
137 // "" only matches ""
138 if (pattern.trim() === "") return p === ""
140 return new Minimatch(pattern, options).match(p)
143 function Minimatch (pattern, options) {
144 if (!(this instanceof Minimatch)) {
145 return new Minimatch(pattern, options, cache)
148 if (typeof pattern !== "string") {
149 throw new TypeError("glob pattern string required")
152 if (!options) options = {}
153 pattern = pattern.trim()
155 // windows: need to use /, not \
156 // On other platforms, \ is a valid (albeit bad) filename char.
157 if (platform === "win32") {
158 pattern = pattern.split("\\").join("/")
162 // these things aren't particularly big, but walking down the string
163 // and turning it into a regexp can get pretty costly.
164 var cacheKey = pattern + "\n" + sigmund(options)
165 var cached = minimatch.cache.get(cacheKey)
166 if (cached) return cached
167 minimatch.cache.set(cacheKey, this)
169 this.options = options
171 this.pattern = pattern
177 // make the set of regexps etc.
181 Minimatch.prototype.make = make
183 // don't do it more than once.
184 if (this._made) return
186 var pattern = this.pattern
187 var options = this.options
189 // empty patterns and comments match nothing.
190 if (!options.nocomment && pattern.charAt(0) === "#") {
199 // step 1: figure out negation, etc.
202 // step 2: expand braces
203 var set = this.globSet = this.braceExpand()
205 if (options.debug) console.error(this.pattern, set)
207 // step 3: now we have a set, so turn each one into a series of path-portion
208 // matching patterns.
209 // These will be regexps, except in the case of "**", which is
210 // set to the GLOBSTAR object for globstar behavior,
211 // and will not contain any / characters
212 set = this.globParts = set.map(function (s) {
213 return s.split(slashSplit)
216 if (options.debug) console.error(this.pattern, set)
219 set = set.map(function (s, si, set) {
220 return s.map(this.parse, this)
223 if (options.debug) console.error(this.pattern, set)
225 // filter out everything that didn't compile properly.
226 set = set.filter(function (s) {
227 return -1 === s.indexOf(false)
230 if (options.debug) console.error(this.pattern, set)
235 Minimatch.prototype.parseNegate = parseNegate
236 function parseNegate () {
237 var pattern = this.pattern
239 , options = this.options
242 if (options.nonegate) return
244 for ( var i = 0, l = pattern.length
245 ; i < l && pattern.charAt(i) === "!"
251 if (negateOffset) this.pattern = pattern.substr(negateOffset)
256 // a{b,c}d -> abd acd
258 // a{0..3}d -> a0d a1d a2d a3d
259 // a{b,c{d,e}f}g -> abg acdfg acefg
260 // a{b,c}d{e,f}g -> abdeg acdeg abdeg abdfg
262 // Invalid sets are not expanded.
263 // a{2..}b -> a{2..}b
265 minimatch.braceExpand = function (pattern, options) {
266 return new Minimatch(pattern, options).braceExpand()
269 Minimatch.prototype.braceExpand = braceExpand
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) !== "{") {
305 // console.error(pattern)
307 for (var i = 0, l = pattern.length; i < l; i ++) {
308 var c = pattern.charAt(i)
309 // console.error(i, c)
312 } else if (c === "{" && !escaping) {
313 prefix = pattern.substr(0, i)
318 // actually no sets, all { were escaped.
319 if (prefix === null) {
320 // console.error("no sets")
324 var tail = braceExpand(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 // console.error("numset", numset[1], numset[2])
340 var suf = braceExpand(pattern.substr(numset[0].length), options)
343 , inc = start > end ? -1 : 1
345 for (var i = start; i != (end + inc); i += inc) {
346 // append all the suffixes
347 for (var ii = 0, ll = suf.length; ii < ll; ii ++) {
348 set.push(i + suf[ii])
354 // ok, walk through the set
355 // We hope, somewhat optimistically, that there
356 // will be a } at the end.
357 // If the closing brace isn't found, then the pattern is
358 // interpreted as braceExpand("\\" + pattern) so that
359 // the leading \{ will be interpreted literally.
360 var i = 1 // skip the \{
367 function addMember () {
372 // console.error("Entering for")
373 FOR: for (i = 1, l = pattern.length; i < l; i ++) {
374 var c = pattern.charAt(i)
375 // console.error("", i, c)
393 // if this closes the actual set, then we're done
396 // pluck off the close-brace
419 // now we've either finished the set, and the suffix is
420 // pattern.substr(i), or we have *not* closed the set,
421 // and need to escape the leading brace
423 // console.error("didn't close", pattern)
424 return braceExpand("\\" + pattern, options)
427 // x{y,z} -> ["xy", "xz"]
428 // console.error("set", set)
429 // console.error("suffix", pattern.substr(i))
430 var suf = braceExpand(pattern.substr(i), options)
431 // ["b", "c{d,e}","{f,g}h"] ->
432 // [["b"], ["cd", "ce"], ["fh", "gh"]]
433 var addBraces = set.length === 1
434 // console.error("set pre-expanded", set)
435 set = set.map(function (p) {
436 return braceExpand(p, options)
438 // console.error("set expanded", set)
441 // [["b"], ["cd", "ce"], ["fh", "gh"]] ->
442 // ["b", "cd", "ce", "fh", "gh"]
443 set = set.reduce(function (l, r) {
448 set = set.map(function (s) {
453 // now attach the suffixes.
455 for (var i = 0, l = set.length; i < l; i ++) {
456 for (var ii = 0, ll = suf.length; ii < ll; ii ++) {
457 ret.push(set[i] + suf[ii])
463 // parse a component of the expanded set.
464 // At this point, no pattern may contain "/" in it
465 // so we're going to return a 2d array, where each entry is the full
466 // pattern, split on '/', and then turned into a regular expression.
467 // A regexp is made at the end which joins each array with an
468 // escaped /, and another full one which joins each regexp with |.
470 // Following the lead of Bash 4.1, note that "**" only has special meaning
471 // when it is the *only* thing in a path portion. Otherwise, any series
472 // of * is equivalent to a single *. Globstar behavior is enabled by
473 // default, and can be disabled by setting options.noglobstar.
474 Minimatch.prototype.parse = parse
476 function parse (pattern, isSub) {
477 var options = this.options
480 if (!options.noglobstar && pattern === "**") return GLOBSTAR
481 if (pattern === "") return ""
486 // ? => one single character
487 , patternListStack = []
493 // . and .. never match anything that doesn't start with .,
494 // even when options.dot is set.
495 , patternStart = pattern.charAt(0) === "." ? "" // anything
496 // not (start or / followed by . or .. followed by / or end)
497 : options.dot ? "(?!(?:^|\\\/)\\.{1,2}(?:$|\\\/))"
500 function clearStateChar () {
502 // we had some state-tracking character
503 // that wasn't consumed by this pass.
521 for ( var i = 0, len = pattern.length, c
522 ; (i < len) && (c = pattern.charAt(i))
526 console.error("%s\t%s %s %j", pattern, i, re, c)
529 // skip over any that are escaped.
530 if (escaping && reSpecials[c]) {
538 // completely not allowed, even escaped.
539 // Should already be path-split by now.
547 // the various stateChar values
548 // for the "extglob" stuff.
555 console.error("%s\t%s %s %j <-- stateChar", pattern, i, re, c)
558 // all of those are literals inside a class, except that
559 // the glob [!a] means [^a] in regexp
561 if (c === "!" && i === classStart + 1) c = "^"
566 // if we already have a stateChar, then it means
567 // that there was something like ** or +? in there.
568 // Handle the stateChar, then proceed with this one.
571 // if extglob is disabled, then +(asdf|foo) isn't a thing.
572 // just clear the statechar *now*, rather than even diving into
573 // the patternList stuff.
574 if (options.noext) clearStateChar()
589 patternListStack.push({ type: plType
591 , reStart: re.length })
592 // negation is (?:(?!js)[^/]*)
593 re += stateChar === "!" ? "(?:(?!" : "(?:"
598 if (inClass || !patternListStack.length) {
605 plType = patternListStack.pop().type
606 // negation is (?:(?!js)[^/]*)
607 // The others are (?:<pattern>)<type>
614 case "*": re += plType
615 case "@": break // the default anyway
620 if (inClass || !patternListStack.length || escaping) {
629 // these are mostly the same in regexp and glob
631 // swallow any state-tracking char before the [
641 reClassStart = re.length
646 // a right bracket shall lose its special
647 // meaning and represent itself in
648 // a bracket expression if it occurs
649 // first in the list. -- POSIX.2 2.8.3.2
650 if (i === classStart + 1 || !inClass) {
656 // finish up the class.
663 // swallow any state char that wasn't consumed
669 } else if (reSpecials[c]
670 && !(c === "^" && inClass)) {
680 // handle the case where we left a class open.
681 // "[abc" is valid, equivalent to "\[abc"
683 // split where the last [ was, and escape it
684 // this is a huge pita. We now have to re-walk
685 // the contents of the would-be class to re-translate
686 // any characters that were passed through as-is
687 var cs = pattern.substr(classStart + 1)
688 , sp = this.parse(cs, SUBPARSE)
689 re = re.substr(0, reClassStart) + "\\[" + sp[0]
690 hasMagic = hasMagic || sp[1]
693 // handle the case where we had a +( thing at the *end*
695 // each pattern list stack adds 3 chars, and we need to go through
696 // and escape any | chars that were passed through as-is for the regexp.
697 // Go through and escape them, taking care not to double-escape any
698 // | chars that were already escaped.
700 while (pl = patternListStack.pop()) {
701 var tail = re.slice(pl.reStart + 3)
702 // maybe some even number of \, then maybe 1 \, followed by a |
703 tail = tail.replace(/((?:\\{2})*)(\\?)\|/g, function (_, $1, $2) {
705 // the | isn't already escaped, so escape it.
709 // need to escape all those slashes *again*, without escaping the
710 // one that we need for escaping the | character. As it works out,
711 // escaping an even number of slashes can be done by simply repeating
712 // it exactly after itself. That's why this trick works.
714 // I am sorry that you have to see this.
715 return $1 + $1 + $2 + "|"
718 // console.error("tail=%j\n %s", tail, tail)
719 var t = pl.type === "*" ? star
720 : pl.type === "?" ? qmark
724 re = re.slice(0, pl.reStart)
729 // handle trailing things that only matter at the very end.
736 // only need to apply the nodot start if the re starts with
737 // something that could conceivably capture a dot
738 var addPatternStart = false
739 switch (re.charAt(0)) {
742 case "(": addPatternStart = true
745 // if the re is not "" at this point, then we need to make sure
746 // it doesn't match against an empty path part.
747 // Otherwise a/* will match a/, which it should not.
748 if (re !== "" && hasMagic) re = "(?=.)" + re
750 if (addPatternStart) re = patternStart + re
752 // parsing just a piece of a larger pattern.
753 if (isSub === SUBPARSE) {
754 return [ re, hasMagic ]
757 // skip the regexp for non-magical patterns
758 // unescape anything in it, though, so that it'll be
759 // an exact match against a file etc.
761 return globUnescape(pattern)
764 var flags = options.nocase ? "i" : ""
765 , regExp = new RegExp("^" + re + "$", flags)
767 regExp._glob = pattern
773 minimatch.makeRe = function (pattern, options) {
774 return new Minimatch(pattern, options || {}).makeRe()
777 Minimatch.prototype.makeRe = makeRe
779 if (this.regexp || this.regexp === false) return this.regexp
781 // at this point, this.set is a 2d array of partial
782 // pattern strings, or "**".
784 // It's better to use .match(). This function shouldn't
785 // be used, really, but it's pretty convenient sometimes,
786 // when you just want to work with a regex.
789 if (!set.length) return this.regexp = false
790 var options = this.options
792 var twoStar = options.noglobstar ? star
793 : options.dot ? twoStarDot
795 , flags = options.nocase ? "i" : ""
797 var re = set.map(function (pattern) {
798 return pattern.map(function (p) {
799 return (p === GLOBSTAR) ? twoStar
800 : (typeof p === "string") ? regExpEscape(p)
805 // must match entire pattern
806 // ending in a * or ** will make it less strict.
809 // can match anything, as long as it's not this.
810 if (this.negate) re = "^(?!" + re + ").*$"
813 return this.regexp = new RegExp(re, flags)
815 return this.regexp = false
819 minimatch.match = function (list, pattern, options) {
820 var mm = new Minimatch(pattern, options)
821 list = list.filter(function (f) {
824 if (options.nonull && !list.length) {
830 Minimatch.prototype.match = match
831 function match (f, partial) {
832 // console.error("match", f, this.pattern)
833 // short-circuit in the case of busted things.
835 if (this.comment) return false
836 if (this.empty) return f === ""
838 if (f === "/" && partial) return true
840 var options = this.options
842 // windows: need to use /, not \
843 // On other platforms, \ is a valid (albeit bad) filename char.
844 if (platform === "win32") {
845 f = f.split("\\").join("/")
848 // treat the test path as a set of pathparts.
849 f = f.split(slashSplit)
851 console.error(this.pattern, "split", f)
854 // just ONE of the pattern sets in this.set needs to match
855 // in order for it to be valid. If negating, then just one
856 // match means that we have failed.
857 // Either way, return on the first hit.
860 // console.error(this.pattern, "set", set)
862 for (var i = 0, l = set.length; i < l; i ++) {
864 var hit = this.matchOne(f, pattern, partial)
866 if (options.flipNegate) return true
871 // didn't get any hits. this is success if it's a negative
872 // pattern, failure otherwise.
873 if (options.flipNegate) return false
877 // set partial to true to test if, for example,
878 // "/a/b" matches the start of "/*/b/*/d"
879 // Partial means, if you run out of file before you run
880 // out of pattern, then that's fine, as long as all
882 Minimatch.prototype.matchOne = function (file, pattern, partial) {
883 var options = this.options
886 console.error("matchOne",
889 , pattern: pattern })
892 if (options.matchBase && pattern.length === 1) {
893 file = path.basename(file.join("/")).split("/")
897 console.error("matchOne", file.length, pattern.length)
903 , pl = pattern.length
904 ; (fi < fl) && (pi < pl)
908 console.error("matchOne loop")
914 console.error(pattern, p, f)
917 // should be impossible.
918 // some invalid regexp stuff in the set.
919 if (p === false) return false
921 if (p === GLOBSTAR) {
923 console.error('GLOBSTAR', [pattern, p, f])
926 // a/**/b/**/c would match the following:
931 // To do this, take the rest of the pattern after
932 // the **, and see if it would match the file remainder.
933 // If so, return success.
934 // If not, the ** "swallows" a segment, and try again.
935 // This is recursively awful.
937 // a/**/b/**/c matching a/b/x/y/z/c
940 // - matchOne(b/x/y/z/c, b/**/c)
943 // - matchOne(x/y/z/c, c) -> no
944 // - matchOne(y/z/c, c) -> no
945 // - matchOne(z/c, c) -> no
946 // - matchOne(c, c) yes, hit
951 console.error('** at the end')
952 // a ** at the end will just swallow the rest.
953 // We have found a match.
954 // however, it will not swallow /.x, unless
955 // options.dot is set.
956 // . and .. are *never* matched by **, for explosively
957 // exponential reasons.
958 for ( ; fi < fl; fi ++) {
959 if (file[fi] === "." || file[fi] === ".." ||
960 (!options.dot && file[fi].charAt(0) === ".")) return false
965 // ok, let's see if we can swallow whatever we can.
966 WHILE: while (fr < fl) {
967 var swallowee = file[fr]
970 console.error('\nglobstar while',
971 file, fr, pattern, pr, swallowee)
974 // XXX remove this slice. Just pass the start index.
975 if (this.matchOne(file.slice(fr), pattern.slice(pr), partial)) {
977 console.error('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) === ".")) {
986 console.error("dot detected!", file, fr, pattern, pr)
990 // ** swallows a segment, and continue.
992 console.error('globstar swallow a segment, and continue')
996 // no match was found.
997 // However, in partial mode, we can't say this is necessarily over.
998 // If there's more *pattern* left, then
1001 // console.error("\n>>> no match, partial?", file, fr, pattern, pr)
1002 if (fr === fl) return true
1007 // something other than **
1008 // non-magic patterns just have to match exactly
1009 // patterns with magic have been turned into regexps.
1011 if (typeof p === "string") {
1012 if (options.nocase) {
1013 hit = f.toLowerCase() === p.toLowerCase()
1017 if (options.debug) {
1018 console.error("string match", p, f, hit)
1022 if (options.debug) {
1023 console.error("pattern match", p, f, hit)
1027 if (!hit) return false
1030 // Note: ending in / means that we'll get a final ""
1031 // at the end of the pattern. This can only match a
1032 // corresponding "" at the end of the file.
1033 // If the file ends in /, then it can only match a
1034 // a pattern that ends in /, unless the pattern just
1035 // doesn't have any more for it. But, a/b/ should *not*
1036 // match "a/b/*", even though "" matches against the
1037 // [^/]*? pattern, except in partial mode, where it might
1038 // simply not be reached yet.
1039 // However, a/b/ should still satisfy a/*
1041 // now either we fell off the end of the pattern, or we're done.
1042 if (fi === fl && pi === pl) {
1043 // ran out of pattern and filename at the same time.
1046 } else if (fi === fl) {
1047 // ran out of file, but still had pattern left.
1048 // this is ok if we're doing the match as part of
1049 // a glob fs traversal.
1051 } else if (pi === pl) {
1052 // ran out of pattern, still have file left.
1053 // this is only acceptable if we're on the very last
1054 // empty segment of a file with a trailing slash.
1055 // a/* should match a/b/
1056 var emptyFileEnd = (fi === fl - 1) && (file[fi] === "")
1060 // should be unreachable.
1061 throw new Error("wtf?")
1065 // replace stuff like \* with *
1066 function globUnescape (s) {
1067 return s.replace(/\\(.)/g, "$1")
1071 function regExpEscape (s) {
1072 return s.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&")
1075 })( typeof require === "function" ? require : null,
1077 typeof module === "object" ? module : null,
1078 typeof process === "object" ? process.platform : "win32"