1 exports.setopts = setopts
2 exports.ownProp = ownProp
3 exports.makeAbs = makeAbs
4 exports.finish = finish
6 exports.isIgnored = isIgnored
7 exports.childrenIgnored = childrenIgnored
9 function ownProp (obj, field) {
10 return Object.prototype.hasOwnProperty.call(obj, field)
13 var fs = require("fs")
14 var path = require("path")
15 var minimatch = require("minimatch")
16 var isAbsolute = require("path-is-absolute")
17 var Minimatch = minimatch.Minimatch
19 function alphasort (a, b) {
20 return a.localeCompare(b, 'en')
23 function setupIgnores (self, options) {
24 self.ignore = options.ignore || []
26 if (!Array.isArray(self.ignore))
27 self.ignore = [self.ignore]
29 if (self.ignore.length) {
30 self.ignore = self.ignore.map(ignoreMap)
34 // ignore patterns are always in dot:true mode.
35 function ignoreMap (pattern) {
37 if (pattern.slice(-3) === '/**') {
38 var gpattern = pattern.replace(/(\/\*\*)+$/, '')
39 gmatcher = new Minimatch(gpattern, { dot: true })
43 matcher: new Minimatch(pattern, { dot: true }),
48 function setopts (self, pattern, options) {
52 // base-matching: just use globstar for that.
53 if (options.matchBase && -1 === pattern.indexOf("/")) {
54 if (options.noglobstar) {
55 throw new Error("base matching requires globstar")
57 pattern = "**/" + pattern
60 self.silent = !!options.silent
61 self.pattern = pattern
62 self.strict = options.strict !== false
63 self.realpath = !!options.realpath
64 self.realpathCache = options.realpathCache || Object.create(null)
65 self.follow = !!options.follow
66 self.dot = !!options.dot
67 self.mark = !!options.mark
68 self.nodir = !!options.nodir
71 self.sync = !!options.sync
72 self.nounique = !!options.nounique
73 self.nonull = !!options.nonull
74 self.nosort = !!options.nosort
75 self.nocase = !!options.nocase
76 self.stat = !!options.stat
77 self.noprocess = !!options.noprocess
78 self.absolute = !!options.absolute
79 self.fs = options.fs || fs
81 self.maxLength = options.maxLength || Infinity
82 self.cache = options.cache || Object.create(null)
83 self.statCache = options.statCache || Object.create(null)
84 self.symlinks = options.symlinks || Object.create(null)
86 setupIgnores(self, options)
88 self.changedCwd = false
89 var cwd = process.cwd()
90 if (!ownProp(options, "cwd"))
93 self.cwd = path.resolve(options.cwd)
94 self.changedCwd = self.cwd !== cwd
97 self.root = options.root || path.resolve(self.cwd, "/")
98 self.root = path.resolve(self.root)
99 if (process.platform === "win32")
100 self.root = self.root.replace(/\\/g, "/")
102 // TODO: is an absolute `cwd` supposed to be resolved against `root`?
103 // e.g. { cwd: '/test', root: __dirname } === path.join(__dirname, '/test')
104 self.cwdAbs = isAbsolute(self.cwd) ? self.cwd : makeAbs(self, self.cwd)
105 if (process.platform === "win32")
106 self.cwdAbs = self.cwdAbs.replace(/\\/g, "/")
107 self.nomount = !!options.nomount
109 // disable comments and negation in Minimatch.
110 // Note that they are not supported in Glob itself anyway.
111 options.nonegate = true
112 options.nocomment = true
114 self.minimatch = new Minimatch(pattern, options)
115 self.options = self.minimatch.options
118 function finish (self) {
119 var nou = self.nounique
120 var all = nou ? [] : Object.create(null)
122 for (var i = 0, l = self.matches.length; i < l; i ++) {
123 var matches = self.matches[i]
124 if (!matches || Object.keys(matches).length === 0) {
126 // do like the shell, and spit out the literal glob
127 var literal = self.minimatch.globSet[i]
135 var m = Object.keys(matches)
137 all.push.apply(all, m)
139 m.forEach(function (m) {
146 all = Object.keys(all)
149 all = all.sort(alphasort)
151 // at *some* point we statted all of these
153 for (var i = 0; i < all.length; i++) {
154 all[i] = self._mark(all[i])
157 all = all.filter(function (e) {
158 var notDir = !(/\/$/.test(e))
159 var c = self.cache[e] || self.cache[makeAbs(self, e)]
161 notDir = c !== 'DIR' && !Array.isArray(c)
167 if (self.ignore.length)
168 all = all.filter(function(m) {
169 return !isIgnored(self, m)
175 function mark (self, p) {
176 var abs = makeAbs(self, p)
177 var c = self.cache[abs]
180 var isDir = c === 'DIR' || Array.isArray(c)
181 var slash = p.slice(-1) === '/'
185 else if (!isDir && slash)
189 var mabs = makeAbs(self, m)
190 self.statCache[mabs] = self.statCache[abs]
191 self.cache[mabs] = self.cache[abs]
199 function makeAbs (self, f) {
201 if (f.charAt(0) === '/') {
202 abs = path.join(self.root, f)
203 } else if (isAbsolute(f) || f === '') {
205 } else if (self.changedCwd) {
206 abs = path.resolve(self.cwd, f)
208 abs = path.resolve(f)
211 if (process.platform === 'win32')
212 abs = abs.replace(/\\/g, '/')
218 // Return true, if pattern ends with globstar '**', for the accompanying parent directory.
219 // Ex:- If node_modules/** is the pattern, add 'node_modules' to ignore list along with it's contents
220 function isIgnored (self, path) {
221 if (!self.ignore.length)
224 return self.ignore.some(function(item) {
225 return item.matcher.match(path) || !!(item.gmatcher && item.gmatcher.match(path))
229 function childrenIgnored (self, path) {
230 if (!self.ignore.length)
233 return self.ignore.some(function(item) {
234 return !!(item.gmatcher && item.gmatcher.match(path))