afd5bf8874a56b70d72375fa35d5eceec3900e9f342149d721ac6142de3c8a84762617d71ffa05b445a311a1c3db4443851482d68b090f2e5d0973425fabc4 30 KB

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