1 exports.alphasort = alphasort
2 exports.alphasorti = alphasorti
3 exports.isAbsolute = process.platform === "win32" ? absWin : absUnix
4 exports.setopts = setopts
5 exports.ownProp = ownProp
6 exports.makeAbs = makeAbs
7 exports.finish = finish
9 exports.isIgnored = isIgnored
10 exports.childrenIgnored = childrenIgnored
12 function ownProp (obj, field) {
13 return Object.prototype.hasOwnProperty.call(obj, field)
16 var path = require("path")
17 var minimatch = require("minimatch")
18 var Minimatch = minimatch.Minimatch
21 if (absUnix(p)) return true
22 // pull off the device/UNC bit from a windows path.
23 // from node's lib/path.js
25 /^([a-zA-Z]:|[\\\/]{2}[^\\\/]+[\\\/]+[^\\\/]+)?([\\\/])?([\s\S]*?)$/
26 var result = splitDeviceRe.exec(p)
27 var device = result[1] || ''
28 var isUnc = device && device.charAt(1) !== ':'
29 var isAbsolute = !!result[2] || isUnc // UNC paths are always absolute
34 function absUnix (p) {
35 return p.charAt(0) === "/" || p === ""
38 function alphasorti (a, b) {
39 return a.toLowerCase().localeCompare(b.toLowerCase())
42 function alphasort (a, b) {
43 return a.localeCompare(b)
46 function setupIgnores (self, options) {
47 self.ignore = options.ignore || []
49 if (!Array.isArray(self.ignore))
50 self.ignore = [self.ignore]
52 if (self.ignore.length) {
53 self.ignore = self.ignore.map(ignoreMap)
57 function ignoreMap (pattern) {
59 if (pattern.slice(-3) === '/**') {
60 var gpattern = pattern.replace(/(\/\*\*)+$/, '')
61 gmatcher = new Minimatch(gpattern, { nonegate: true })
65 matcher: new Minimatch(pattern, { nonegate: true }),
70 function setopts (self, pattern, options) {
74 // base-matching: just use globstar for that.
75 if (options.matchBase && -1 === pattern.indexOf("/")) {
76 if (options.noglobstar) {
77 throw new Error("base matching requires globstar")
79 pattern = "**/" + pattern
82 self.pattern = pattern
83 self.strict = options.strict !== false
84 self.realpath = !!options.realpath
85 self.realpathCache = options.realpathCache || Object.create(null)
86 self.follow = !!options.follow
87 self.dot = !!options.dot
88 self.mark = !!options.mark
89 self.nodir = !!options.nodir
92 self.sync = !!options.sync
93 self.nounique = !!options.nounique
94 self.nonull = !!options.nonull
95 self.nosort = !!options.nosort
96 self.nocase = !!options.nocase
97 self.stat = !!options.stat
98 self.noprocess = !!options.noprocess
100 self.maxLength = options.maxLength || Infinity
101 self.cache = options.cache || Object.create(null)
102 self.statCache = options.statCache || Object.create(null)
103 self.symlinks = options.symlinks || Object.create(null)
105 setupIgnores(self, options)
107 self.changedCwd = false
108 var cwd = process.cwd()
109 if (!ownProp(options, "cwd"))
112 self.cwd = options.cwd
113 self.changedCwd = path.resolve(options.cwd) !== cwd
116 self.root = options.root || path.resolve(self.cwd, "/")
117 self.root = path.resolve(self.root)
118 if (process.platform === "win32")
119 self.root = self.root.replace(/\\/g, "/")
121 self.nomount = !!options.nomount
123 self.minimatch = new Minimatch(pattern, options)
124 self.options = self.minimatch.options
127 function finish (self) {
128 var nou = self.nounique
129 var all = nou ? [] : Object.create(null)
131 for (var i = 0, l = self.matches.length; i < l; i ++) {
132 var matches = self.matches[i]
133 if (!matches || Object.keys(matches).length === 0) {
135 // do like the shell, and spit out the literal glob
136 var literal = self.minimatch.globSet[i]
144 var m = Object.keys(matches)
146 all.push.apply(all, m)
148 m.forEach(function (m) {
155 all = Object.keys(all)
158 all = all.sort(self.nocase ? alphasorti : alphasort)
160 // at *some* point we statted all of these
162 for (var i = 0; i < all.length; i++) {
163 all[i] = self._mark(all[i])
166 all = all.filter(function (e) {
167 return !(/\/$/.test(e))
172 if (self.ignore.length)
173 all = all.filter(function(m) {
174 return !isIgnored(self, m)
180 function mark (self, p) {
181 var abs = makeAbs(self, p)
182 var c = self.cache[abs]
185 var isDir = c === 'DIR' || Array.isArray(c)
186 var slash = p.slice(-1) === '/'
190 else if (!isDir && slash)
194 var mabs = makeAbs(self, m)
195 self.statCache[mabs] = self.statCache[abs]
196 self.cache[mabs] = self.cache[abs]
204 function makeAbs (self, f) {
206 if (f.charAt(0) === '/') {
207 abs = path.join(self.root, f)
208 } else if (exports.isAbsolute(f)) {
210 } else if (self.changedCwd) {
211 abs = path.resolve(self.cwd, f)
212 } else if (self.realpath) {
213 abs = path.resolve(f)
219 // Return true, if pattern ends with globstar '**', for the accompanying parent directory.
220 // Ex:- If node_modules/** is the pattern, add 'node_modules' to ignore list along with it's contents
221 function isIgnored (self, path) {
222 if (!self.ignore.length)
225 return self.ignore.some(function(item) {
226 return item.matcher.match(path) || !!(item.gmatcher && item.gmatcher.match(path))
230 function childrenIgnored (self, path) {
231 if (!self.ignore.length)
234 return self.ignore.some(function(item) {
235 return !!(item.gmatcher && item.gmatcher.match(path))