Nenhuma descrição

minimatch.js 28KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  1. ;(function (require, exports, module, platform) {
  2. if (module) module.exports = minimatch
  3. else exports.minimatch = minimatch
  4. if (!require) {
  5. require = function (id) {
  6. switch (id) {
  7. case "sigmund": return function sigmund (obj) {
  8. return JSON.stringify(obj)
  9. }
  10. case "path": return { basename: function (f) {
  11. f = f.split(/[\/\\]/)
  12. var e = f.pop()
  13. if (!e) e = f.pop()
  14. return e
  15. }}
  16. case "lru-cache": return function LRUCache () {
  17. // not quite an LRU, but still space-limited.
  18. var cache = {}
  19. var cnt = 0
  20. this.set = function (k, v) {
  21. cnt ++
  22. if (cnt >= 100) cache = {}
  23. cache[k] = v
  24. }
  25. this.get = function (k) { return cache[k] }
  26. }
  27. }
  28. }
  29. }
  30. minimatch.Minimatch = Minimatch
  31. var LRU = require("lru-cache")
  32. , cache = minimatch.cache = new LRU({max: 100})
  33. , GLOBSTAR = minimatch.GLOBSTAR = Minimatch.GLOBSTAR = {}
  34. , sigmund = require("sigmund")
  35. var path = require("path")
  36. // any single thing other than /
  37. // don't need to escape / when using new RegExp()
  38. , qmark = "[^/]"
  39. // * => any number of characters
  40. , star = qmark + "*?"
  41. // ** when dots are allowed. Anything goes, except .. and .
  42. // not (^ or / followed by one or two dots followed by $ or /),
  43. // followed by anything, any number of times.
  44. , twoStarDot = "(?:(?!(?:\\\/|^)(?:\\.{1,2})($|\\\/)).)*?"
  45. // not a ^ or / followed by a dot,
  46. // followed by anything, any number of times.
  47. , twoStarNoDot = "(?:(?!(?:\\\/|^)\\.).)*?"
  48. // characters that need to be escaped in RegExp.
  49. , reSpecials = charSet("().*{}+?[]^$\\!")
  50. // "abc" -> { a:true, b:true, c:true }
  51. function charSet (s) {
  52. return s.split("").reduce(function (set, c) {
  53. set[c] = true
  54. return set
  55. }, {})
  56. }
  57. // normalizes slashes.
  58. var slashSplit = /\/+/
  59. minimatch.filter = filter
  60. function filter (pattern, options) {
  61. options = options || {}
  62. return function (p, i, list) {
  63. return minimatch(p, pattern, options)
  64. }
  65. }
  66. function ext (a, b) {
  67. a = a || {}
  68. b = b || {}
  69. var t = {}
  70. Object.keys(b).forEach(function (k) {
  71. t[k] = b[k]
  72. })
  73. Object.keys(a).forEach(function (k) {
  74. t[k] = a[k]
  75. })
  76. return t
  77. }
  78. minimatch.defaults = function (def) {
  79. if (!def || !Object.keys(def).length) return minimatch
  80. var orig = minimatch
  81. var m = function minimatch (p, pattern, options) {
  82. return orig.minimatch(p, pattern, ext(def, options))
  83. }
  84. m.Minimatch = function Minimatch (pattern, options) {
  85. return new orig.Minimatch(pattern, ext(def, options))
  86. }
  87. return m
  88. }
  89. Minimatch.defaults = function (def) {
  90. if (!def || !Object.keys(def).length) return Minimatch
  91. return minimatch.defaults(def).Minimatch
  92. }
  93. function minimatch (p, pattern, options) {
  94. if (typeof pattern !== "string") {
  95. throw new TypeError("glob pattern string required")
  96. }
  97. if (!options) options = {}
  98. // shortcut: comments match nothing.
  99. if (!options.nocomment && pattern.charAt(0) === "#") {
  100. return false
  101. }
  102. // "" only matches ""
  103. if (pattern.trim() === "") return p === ""
  104. return new Minimatch(pattern, options).match(p)
  105. }
  106. function Minimatch (pattern, options) {
  107. if (!(this instanceof Minimatch)) {
  108. return new Minimatch(pattern, options, cache)
  109. }
  110. if (typeof pattern !== "string") {
  111. throw new TypeError("glob pattern string required")
  112. }
  113. if (!options) options = {}
  114. pattern = pattern.trim()
  115. // windows: need to use /, not \
  116. // On other platforms, \ is a valid (albeit bad) filename char.
  117. if (platform === "win32") {
  118. pattern = pattern.split("\\").join("/")
  119. }
  120. // lru storage.
  121. // these things aren't particularly big, but walking down the string
  122. // and turning it into a regexp can get pretty costly.
  123. var cacheKey = pattern + "\n" + sigmund(options)
  124. var cached = minimatch.cache.get(cacheKey)
  125. if (cached) return cached
  126. minimatch.cache.set(cacheKey, this)
  127. this.options = options
  128. this.set = []
  129. this.pattern = pattern
  130. this.regexp = null
  131. this.negate = false
  132. this.comment = false
  133. this.empty = false
  134. // make the set of regexps etc.
  135. this.make()
  136. }
  137. Minimatch.prototype.debug = function() {}
  138. Minimatch.prototype.make = make
  139. function make () {
  140. // don't do it more than once.
  141. if (this._made) return
  142. var pattern = this.pattern
  143. var options = this.options
  144. // empty patterns and comments match nothing.
  145. if (!options.nocomment && pattern.charAt(0) === "#") {
  146. this.comment = true
  147. return
  148. }
  149. if (!pattern) {
  150. this.empty = true
  151. return
  152. }
  153. // step 1: figure out negation, etc.
  154. this.parseNegate()
  155. // step 2: expand braces
  156. var set = this.globSet = this.braceExpand()
  157. if (options.debug) this.debug = console.error
  158. this.debug(this.pattern, set)
  159. // step 3: now we have a set, so turn each one into a series of path-portion
  160. // matching patterns.
  161. // These will be regexps, except in the case of "**", which is
  162. // set to the GLOBSTAR object for globstar behavior,
  163. // and will not contain any / characters
  164. set = this.globParts = set.map(function (s) {
  165. return s.split(slashSplit)
  166. })
  167. this.debug(this.pattern, set)
  168. // glob --> regexps
  169. set = set.map(function (s, si, set) {
  170. return s.map(this.parse, this)
  171. }, this)
  172. this.debug(this.pattern, set)
  173. // filter out everything that didn't compile properly.
  174. set = set.filter(function (s) {
  175. return -1 === s.indexOf(false)
  176. })
  177. this.debug(this.pattern, set)
  178. this.set = set
  179. }
  180. Minimatch.prototype.parseNegate = parseNegate
  181. function parseNegate () {
  182. var pattern = this.pattern
  183. , negate = false
  184. , options = this.options
  185. , negateOffset = 0
  186. if (options.nonegate) return
  187. for ( var i = 0, l = pattern.length
  188. ; i < l && pattern.charAt(i) === "!"
  189. ; i ++) {
  190. negate = !negate
  191. negateOffset ++
  192. }
  193. if (negateOffset) this.pattern = pattern.substr(negateOffset)
  194. this.negate = negate
  195. }
  196. // Brace expansion:
  197. // a{b,c}d -> abd acd
  198. // a{b,}c -> abc ac
  199. // a{0..3}d -> a0d a1d a2d a3d
  200. // a{b,c{d,e}f}g -> abg acdfg acefg
  201. // a{b,c}d{e,f}g -> abdeg acdeg abdeg abdfg
  202. //
  203. // Invalid sets are not expanded.
  204. // a{2..}b -> a{2..}b
  205. // a{b}c -> a{b}c
  206. minimatch.braceExpand = function (pattern, options) {
  207. return new Minimatch(pattern, options).braceExpand()
  208. }
  209. Minimatch.prototype.braceExpand = braceExpand
  210. function braceExpand (pattern, options) {
  211. options = options || this.options
  212. pattern = typeof pattern === "undefined"
  213. ? this.pattern : pattern
  214. if (typeof pattern === "undefined") {
  215. throw new Error("undefined pattern")
  216. }
  217. if (options.nobrace ||
  218. !pattern.match(/\{.*\}/)) {
  219. // shortcut. no need to expand.
  220. return [pattern]
  221. }
  222. var escaping = false
  223. // examples and comments refer to this crazy pattern:
  224. // a{b,c{d,e},{f,g}h}x{y,z}
  225. // expected:
  226. // abxy
  227. // abxz
  228. // acdxy
  229. // acdxz
  230. // acexy
  231. // acexz
  232. // afhxy
  233. // afhxz
  234. // aghxy
  235. // aghxz
  236. // everything before the first \{ is just a prefix.
  237. // So, we pluck that off, and work with the rest,
  238. // and then prepend it to everything we find.
  239. if (pattern.charAt(0) !== "{") {
  240. this.debug(pattern)
  241. var prefix = null
  242. for (var i = 0, l = pattern.length; i < l; i ++) {
  243. var c = pattern.charAt(i)
  244. this.debug(i, c)
  245. if (c === "\\") {
  246. escaping = !escaping
  247. } else if (c === "{" && !escaping) {
  248. prefix = pattern.substr(0, i)
  249. break
  250. }
  251. }
  252. // actually no sets, all { were escaped.
  253. if (prefix === null) {
  254. this.debug("no sets")
  255. return [pattern]
  256. }
  257. var tail = braceExpand.call(this, pattern.substr(i), options)
  258. return tail.map(function (t) {
  259. return prefix + t
  260. })
  261. }
  262. // now we have something like:
  263. // {b,c{d,e},{f,g}h}x{y,z}
  264. // walk through the set, expanding each part, until
  265. // the set ends. then, we'll expand the suffix.
  266. // If the set only has a single member, then'll put the {} back
  267. // first, handle numeric sets, since they're easier
  268. var numset = pattern.match(/^\{(-?[0-9]+)\.\.(-?[0-9]+)\}/)
  269. if (numset) {
  270. this.debug("numset", numset[1], numset[2])
  271. var suf = braceExpand.call(this, pattern.substr(numset[0].length), options)
  272. , start = +numset[1]
  273. , end = +numset[2]
  274. , inc = start > end ? -1 : 1
  275. , set = []
  276. for (var i = start; i != (end + inc); i += inc) {
  277. // append all the suffixes
  278. for (var ii = 0, ll = suf.length; ii < ll; ii ++) {
  279. set.push(i + suf[ii])
  280. }
  281. }
  282. return set
  283. }
  284. // ok, walk through the set
  285. // We hope, somewhat optimistically, that there
  286. // will be a } at the end.
  287. // If the closing brace isn't found, then the pattern is
  288. // interpreted as braceExpand("\\" + pattern) so that
  289. // the leading \{ will be interpreted literally.
  290. var i = 1 // skip the \{
  291. , depth = 1
  292. , set = []
  293. , member = ""
  294. , sawEnd = false
  295. , escaping = false
  296. function addMember () {
  297. set.push(member)
  298. member = ""
  299. }
  300. this.debug("Entering for")
  301. FOR: for (i = 1, l = pattern.length; i < l; i ++) {
  302. var c = pattern.charAt(i)
  303. this.debug("", i, c)
  304. if (escaping) {
  305. escaping = false
  306. member += "\\" + c
  307. } else {
  308. switch (c) {
  309. case "\\":
  310. escaping = true
  311. continue
  312. case "{":
  313. depth ++
  314. member += "{"
  315. continue
  316. case "}":
  317. depth --
  318. // if this closes the actual set, then we're done
  319. if (depth === 0) {
  320. addMember()
  321. // pluck off the close-brace
  322. i ++
  323. break FOR
  324. } else {
  325. member += c
  326. continue
  327. }
  328. case ",":
  329. if (depth === 1) {
  330. addMember()
  331. } else {
  332. member += c
  333. }
  334. continue
  335. default:
  336. member += c
  337. continue
  338. } // switch
  339. } // else
  340. } // for
  341. // now we've either finished the set, and the suffix is
  342. // pattern.substr(i), or we have *not* closed the set,
  343. // and need to escape the leading brace
  344. if (depth !== 0) {
  345. this.debug("didn't close", pattern)
  346. return braceExpand.call(this, "\\" + pattern, options)
  347. }
  348. // x{y,z} -> ["xy", "xz"]
  349. this.debug("set", set)
  350. this.debug("suffix", pattern.substr(i))
  351. var suf = braceExpand.call(this, pattern.substr(i), options)
  352. // ["b", "c{d,e}","{f,g}h"] ->
  353. // [["b"], ["cd", "ce"], ["fh", "gh"]]
  354. var addBraces = set.length === 1
  355. this.debug("set pre-expanded", set)
  356. set = set.map(function (p) {
  357. return braceExpand.call(this, p, options)
  358. }, this)
  359. this.debug("set expanded", set)
  360. // [["b"], ["cd", "ce"], ["fh", "gh"]] ->
  361. // ["b", "cd", "ce", "fh", "gh"]
  362. set = set.reduce(function (l, r) {
  363. return l.concat(r)
  364. })
  365. if (addBraces) {
  366. set = set.map(function (s) {
  367. return "{" + s + "}"
  368. })
  369. }
  370. // now attach the suffixes.
  371. var ret = []
  372. for (var i = 0, l = set.length; i < l; i ++) {
  373. for (var ii = 0, ll = suf.length; ii < ll; ii ++) {
  374. ret.push(set[i] + suf[ii])
  375. }
  376. }
  377. return ret
  378. }
  379. // parse a component of the expanded set.
  380. // At this point, no pattern may contain "/" in it
  381. // so we're going to return a 2d array, where each entry is the full
  382. // pattern, split on '/', and then turned into a regular expression.
  383. // A regexp is made at the end which joins each array with an
  384. // escaped /, and another full one which joins each regexp with |.
  385. //
  386. // Following the lead of Bash 4.1, note that "**" only has special meaning
  387. // when it is the *only* thing in a path portion. Otherwise, any series
  388. // of * is equivalent to a single *. Globstar behavior is enabled by
  389. // default, and can be disabled by setting options.noglobstar.
  390. Minimatch.prototype.parse = parse
  391. var SUBPARSE = {}
  392. function parse (pattern, isSub) {
  393. var options = this.options
  394. // shortcuts
  395. if (!options.noglobstar && pattern === "**") return GLOBSTAR
  396. if (pattern === "") return ""
  397. var re = ""
  398. , hasMagic = !!options.nocase
  399. , escaping = false
  400. // ? => one single character
  401. , patternListStack = []
  402. , plType
  403. , stateChar
  404. , inClass = false
  405. , reClassStart = -1
  406. , classStart = -1
  407. // . and .. never match anything that doesn't start with .,
  408. // even when options.dot is set.
  409. , patternStart = pattern.charAt(0) === "." ? "" // anything
  410. // not (start or / followed by . or .. followed by / or end)
  411. : options.dot ? "(?!(?:^|\\\/)\\.{1,2}(?:$|\\\/))"
  412. : "(?!\\.)"
  413. , self = this
  414. function clearStateChar () {
  415. if (stateChar) {
  416. // we had some state-tracking character
  417. // that wasn't consumed by this pass.
  418. switch (stateChar) {
  419. case "*":
  420. re += star
  421. hasMagic = true
  422. break
  423. case "?":
  424. re += qmark
  425. hasMagic = true
  426. break
  427. default:
  428. re += "\\"+stateChar
  429. break
  430. }
  431. self.debug('clearStateChar %j %j', stateChar, re)
  432. stateChar = false
  433. }
  434. }
  435. for ( var i = 0, len = pattern.length, c
  436. ; (i < len) && (c = pattern.charAt(i))
  437. ; i ++ ) {
  438. this.debug("%s\t%s %s %j", pattern, i, re, c)
  439. // skip over any that are escaped.
  440. if (escaping && reSpecials[c]) {
  441. re += "\\" + c
  442. escaping = false
  443. continue
  444. }
  445. SWITCH: switch (c) {
  446. case "/":
  447. // completely not allowed, even escaped.
  448. // Should already be path-split by now.
  449. return false
  450. case "\\":
  451. clearStateChar()
  452. escaping = true
  453. continue
  454. // the various stateChar values
  455. // for the "extglob" stuff.
  456. case "?":
  457. case "*":
  458. case "+":
  459. case "@":
  460. case "!":
  461. this.debug("%s\t%s %s %j <-- stateChar", pattern, i, re, c)
  462. // all of those are literals inside a class, except that
  463. // the glob [!a] means [^a] in regexp
  464. if (inClass) {
  465. this.debug(' in class')
  466. if (c === "!" && i === classStart + 1) c = "^"
  467. re += c
  468. continue
  469. }
  470. // if we already have a stateChar, then it means
  471. // that there was something like ** or +? in there.
  472. // Handle the stateChar, then proceed with this one.
  473. self.debug('call clearStateChar %j', stateChar)
  474. clearStateChar()
  475. stateChar = c
  476. // if extglob is disabled, then +(asdf|foo) isn't a thing.
  477. // just clear the statechar *now*, rather than even diving into
  478. // the patternList stuff.
  479. if (options.noext) clearStateChar()
  480. continue
  481. case "(":
  482. if (inClass) {
  483. re += "("
  484. continue
  485. }
  486. if (!stateChar) {
  487. re += "\\("
  488. continue
  489. }
  490. plType = stateChar
  491. patternListStack.push({ type: plType
  492. , start: i - 1
  493. , reStart: re.length })
  494. // negation is (?:(?!js)[^/]*)
  495. re += stateChar === "!" ? "(?:(?!" : "(?:"
  496. this.debug('plType %j %j', stateChar, re)
  497. stateChar = false
  498. continue
  499. case ")":
  500. if (inClass || !patternListStack.length) {
  501. re += "\\)"
  502. continue
  503. }
  504. clearStateChar()
  505. hasMagic = true
  506. re += ")"
  507. plType = patternListStack.pop().type
  508. // negation is (?:(?!js)[^/]*)
  509. // The others are (?:<pattern>)<type>
  510. switch (plType) {
  511. case "!":
  512. re += "[^/]*?)"
  513. break
  514. case "?":
  515. case "+":
  516. case "*": re += plType
  517. case "@": break // the default anyway
  518. }
  519. continue
  520. case "|":
  521. if (inClass || !patternListStack.length || escaping) {
  522. re += "\\|"
  523. escaping = false
  524. continue
  525. }
  526. clearStateChar()
  527. re += "|"
  528. continue
  529. // these are mostly the same in regexp and glob
  530. case "[":
  531. // swallow any state-tracking char before the [
  532. clearStateChar()
  533. if (inClass) {
  534. re += "\\" + c
  535. continue
  536. }
  537. inClass = true
  538. classStart = i
  539. reClassStart = re.length
  540. re += c
  541. continue
  542. case "]":
  543. // a right bracket shall lose its special
  544. // meaning and represent itself in
  545. // a bracket expression if it occurs
  546. // first in the list. -- POSIX.2 2.8.3.2
  547. if (i === classStart + 1 || !inClass) {
  548. re += "\\" + c
  549. escaping = false
  550. continue
  551. }
  552. // finish up the class.
  553. hasMagic = true
  554. inClass = false
  555. re += c
  556. continue
  557. default:
  558. // swallow any state char that wasn't consumed
  559. clearStateChar()
  560. if (escaping) {
  561. // no need
  562. escaping = false
  563. } else if (reSpecials[c]
  564. && !(c === "^" && inClass)) {
  565. re += "\\"
  566. }
  567. re += c
  568. } // switch
  569. } // for
  570. // handle the case where we left a class open.
  571. // "[abc" is valid, equivalent to "\[abc"
  572. if (inClass) {
  573. // split where the last [ was, and escape it
  574. // this is a huge pita. We now have to re-walk
  575. // the contents of the would-be class to re-translate
  576. // any characters that were passed through as-is
  577. var cs = pattern.substr(classStart + 1)
  578. , sp = this.parse(cs, SUBPARSE)
  579. re = re.substr(0, reClassStart) + "\\[" + sp[0]
  580. hasMagic = hasMagic || sp[1]
  581. }
  582. // handle the case where we had a +( thing at the *end*
  583. // of the pattern.
  584. // each pattern list stack adds 3 chars, and we need to go through
  585. // and escape any | chars that were passed through as-is for the regexp.
  586. // Go through and escape them, taking care not to double-escape any
  587. // | chars that were already escaped.
  588. var pl
  589. while (pl = patternListStack.pop()) {
  590. var tail = re.slice(pl.reStart + 3)
  591. // maybe some even number of \, then maybe 1 \, followed by a |
  592. tail = tail.replace(/((?:\\{2})*)(\\?)\|/g, function (_, $1, $2) {
  593. if (!$2) {
  594. // the | isn't already escaped, so escape it.
  595. $2 = "\\"
  596. }
  597. // need to escape all those slashes *again*, without escaping the
  598. // one that we need for escaping the | character. As it works out,
  599. // escaping an even number of slashes can be done by simply repeating
  600. // it exactly after itself. That's why this trick works.
  601. //
  602. // I am sorry that you have to see this.
  603. return $1 + $1 + $2 + "|"
  604. })
  605. this.debug("tail=%j\n %s", tail, tail)
  606. var t = pl.type === "*" ? star
  607. : pl.type === "?" ? qmark
  608. : "\\" + pl.type
  609. hasMagic = true
  610. re = re.slice(0, pl.reStart)
  611. + t + "\\("
  612. + tail
  613. }
  614. // handle trailing things that only matter at the very end.
  615. clearStateChar()
  616. if (escaping) {
  617. // trailing \\
  618. re += "\\\\"
  619. }
  620. // only need to apply the nodot start if the re starts with
  621. // something that could conceivably capture a dot
  622. var addPatternStart = false
  623. switch (re.charAt(0)) {
  624. case ".":
  625. case "[":
  626. case "(": addPatternStart = true
  627. }
  628. // if the re is not "" at this point, then we need to make sure
  629. // it doesn't match against an empty path part.
  630. // Otherwise a/* will match a/, which it should not.
  631. if (re !== "" && hasMagic) re = "(?=.)" + re
  632. if (addPatternStart) re = patternStart + re
  633. // parsing just a piece of a larger pattern.
  634. if (isSub === SUBPARSE) {
  635. return [ re, hasMagic ]
  636. }
  637. // skip the regexp for non-magical patterns
  638. // unescape anything in it, though, so that it'll be
  639. // an exact match against a file etc.
  640. if (!hasMagic) {
  641. return globUnescape(pattern)
  642. }
  643. var flags = options.nocase ? "i" : ""
  644. , regExp = new RegExp("^" + re + "$", flags)
  645. regExp._glob = pattern
  646. regExp._src = re
  647. return regExp
  648. }
  649. minimatch.makeRe = function (pattern, options) {
  650. return new Minimatch(pattern, options || {}).makeRe()
  651. }
  652. Minimatch.prototype.makeRe = makeRe
  653. function makeRe () {
  654. if (this.regexp || this.regexp === false) return this.regexp
  655. // at this point, this.set is a 2d array of partial
  656. // pattern strings, or "**".
  657. //
  658. // It's better to use .match(). This function shouldn't
  659. // be used, really, but it's pretty convenient sometimes,
  660. // when you just want to work with a regex.
  661. var set = this.set
  662. if (!set.length) return this.regexp = false
  663. var options = this.options
  664. var twoStar = options.noglobstar ? star
  665. : options.dot ? twoStarDot
  666. : twoStarNoDot
  667. , flags = options.nocase ? "i" : ""
  668. var re = set.map(function (pattern) {
  669. return pattern.map(function (p) {
  670. return (p === GLOBSTAR) ? twoStar
  671. : (typeof p === "string") ? regExpEscape(p)
  672. : p._src
  673. }).join("\\\/")
  674. }).join("|")
  675. // must match entire pattern
  676. // ending in a * or ** will make it less strict.
  677. re = "^(?:" + re + ")$"
  678. // can match anything, as long as it's not this.
  679. if (this.negate) re = "^(?!" + re + ").*$"
  680. try {
  681. return this.regexp = new RegExp(re, flags)
  682. } catch (ex) {
  683. return this.regexp = false
  684. }
  685. }
  686. minimatch.match = function (list, pattern, options) {
  687. var mm = new Minimatch(pattern, options)
  688. list = list.filter(function (f) {
  689. return mm.match(f)
  690. })
  691. if (options.nonull && !list.length) {
  692. list.push(pattern)
  693. }
  694. return list
  695. }
  696. Minimatch.prototype.match = match
  697. function match (f, partial) {
  698. this.debug("match", f, this.pattern)
  699. // short-circuit in the case of busted things.
  700. // comments, etc.
  701. if (this.comment) return false
  702. if (this.empty) return f === ""
  703. if (f === "/" && partial) return true
  704. var options = this.options
  705. // windows: need to use /, not \
  706. // On other platforms, \ is a valid (albeit bad) filename char.
  707. if (platform === "win32") {
  708. f = f.split("\\").join("/")
  709. }
  710. // treat the test path as a set of pathparts.
  711. f = f.split(slashSplit)
  712. this.debug(this.pattern, "split", f)
  713. // just ONE of the pattern sets in this.set needs to match
  714. // in order for it to be valid. If negating, then just one
  715. // match means that we have failed.
  716. // Either way, return on the first hit.
  717. var set = this.set
  718. this.debug(this.pattern, "set", set)
  719. var splitFile = path.basename(f.join("/")).split("/")
  720. for (var i = 0, l = set.length; i < l; i ++) {
  721. var pattern = set[i], file = f
  722. if (options.matchBase && pattern.length === 1) {
  723. file = splitFile
  724. }
  725. var hit = this.matchOne(file, pattern, partial)
  726. if (hit) {
  727. if (options.flipNegate) return true
  728. return !this.negate
  729. }
  730. }
  731. // didn't get any hits. this is success if it's a negative
  732. // pattern, failure otherwise.
  733. if (options.flipNegate) return false
  734. return this.negate
  735. }
  736. // set partial to true to test if, for example,
  737. // "/a/b" matches the start of "/*/b/*/d"
  738. // Partial means, if you run out of file before you run
  739. // out of pattern, then that's fine, as long as all
  740. // the parts match.
  741. Minimatch.prototype.matchOne = function (file, pattern, partial) {
  742. var options = this.options
  743. this.debug("matchOne",
  744. { "this": this
  745. , file: file
  746. , pattern: pattern })
  747. this.debug("matchOne", file.length, pattern.length)
  748. for ( var fi = 0
  749. , pi = 0
  750. , fl = file.length
  751. , pl = pattern.length
  752. ; (fi < fl) && (pi < pl)
  753. ; fi ++, pi ++ ) {
  754. this.debug("matchOne loop")
  755. var p = pattern[pi]
  756. , f = file[fi]
  757. this.debug(pattern, p, f)
  758. // should be impossible.
  759. // some invalid regexp stuff in the set.
  760. if (p === false) return false
  761. if (p === GLOBSTAR) {
  762. this.debug('GLOBSTAR', [pattern, p, f])
  763. // "**"
  764. // a/**/b/**/c would match the following:
  765. // a/b/x/y/z/c
  766. // a/x/y/z/b/c
  767. // a/b/x/b/x/c
  768. // a/b/c
  769. // To do this, take the rest of the pattern after
  770. // the **, and see if it would match the file remainder.
  771. // If so, return success.
  772. // If not, the ** "swallows" a segment, and try again.
  773. // This is recursively awful.
  774. //
  775. // a/**/b/**/c matching a/b/x/y/z/c
  776. // - a matches a
  777. // - doublestar
  778. // - matchOne(b/x/y/z/c, b/**/c)
  779. // - b matches b
  780. // - doublestar
  781. // - matchOne(x/y/z/c, c) -> no
  782. // - matchOne(y/z/c, c) -> no
  783. // - matchOne(z/c, c) -> no
  784. // - matchOne(c, c) yes, hit
  785. var fr = fi
  786. , pr = pi + 1
  787. if (pr === pl) {
  788. this.debug('** at the end')
  789. // a ** at the end will just swallow the rest.
  790. // We have found a match.
  791. // however, it will not swallow /.x, unless
  792. // options.dot is set.
  793. // . and .. are *never* matched by **, for explosively
  794. // exponential reasons.
  795. for ( ; fi < fl; fi ++) {
  796. if (file[fi] === "." || file[fi] === ".." ||
  797. (!options.dot && file[fi].charAt(0) === ".")) return false
  798. }
  799. return true
  800. }
  801. // ok, let's see if we can swallow whatever we can.
  802. WHILE: while (fr < fl) {
  803. var swallowee = file[fr]
  804. this.debug('\nglobstar while',
  805. file, fr, pattern, pr, swallowee)
  806. // XXX remove this slice. Just pass the start index.
  807. if (this.matchOne(file.slice(fr), pattern.slice(pr), partial)) {
  808. this.debug('globstar found match!', fr, fl, swallowee)
  809. // found a match.
  810. return true
  811. } else {
  812. // can't swallow "." or ".." ever.
  813. // can only swallow ".foo" when explicitly asked.
  814. if (swallowee === "." || swallowee === ".." ||
  815. (!options.dot && swallowee.charAt(0) === ".")) {
  816. this.debug("dot detected!", file, fr, pattern, pr)
  817. break WHILE
  818. }
  819. // ** swallows a segment, and continue.
  820. this.debug('globstar swallow a segment, and continue')
  821. fr ++
  822. }
  823. }
  824. // no match was found.
  825. // However, in partial mode, we can't say this is necessarily over.
  826. // If there's more *pattern* left, then
  827. if (partial) {
  828. // ran out of file
  829. this.debug("\n>>> no match, partial?", file, fr, pattern, pr)
  830. if (fr === fl) return true
  831. }
  832. return false
  833. }
  834. // something other than **
  835. // non-magic patterns just have to match exactly
  836. // patterns with magic have been turned into regexps.
  837. var hit
  838. if (typeof p === "string") {
  839. if (options.nocase) {
  840. hit = f.toLowerCase() === p.toLowerCase()
  841. } else {
  842. hit = f === p
  843. }
  844. this.debug("string match", p, f, hit)
  845. } else {
  846. hit = f.match(p)
  847. this.debug("pattern match", p, f, hit)
  848. }
  849. if (!hit) return false
  850. }
  851. // Note: ending in / means that we'll get a final ""
  852. // at the end of the pattern. This can only match a
  853. // corresponding "" at the end of the file.
  854. // If the file ends in /, then it can only match a
  855. // a pattern that ends in /, unless the pattern just
  856. // doesn't have any more for it. But, a/b/ should *not*
  857. // match "a/b/*", even though "" matches against the
  858. // [^/]*? pattern, except in partial mode, where it might
  859. // simply not be reached yet.
  860. // However, a/b/ should still satisfy a/*
  861. // now either we fell off the end of the pattern, or we're done.
  862. if (fi === fl && pi === pl) {
  863. // ran out of pattern and filename at the same time.
  864. // an exact hit!
  865. return true
  866. } else if (fi === fl) {
  867. // ran out of file, but still had pattern left.
  868. // this is ok if we're doing the match as part of
  869. // a glob fs traversal.
  870. return partial
  871. } else if (pi === pl) {
  872. // ran out of pattern, still have file left.
  873. // this is only acceptable if we're on the very last
  874. // empty segment of a file with a trailing slash.
  875. // a/* should match a/b/
  876. var emptyFileEnd = (fi === fl - 1) && (file[fi] === "")
  877. return emptyFileEnd
  878. }
  879. // should be unreachable.
  880. throw new Error("wtf?")
  881. }
  882. // replace stuff like \* with *
  883. function globUnescape (s) {
  884. return s.replace(/\\(.)/g, "$1")
  885. }
  886. function regExpEscape (s) {
  887. return s.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&")
  888. }
  889. })( typeof require === "function" ? require : null,
  890. this,
  891. typeof module === "object" ? module : null,
  892. typeof process === "object" ? process.platform : "win32"
  893. )