123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- exports.alphasort = alphasort
- exports.alphasorti = alphasorti
- exports.isAbsolute = process.platform === "win32" ? absWin : absUnix
- exports.setopts = setopts
- exports.ownProp = ownProp
- exports.makeAbs = makeAbs
- exports.finish = finish
- exports.mark = mark
- exports.isIgnored = isIgnored
- exports.childrenIgnored = childrenIgnored
- function ownProp (obj, field) {
- return Object.prototype.hasOwnProperty.call(obj, field)
- }
- var path = require("path")
- var minimatch = require("minimatch")
- var Minimatch = minimatch.Minimatch
- function absWin (p) {
- if (absUnix(p)) return true
-
-
- var splitDeviceRe =
- /^([a-zA-Z]:|[\\\/]{2}[^\\\/]+[\\\/]+[^\\\/]+)?([\\\/])?([\s\S]*?)$/
- var result = splitDeviceRe.exec(p)
- var device = result[1] || ''
- var isUnc = device && device.charAt(1) !== ':'
- var isAbsolute = !!result[2] || isUnc
- return isAbsolute
- }
- function absUnix (p) {
- return p.charAt(0) === "/" || p === ""
- }
- function alphasorti (a, b) {
- return a.toLowerCase().localeCompare(b.toLowerCase())
- }
- function alphasort (a, b) {
- return a.localeCompare(b)
- }
- function setupIgnores (self, options) {
- self.ignore = options.ignore || []
- if (!Array.isArray(self.ignore))
- self.ignore = [self.ignore]
- if (self.ignore.length) {
- self.ignore = self.ignore.map(ignoreMap)
- }
- }
- function ignoreMap (pattern) {
- var gmatcher = null
- if (pattern.slice(-3) === '/**') {
- var gpattern = pattern.replace(/(\/\*\*)+$/, '')
- gmatcher = new Minimatch(gpattern, { nonegate: true })
- }
- return {
- matcher: new Minimatch(pattern, { nonegate: true }),
- gmatcher: gmatcher
- }
- }
- function setopts (self, pattern, options) {
- if (!options)
- options = {}
-
- if (options.matchBase && -1 === pattern.indexOf("/")) {
- if (options.noglobstar) {
- throw new Error("base matching requires globstar")
- }
- pattern = "**/" + pattern
- }
- self.pattern = pattern
- self.strict = options.strict !== false
- self.realpath = !!options.realpath
- self.realpathCache = options.realpathCache || Object.create(null)
- self.follow = !!options.follow
- self.dot = !!options.dot
- self.mark = !!options.mark
- self.nodir = !!options.nodir
- if (self.nodir)
- self.mark = true
- self.sync = !!options.sync
- self.nounique = !!options.nounique
- self.nonull = !!options.nonull
- self.nosort = !!options.nosort
- self.nocase = !!options.nocase
- self.stat = !!options.stat
- self.noprocess = !!options.noprocess
- self.maxLength = options.maxLength || Infinity
- self.cache = options.cache || Object.create(null)
- self.statCache = options.statCache || Object.create(null)
- self.symlinks = options.symlinks || Object.create(null)
- setupIgnores(self, options)
- self.changedCwd = false
- var cwd = process.cwd()
- if (!ownProp(options, "cwd"))
- self.cwd = cwd
- else {
- self.cwd = options.cwd
- self.changedCwd = path.resolve(options.cwd) !== cwd
- }
- self.root = options.root || path.resolve(self.cwd, "/")
- self.root = path.resolve(self.root)
- if (process.platform === "win32")
- self.root = self.root.replace(/\\/g, "/")
- self.nomount = !!options.nomount
- self.minimatch = new Minimatch(pattern, options)
- self.options = self.minimatch.options
- }
- function finish (self) {
- var nou = self.nounique
- var all = nou ? [] : Object.create(null)
- for (var i = 0, l = self.matches.length; i < l; i ++) {
- var matches = self.matches[i]
- if (!matches || Object.keys(matches).length === 0) {
- if (self.nonull) {
-
- var literal = self.minimatch.globSet[i]
- if (nou)
- all.push(literal)
- else
- all[literal] = true
- }
- } else {
-
- var m = Object.keys(matches)
- if (nou)
- all.push.apply(all, m)
- else
- m.forEach(function (m) {
- all[m] = true
- })
- }
- }
- if (!nou)
- all = Object.keys(all)
- if (!self.nosort)
- all = all.sort(self.nocase ? alphasorti : alphasort)
-
- if (self.mark) {
- for (var i = 0; i < all.length; i++) {
- all[i] = self._mark(all[i])
- }
- if (self.nodir) {
- all = all.filter(function (e) {
- return !(/\/$/.test(e))
- })
- }
- }
- if (self.ignore.length)
- all = all.filter(function(m) {
- return !isIgnored(self, m)
- })
- self.found = all
- }
- function mark (self, p) {
- var abs = makeAbs(self, p)
- var c = self.cache[abs]
- var m = p
- if (c) {
- var isDir = c === 'DIR' || Array.isArray(c)
- var slash = p.slice(-1) === '/'
- if (isDir && !slash)
- m += '/'
- else if (!isDir && slash)
- m = m.slice(0, -1)
- if (m !== p) {
- var mabs = makeAbs(self, m)
- self.statCache[mabs] = self.statCache[abs]
- self.cache[mabs] = self.cache[abs]
- }
- }
- return m
- }
- function makeAbs (self, f) {
- var abs = f
- if (f.charAt(0) === '/') {
- abs = path.join(self.root, f)
- } else if (exports.isAbsolute(f)) {
- abs = f
- } else if (self.changedCwd) {
- abs = path.resolve(self.cwd, f)
- } else if (self.realpath) {
- abs = path.resolve(f)
- }
- return abs
- }
- function isIgnored (self, path) {
- if (!self.ignore.length)
- return false
- return self.ignore.some(function(item) {
- return item.matcher.match(path) || !!(item.gmatcher && item.gmatcher.match(path))
- })
- }
- function childrenIgnored (self, path) {
- if (!self.ignore.length)
- return false
- return self.ignore.some(function(item) {
- return !!(item.gmatcher && item.gmatcher.match(path))
- })
- }
|