[d565449] | 1 | exports.setopts = setopts
|
---|
| 2 | exports.ownProp = ownProp
|
---|
| 3 | exports.makeAbs = makeAbs
|
---|
| 4 | exports.finish = finish
|
---|
| 5 | exports.mark = mark
|
---|
| 6 | exports.isIgnored = isIgnored
|
---|
| 7 | exports.childrenIgnored = childrenIgnored
|
---|
| 8 |
|
---|
| 9 | function ownProp (obj, field) {
|
---|
| 10 | return Object.prototype.hasOwnProperty.call(obj, field)
|
---|
| 11 | }
|
---|
| 12 |
|
---|
| 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
|
---|
| 18 |
|
---|
| 19 | function alphasort (a, b) {
|
---|
| 20 | return a.localeCompare(b, 'en')
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | function setupIgnores (self, options) {
|
---|
| 24 | self.ignore = options.ignore || []
|
---|
| 25 |
|
---|
| 26 | if (!Array.isArray(self.ignore))
|
---|
| 27 | self.ignore = [self.ignore]
|
---|
| 28 |
|
---|
| 29 | if (self.ignore.length) {
|
---|
| 30 | self.ignore = self.ignore.map(ignoreMap)
|
---|
| 31 | }
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | // ignore patterns are always in dot:true mode.
|
---|
| 35 | function ignoreMap (pattern) {
|
---|
| 36 | var gmatcher = null
|
---|
| 37 | if (pattern.slice(-3) === '/**') {
|
---|
| 38 | var gpattern = pattern.replace(/(\/\*\*)+$/, '')
|
---|
| 39 | gmatcher = new Minimatch(gpattern, { dot: true })
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | return {
|
---|
| 43 | matcher: new Minimatch(pattern, { dot: true }),
|
---|
| 44 | gmatcher: gmatcher
|
---|
| 45 | }
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | function setopts (self, pattern, options) {
|
---|
| 49 | if (!options)
|
---|
| 50 | options = {}
|
---|
| 51 |
|
---|
| 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")
|
---|
| 56 | }
|
---|
| 57 | pattern = "**/" + pattern
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 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
|
---|
| 69 | if (self.nodir)
|
---|
| 70 | self.mark = true
|
---|
| 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
|
---|
| 80 |
|
---|
| 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)
|
---|
| 85 |
|
---|
| 86 | setupIgnores(self, options)
|
---|
| 87 |
|
---|
| 88 | self.changedCwd = false
|
---|
| 89 | var cwd = process.cwd()
|
---|
| 90 | if (!ownProp(options, "cwd"))
|
---|
| 91 | self.cwd = cwd
|
---|
| 92 | else {
|
---|
| 93 | self.cwd = path.resolve(options.cwd)
|
---|
| 94 | self.changedCwd = self.cwd !== cwd
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 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, "/")
|
---|
| 101 |
|
---|
| 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
|
---|
| 108 |
|
---|
| 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
|
---|
| 113 | // always treat \ in patterns as escapes, not path separators
|
---|
| 114 | options.allowWindowsEscape = false
|
---|
| 115 |
|
---|
| 116 | self.minimatch = new Minimatch(pattern, options)
|
---|
| 117 | self.options = self.minimatch.options
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | function finish (self) {
|
---|
| 121 | var nou = self.nounique
|
---|
| 122 | var all = nou ? [] : Object.create(null)
|
---|
| 123 |
|
---|
| 124 | for (var i = 0, l = self.matches.length; i < l; i ++) {
|
---|
| 125 | var matches = self.matches[i]
|
---|
| 126 | if (!matches || Object.keys(matches).length === 0) {
|
---|
| 127 | if (self.nonull) {
|
---|
| 128 | // do like the shell, and spit out the literal glob
|
---|
| 129 | var literal = self.minimatch.globSet[i]
|
---|
| 130 | if (nou)
|
---|
| 131 | all.push(literal)
|
---|
| 132 | else
|
---|
| 133 | all[literal] = true
|
---|
| 134 | }
|
---|
| 135 | } else {
|
---|
| 136 | // had matches
|
---|
| 137 | var m = Object.keys(matches)
|
---|
| 138 | if (nou)
|
---|
| 139 | all.push.apply(all, m)
|
---|
| 140 | else
|
---|
| 141 | m.forEach(function (m) {
|
---|
| 142 | all[m] = true
|
---|
| 143 | })
|
---|
| 144 | }
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | if (!nou)
|
---|
| 148 | all = Object.keys(all)
|
---|
| 149 |
|
---|
| 150 | if (!self.nosort)
|
---|
| 151 | all = all.sort(alphasort)
|
---|
| 152 |
|
---|
| 153 | // at *some* point we statted all of these
|
---|
| 154 | if (self.mark) {
|
---|
| 155 | for (var i = 0; i < all.length; i++) {
|
---|
| 156 | all[i] = self._mark(all[i])
|
---|
| 157 | }
|
---|
| 158 | if (self.nodir) {
|
---|
| 159 | all = all.filter(function (e) {
|
---|
| 160 | var notDir = !(/\/$/.test(e))
|
---|
| 161 | var c = self.cache[e] || self.cache[makeAbs(self, e)]
|
---|
| 162 | if (notDir && c)
|
---|
| 163 | notDir = c !== 'DIR' && !Array.isArray(c)
|
---|
| 164 | return notDir
|
---|
| 165 | })
|
---|
| 166 | }
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | if (self.ignore.length)
|
---|
| 170 | all = all.filter(function(m) {
|
---|
| 171 | return !isIgnored(self, m)
|
---|
| 172 | })
|
---|
| 173 |
|
---|
| 174 | self.found = all
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | function mark (self, p) {
|
---|
| 178 | var abs = makeAbs(self, p)
|
---|
| 179 | var c = self.cache[abs]
|
---|
| 180 | var m = p
|
---|
| 181 | if (c) {
|
---|
| 182 | var isDir = c === 'DIR' || Array.isArray(c)
|
---|
| 183 | var slash = p.slice(-1) === '/'
|
---|
| 184 |
|
---|
| 185 | if (isDir && !slash)
|
---|
| 186 | m += '/'
|
---|
| 187 | else if (!isDir && slash)
|
---|
| 188 | m = m.slice(0, -1)
|
---|
| 189 |
|
---|
| 190 | if (m !== p) {
|
---|
| 191 | var mabs = makeAbs(self, m)
|
---|
| 192 | self.statCache[mabs] = self.statCache[abs]
|
---|
| 193 | self.cache[mabs] = self.cache[abs]
|
---|
| 194 | }
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | return m
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | // lotta situps...
|
---|
| 201 | function makeAbs (self, f) {
|
---|
| 202 | var abs = f
|
---|
| 203 | if (f.charAt(0) === '/') {
|
---|
| 204 | abs = path.join(self.root, f)
|
---|
| 205 | } else if (isAbsolute(f) || f === '') {
|
---|
| 206 | abs = f
|
---|
| 207 | } else if (self.changedCwd) {
|
---|
| 208 | abs = path.resolve(self.cwd, f)
|
---|
| 209 | } else {
|
---|
| 210 | abs = path.resolve(f)
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | if (process.platform === 'win32')
|
---|
| 214 | abs = abs.replace(/\\/g, '/')
|
---|
| 215 |
|
---|
| 216 | return abs
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 |
|
---|
| 220 | // Return true, if pattern ends with globstar '**', for the accompanying parent directory.
|
---|
| 221 | // Ex:- If node_modules/** is the pattern, add 'node_modules' to ignore list along with it's contents
|
---|
| 222 | function isIgnored (self, path) {
|
---|
| 223 | if (!self.ignore.length)
|
---|
| 224 | return false
|
---|
| 225 |
|
---|
| 226 | return self.ignore.some(function(item) {
|
---|
| 227 | return item.matcher.match(path) || !!(item.gmatcher && item.gmatcher.match(path))
|
---|
| 228 | })
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | function childrenIgnored (self, path) {
|
---|
| 232 | if (!self.ignore.length)
|
---|
| 233 | return false
|
---|
| 234 |
|
---|
| 235 | return self.ignore.some(function(item) {
|
---|
| 236 | return !!(item.gmatcher && item.gmatcher.match(path))
|
---|
| 237 | })
|
---|
| 238 | }
|
---|