Initial commit
[yaffs-website] / node_modules / glob-stream / node_modules / glob / common.js
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
8 exports.mark = mark
9 exports.isIgnored = isIgnored
10 exports.childrenIgnored = childrenIgnored
11
12 function ownProp (obj, field) {
13   return Object.prototype.hasOwnProperty.call(obj, field)
14 }
15
16 var path = require("path")
17 var minimatch = require("minimatch")
18 var Minimatch = minimatch.Minimatch
19
20 function absWin (p) {
21   if (absUnix(p)) return true
22   // pull off the device/UNC bit from a windows path.
23   // from node's lib/path.js
24   var splitDeviceRe =
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
30
31   return isAbsolute
32 }
33
34 function absUnix (p) {
35   return p.charAt(0) === "/" || p === ""
36 }
37
38 function alphasorti (a, b) {
39   return a.toLowerCase().localeCompare(b.toLowerCase())
40 }
41
42 function alphasort (a, b) {
43   return a.localeCompare(b)
44 }
45
46 function setupIgnores (self, options) {
47   self.ignore = options.ignore || []
48
49   if (!Array.isArray(self.ignore))
50     self.ignore = [self.ignore]
51
52   if (self.ignore.length) {
53     self.ignore = self.ignore.map(ignoreMap)
54   }
55 }
56
57 function ignoreMap (pattern) {
58   var gmatcher = null
59   if (pattern.slice(-3) === '/**') {
60     var gpattern = pattern.replace(/(\/\*\*)+$/, '')
61     gmatcher = new Minimatch(gpattern, { nonegate: true })
62   }
63
64   return {
65     matcher: new Minimatch(pattern, { nonegate: true }),
66     gmatcher: gmatcher
67   }
68 }
69
70 function setopts (self, pattern, options) {
71   if (!options)
72     options = {}
73
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")
78     }
79     pattern = "**/" + pattern
80   }
81
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
90   if (self.nodir)
91     self.mark = true
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
99
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)
104
105   setupIgnores(self, options)
106
107   self.changedCwd = false
108   var cwd = process.cwd()
109   if (!ownProp(options, "cwd"))
110     self.cwd = cwd
111   else {
112     self.cwd = options.cwd
113     self.changedCwd = path.resolve(options.cwd) !== cwd
114   }
115
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, "/")
120
121   self.nomount = !!options.nomount
122
123   self.minimatch = new Minimatch(pattern, options)
124   self.options = self.minimatch.options
125 }
126
127 function finish (self) {
128   var nou = self.nounique
129   var all = nou ? [] : Object.create(null)
130
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) {
134       if (self.nonull) {
135         // do like the shell, and spit out the literal glob
136         var literal = self.minimatch.globSet[i]
137         if (nou)
138           all.push(literal)
139         else
140           all[literal] = true
141       }
142     } else {
143       // had matches
144       var m = Object.keys(matches)
145       if (nou)
146         all.push.apply(all, m)
147       else
148         m.forEach(function (m) {
149           all[m] = true
150         })
151     }
152   }
153
154   if (!nou)
155     all = Object.keys(all)
156
157   if (!self.nosort)
158     all = all.sort(self.nocase ? alphasorti : alphasort)
159
160   // at *some* point we statted all of these
161   if (self.mark) {
162     for (var i = 0; i < all.length; i++) {
163       all[i] = self._mark(all[i])
164     }
165     if (self.nodir) {
166       all = all.filter(function (e) {
167         return !(/\/$/.test(e))
168       })
169     }
170   }
171
172   if (self.ignore.length)
173     all = all.filter(function(m) {
174       return !isIgnored(self, m)
175     })
176
177   self.found = all
178 }
179
180 function mark (self, p) {
181   var abs = makeAbs(self, p)
182   var c = self.cache[abs]
183   var m = p
184   if (c) {
185     var isDir = c === 'DIR' || Array.isArray(c)
186     var slash = p.slice(-1) === '/'
187
188     if (isDir && !slash)
189       m += '/'
190     else if (!isDir && slash)
191       m = m.slice(0, -1)
192
193     if (m !== p) {
194       var mabs = makeAbs(self, m)
195       self.statCache[mabs] = self.statCache[abs]
196       self.cache[mabs] = self.cache[abs]
197     }
198   }
199
200   return m
201 }
202
203 // lotta situps...
204 function makeAbs (self, f) {
205   var abs = f
206   if (f.charAt(0) === '/') {
207     abs = path.join(self.root, f)
208   } else if (exports.isAbsolute(f)) {
209     abs = f
210   } else if (self.changedCwd) {
211     abs = path.resolve(self.cwd, f)
212   } else if (self.realpath) {
213     abs = path.resolve(f)
214   }
215   return abs
216 }
217
218
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)
223     return false
224
225   return self.ignore.some(function(item) {
226     return item.matcher.match(path) || !!(item.gmatcher && item.gmatcher.match(path))
227   })
228 }
229
230 function childrenIgnored (self, path) {
231   if (!self.ignore.length)
232     return false
233
234   return self.ignore.some(function(item) {
235     return !!(item.gmatcher && item.gmatcher.match(path))
236   })
237 }