123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947 |
- module.exports = minimatch
- minimatch.Minimatch = Minimatch
- var path = (function () { try { return require('path') } catch (e) {}}()) || {
- sep: '/'
- }
- minimatch.sep = path.sep
- var GLOBSTAR = minimatch.GLOBSTAR = Minimatch.GLOBSTAR = {}
- var expand = require('brace-expansion')
- var plTypes = {
- '!': { open: '(?:(?!(?:', close: '))[^/]*?)'},
- '?': { open: '(?:', close: ')?' },
- '+': { open: '(?:', close: ')+' },
- '*': { open: '(?:', close: ')*' },
- '@': { open: '(?:', close: ')' }
- }
- var qmark = '[^/]'
- var star = qmark + '*?'
- var twoStarDot = '(?:(?!(?:\\\/|^)(?:\\.{1,2})($|\\\/)).)*?'
- var twoStarNoDot = '(?:(?!(?:\\\/|^)\\.).)*?'
- var reSpecials = charSet('().*{}+?[]^$\\!')
- function charSet (s) {
- return s.split('').reduce(function (set, c) {
- set[c] = true
- return set
- }, {})
- }
- var slashSplit = /\/+/
- minimatch.filter = filter
- function filter (pattern, options) {
- options = options || {}
- return function (p, i, list) {
- return minimatch(p, pattern, options)
- }
- }
- function ext (a, b) {
- b = b || {}
- var t = {}
- Object.keys(a).forEach(function (k) {
- t[k] = a[k]
- })
- Object.keys(b).forEach(function (k) {
- t[k] = b[k]
- })
- return t
- }
- minimatch.defaults = function (def) {
- if (!def || typeof def !== 'object' || !Object.keys(def).length) {
- return minimatch
- }
- var orig = minimatch
- var m = function minimatch (p, pattern, options) {
- return orig(p, pattern, ext(def, options))
- }
- m.Minimatch = function Minimatch (pattern, options) {
- return new orig.Minimatch(pattern, ext(def, options))
- }
- m.Minimatch.defaults = function defaults (options) {
- return orig.defaults(ext(def, options)).Minimatch
- }
- m.filter = function filter (pattern, options) {
- return orig.filter(pattern, ext(def, options))
- }
- m.defaults = function defaults (options) {
- return orig.defaults(ext(def, options))
- }
- m.makeRe = function makeRe (pattern, options) {
- return orig.makeRe(pattern, ext(def, options))
- }
- m.braceExpand = function braceExpand (pattern, options) {
- return orig.braceExpand(pattern, ext(def, options))
- }
- m.match = function (list, pattern, options) {
- return orig.match(list, pattern, ext(def, options))
- }
- return m
- }
- Minimatch.defaults = function (def) {
- return minimatch.defaults(def).Minimatch
- }
- function minimatch (p, pattern, options) {
- assertValidPattern(pattern)
- if (!options) options = {}
-
- if (!options.nocomment && pattern.charAt(0) === '#') {
- return false
- }
- return new Minimatch(pattern, options).match(p)
- }
- function Minimatch (pattern, options) {
- if (!(this instanceof Minimatch)) {
- return new Minimatch(pattern, options)
- }
- assertValidPattern(pattern)
- if (!options) options = {}
- pattern = pattern.trim()
-
- if (!options.allowWindowsEscape && path.sep !== '/') {
- pattern = pattern.split(path.sep).join('/')
- }
- this.options = options
- this.set = []
- this.pattern = pattern
- this.regexp = null
- this.negate = false
- this.comment = false
- this.empty = false
- this.partial = !!options.partial
-
- this.make()
- }
- Minimatch.prototype.debug = function () {}
- Minimatch.prototype.make = make
- function make () {
- var pattern = this.pattern
- var options = this.options
-
- if (!options.nocomment && pattern.charAt(0) === '#') {
- this.comment = true
- return
- }
- if (!pattern) {
- this.empty = true
- return
- }
-
- this.parseNegate()
-
- var set = this.globSet = this.braceExpand()
- if (options.debug) this.debug = function debug() { console.error.apply(console, arguments) }
- this.debug(this.pattern, set)
- // step 3: now we have a set, so turn each one into a series of path-portion
- // matching patterns.
- // These will be regexps, except in the case of "**", which is
- // set to the GLOBSTAR object for globstar behavior,
- // and will not contain any / characters
- set = this.globParts = set.map(function (s) {
- return s.split(slashSplit)
- })
- this.debug(this.pattern, set)
- // glob --> regexps
- set = set.map(function (s, si, set) {
- return s.map(this.parse, this)
- }, this)
- this.debug(this.pattern, set)
- // filter out everything that didn't compile properly.
- set = set.filter(function (s) {
- return s.indexOf(false) === -1
- })
- this.debug(this.pattern, set)
- this.set = set
- }
- Minimatch.prototype.parseNegate = parseNegate
- function parseNegate () {
- var pattern = this.pattern
- var negate = false
- var options = this.options
- var negateOffset = 0
- if (options.nonegate) return
- for (var i = 0, l = pattern.length
- ; i < l && pattern.charAt(i) === '!'
- ; i++) {
- negate = !negate
- negateOffset++
- }
- if (negateOffset) this.pattern = pattern.substr(negateOffset)
- this.negate = negate
- }
- minimatch.braceExpand = function (pattern, options) {
- return braceExpand(pattern, options)
- }
- Minimatch.prototype.braceExpand = braceExpand
- function braceExpand (pattern, options) {
- if (!options) {
- if (this instanceof Minimatch) {
- options = this.options
- } else {
- options = {}
- }
- }
- pattern = typeof pattern === 'undefined'
- ? this.pattern : pattern
- assertValidPattern(pattern)
-
-
- if (options.nobrace || !/\{(?:(?!\{).)*\}/.test(pattern)) {
-
- return [pattern]
- }
- return expand(pattern)
- }
- var MAX_PATTERN_LENGTH = 1024 * 64
- var assertValidPattern = function (pattern) {
- if (typeof pattern !== 'string') {
- throw new TypeError('invalid pattern')
- }
- if (pattern.length > MAX_PATTERN_LENGTH) {
- throw new TypeError('pattern is too long')
- }
- }
- Minimatch.prototype.parse = parse
- var SUBPARSE = {}
- function parse (pattern, isSub) {
- assertValidPattern(pattern)
- var options = this.options
-
- if (pattern === '**') {
- if (!options.noglobstar)
- return GLOBSTAR
- else
- pattern = '*'
- }
- if (pattern === '') return ''
- var re = ''
- var hasMagic = !!options.nocase
- var escaping = false
-
- var patternListStack = []
- var negativeLists = []
- var stateChar
- var inClass = false
- var reClassStart = -1
- var classStart = -1
-
-
- var patternStart = pattern.charAt(0) === '.' ? ''
-
- : options.dot ? '(?!(?:^|\\\/)\\.{1,2}(?:$|\\\/))'
- : '(?!\\.)'
- var self = this
- function clearStateChar () {
- if (stateChar) {
-
-
- switch (stateChar) {
- case '*':
- re += star
- hasMagic = true
- break
- case '?':
- re += qmark
- hasMagic = true
- break
- default:
- re += '\\' + stateChar
- break
- }
- self.debug('clearStateChar %j %j', stateChar, re)
- stateChar = false
- }
- }
- for (var i = 0, len = pattern.length, c
- ; (i < len) && (c = pattern.charAt(i))
- ; i++) {
- this.debug('%s\t%s %s %j', pattern, i, re, c)
-
- if (escaping && reSpecials[c]) {
- re += '\\' + c
- escaping = false
- continue
- }
- switch (c) {
-
- case '/': {
-
-
- return false
- }
- case '\\':
- clearStateChar()
- escaping = true
- continue
-
-
- case '?':
- case '*':
- case '+':
- case '@':
- case '!':
- this.debug('%s\t%s %s %j <-- stateChar', pattern, i, re, c)
-
-
- if (inClass) {
- this.debug(' in class')
- if (c === '!' && i === classStart + 1) c = '^'
- re += c
- continue
- }
-
-
-
- self.debug('call clearStateChar %j', stateChar)
- clearStateChar()
- stateChar = c
-
-
-
- if (options.noext) clearStateChar()
- continue
- case '(':
- if (inClass) {
- re += '('
- continue
- }
- if (!stateChar) {
- re += '\\('
- continue
- }
- patternListStack.push({
- type: stateChar,
- start: i - 1,
- reStart: re.length,
- open: plTypes[stateChar].open,
- close: plTypes[stateChar].close
- })
-
- re += stateChar === '!' ? '(?:(?!(?:' : '(?:'
- this.debug('plType %j %j', stateChar, re)
- stateChar = false
- continue
- case ')':
- if (inClass || !patternListStack.length) {
- re += '\\)'
- continue
- }
- clearStateChar()
- hasMagic = true
- var pl = patternListStack.pop()
-
-
- re += pl.close
- if (pl.type === '!') {
- negativeLists.push(pl)
- }
- pl.reEnd = re.length
- continue
- case '|':
- if (inClass || !patternListStack.length || escaping) {
- re += '\\|'
- escaping = false
- continue
- }
- clearStateChar()
- re += '|'
- continue
-
- case '[':
-
- clearStateChar()
- if (inClass) {
- re += '\\' + c
- continue
- }
- inClass = true
- classStart = i
- reClassStart = re.length
- re += c
- continue
- case ']':
-
-
-
-
- if (i === classStart + 1 || !inClass) {
- re += '\\' + c
- escaping = false
- continue
- }
-
-
-
-
-
-
-
-
-
- var cs = pattern.substring(classStart + 1, i)
- try {
- RegExp('[' + cs + ']')
- } catch (er) {
-
- var sp = this.parse(cs, SUBPARSE)
- re = re.substr(0, reClassStart) + '\\[' + sp[0] + '\\]'
- hasMagic = hasMagic || sp[1]
- inClass = false
- continue
- }
-
- hasMagic = true
- inClass = false
- re += c
- continue
- default:
-
- clearStateChar()
- if (escaping) {
-
- escaping = false
- } else if (reSpecials[c]
- && !(c === '^' && inClass)) {
- re += '\\'
- }
- re += c
- }
- }
-
-
- if (inClass) {
-
-
-
-
- cs = pattern.substr(classStart + 1)
- sp = this.parse(cs, SUBPARSE)
- re = re.substr(0, reClassStart) + '\\[' + sp[0]
- hasMagic = hasMagic || sp[1]
- }
-
-
-
-
-
-
- for (pl = patternListStack.pop(); pl; pl = patternListStack.pop()) {
- var tail = re.slice(pl.reStart + pl.open.length)
- this.debug('setting tail', re, pl)
-
- tail = tail.replace(/((?:\\{2}){0,64})(\\?)\|/g, function (_, $1, $2) {
- if (!$2) {
-
- $2 = '\\'
- }
-
-
-
-
-
-
- return $1 + $1 + $2 + '|'
- })
- this.debug('tail=%j\n %s', tail, tail, pl, re)
- var t = pl.type === '*' ? star
- : pl.type === '?' ? qmark
- : '\\' + pl.type
- hasMagic = true
- re = re.slice(0, pl.reStart) + t + '\\(' + tail
- }
-
- clearStateChar()
- if (escaping) {
-
- re += '\\\\'
- }
-
-
- var addPatternStart = false
- switch (re.charAt(0)) {
- case '[': case '.': case '(': addPatternStart = true
- }
-
-
-
-
-
- for (var n = negativeLists.length - 1; n > -1; n--) {
- var nl = negativeLists[n]
- var nlBefore = re.slice(0, nl.reStart)
- var nlFirst = re.slice(nl.reStart, nl.reEnd - 8)
- var nlLast = re.slice(nl.reEnd - 8, nl.reEnd)
- var nlAfter = re.slice(nl.reEnd)
- nlLast += nlAfter
-
-
-
- var openParensBefore = nlBefore.split('(').length - 1
- var cleanAfter = nlAfter
- for (i = 0; i < openParensBefore; i++) {
- cleanAfter = cleanAfter.replace(/\)[+*?]?/, '')
- }
- nlAfter = cleanAfter
- var dollar = ''
- if (nlAfter === '' && isSub !== SUBPARSE) {
- dollar = '$'
- }
- var newRe = nlBefore + nlFirst + nlAfter + dollar + nlLast
- re = newRe
- }
-
-
-
- if (re !== '' && hasMagic) {
- re = '(?=.)' + re
- }
- if (addPatternStart) {
- re = patternStart + re
- }
-
- if (isSub === SUBPARSE) {
- return [re, hasMagic]
- }
-
-
-
- if (!hasMagic) {
- return globUnescape(pattern)
- }
- var flags = options.nocase ? 'i' : ''
- try {
- var regExp = new RegExp('^' + re + '$', flags)
- } catch (er) {
-
-
-
-
- return new RegExp('$.')
- }
- regExp._glob = pattern
- regExp._src = re
- return regExp
- }
- minimatch.makeRe = function (pattern, options) {
- return new Minimatch(pattern, options || {}).makeRe()
- }
- Minimatch.prototype.makeRe = makeRe
- function makeRe () {
- if (this.regexp || this.regexp === false) return this.regexp
-
-
-
-
-
-
- var set = this.set
- if (!set.length) {
- this.regexp = false
- return this.regexp
- }
- var options = this.options
- var twoStar = options.noglobstar ? star
- : options.dot ? twoStarDot
- : twoStarNoDot
- var flags = options.nocase ? 'i' : ''
- var re = set.map(function (pattern) {
- return pattern.map(function (p) {
- return (p === GLOBSTAR) ? twoStar
- : (typeof p === 'string') ? regExpEscape(p)
- : p._src
- }).join('\\\/')
- }).join('|')
-
-
- re = '^(?:' + re + ')$'
-
- if (this.negate) re = '^(?!' + re + ').*$'
- try {
- this.regexp = new RegExp(re, flags)
- } catch (ex) {
- this.regexp = false
- }
- return this.regexp
- }
- minimatch.match = function (list, pattern, options) {
- options = options || {}
- var mm = new Minimatch(pattern, options)
- list = list.filter(function (f) {
- return mm.match(f)
- })
- if (mm.options.nonull && !list.length) {
- list.push(pattern)
- }
- return list
- }
- Minimatch.prototype.match = function match (f, partial) {
- if (typeof partial === 'undefined') partial = this.partial
- this.debug('match', f, this.pattern)
-
-
- if (this.comment) return false
- if (this.empty) return f === ''
- if (f === '/' && partial) return true
- var options = this.options
-
- if (path.sep !== '/') {
- f = f.split(path.sep).join('/')
- }
-
- f = f.split(slashSplit)
- this.debug(this.pattern, 'split', f)
-
-
-
-
- var set = this.set
- this.debug(this.pattern, 'set', set)
- // Find the basename of the path by looking for the last non-empty segment
- var filename
- var i
- for (i = f.length - 1; i >= 0; i--) {
- filename = f[i]
- if (filename) break
- }
- for (i = 0; i < set.length; i++) {
- var pattern = set[i]
- var file = f
- if (options.matchBase && pattern.length === 1) {
- file = [filename]
- }
- var hit = this.matchOne(file, pattern, partial)
- if (hit) {
- if (options.flipNegate) return true
- return !this.negate
- }
- }
-
-
- if (options.flipNegate) return false
- return this.negate
- }
- Minimatch.prototype.matchOne = function (file, pattern, partial) {
- var options = this.options
- this.debug('matchOne',
- { 'this': this, file: file, pattern: pattern })
- this.debug('matchOne', file.length, pattern.length)
- for (var fi = 0,
- pi = 0,
- fl = file.length,
- pl = pattern.length
- ; (fi < fl) && (pi < pl)
- ; fi++, pi++) {
- this.debug('matchOne loop')
- var p = pattern[pi]
- var f = file[fi]
- this.debug(pattern, p, f)
-
-
-
- if (p === false) return false
- if (p === GLOBSTAR) {
- this.debug('GLOBSTAR', [pattern, p, f])
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- var fr = fi
- var pr = pi + 1
- if (pr === pl) {
- this.debug('** at the end')
-
-
-
-
-
-
- for (; fi < fl; fi++) {
- if (file[fi] === '.' || file[fi] === '..' ||
- (!options.dot && file[fi].charAt(0) === '.')) return false
- }
- return true
- }
-
- while (fr < fl) {
- var swallowee = file[fr]
- this.debug('\nglobstar while', file, fr, pattern, pr, swallowee)
-
- if (this.matchOne(file.slice(fr), pattern.slice(pr), partial)) {
- this.debug('globstar found match!', fr, fl, swallowee)
-
- return true
- } else {
-
-
- if (swallowee === '.' || swallowee === '..' ||
- (!options.dot && swallowee.charAt(0) === '.')) {
- this.debug('dot detected!', file, fr, pattern, pr)
- break
- }
-
- this.debug('globstar swallow a segment, and continue')
- fr++
- }
- }
-
-
-
-
- if (partial) {
-
- this.debug('\n>>> no match, partial?', file, fr, pattern, pr)
- if (fr === fl) return true
- }
- return false
- }
-
-
-
- var hit
- if (typeof p === 'string') {
- hit = f === p
- this.debug('string match', p, f, hit)
- } else {
- hit = f.match(p)
- this.debug('pattern match', p, f, hit)
- }
- if (!hit) return false
- }
-
-
-
-
-
-
-
-
-
-
-
- if (fi === fl && pi === pl) {
-
-
- return true
- } else if (fi === fl) {
-
-
-
- return partial
- } else if (pi === pl) {
-
-
-
-
- return (fi === fl - 1) && (file[fi] === '')
- }
-
-
- throw new Error('wtf?')
- }
- function globUnescape (s) {
- return s.replace(/\\(.)/g, '$1')
- }
- function regExpEscape (s) {
- return s.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&')
- }
|