Initial commit
[yaffs-website] / node_modules / glob / common.js
1 exports.alphasort = alphasort
2 exports.alphasorti = alphasorti
3 exports.setopts = setopts
4 exports.ownProp = ownProp
5 exports.makeAbs = makeAbs
6 exports.finish = finish
7 exports.mark = mark
8 exports.isIgnored = isIgnored
9 exports.childrenIgnored = childrenIgnored
10
11 function ownProp (obj, field) {
12   return Object.prototype.hasOwnProperty.call(obj, field)
13 }
14
15 var path = require("path")
16 var minimatch = require("minimatch")
17 var isAbsolute = require("path-is-absolute")
18 var Minimatch = minimatch.Minimatch
19
20 function alphasorti (a, b) {
21   return a.toLowerCase().localeCompare(b.toLowerCase())
22 }
23
24 function alphasort (a, b) {
25   return a.localeCompare(b)
26 }
27
28 function setupIgnores (self, options) {
29   self.ignore = options.ignore || []
30
31   if (!Array.isArray(self.ignore))
32     self.ignore = [self.ignore]
33
34   if (self.ignore.length) {
35     self.ignore = self.ignore.map(ignoreMap)
36   }
37 }
38
39 // ignore patterns are always in dot:true mode.
40 function ignoreMap (pattern) {
41   var gmatcher = null
42   if (pattern.slice(-3) === '/**') {
43     var gpattern = pattern.replace(/(\/\*\*)+$/, '')
44     gmatcher = new Minimatch(gpattern, { dot: true })
45   }
46
47   return {
48     matcher: new Minimatch(pattern, { dot: true }),
49     gmatcher: gmatcher
50   }
51 }
52
53 function setopts (self, pattern, options) {
54   if (!options)
55     options = {}
56
57   // base-matching: just use globstar for that.
58   if (options.matchBase && -1 === pattern.indexOf("/")) {
59     if (options.noglobstar) {
60       throw new Error("base matching requires globstar")
61     }
62     pattern = "**/" + pattern
63   }
64
65   self.silent = !!options.silent
66   self.pattern = pattern
67   self.strict = options.strict !== false
68   self.realpath = !!options.realpath
69   self.realpathCache = options.realpathCache || Object.create(null)
70   self.follow = !!options.follow
71   self.dot = !!options.dot
72   self.mark = !!options.mark
73   self.nodir = !!options.nodir
74   if (self.nodir)
75     self.mark = true
76   self.sync = !!options.sync
77   self.nounique = !!options.nounique
78   self.nonull = !!options.nonull
79   self.nosort = !!options.nosort
80   self.nocase = !!options.nocase
81   self.stat = !!options.stat
82   self.noprocess = !!options.noprocess
83
84   self.maxLength = options.maxLength || Infinity
85   self.cache = options.cache || Object.create(null)
86   self.statCache = options.statCache || Object.create(null)
87   self.symlinks = options.symlinks || Object.create(null)
88
89   setupIgnores(self, options)
90
91   self.changedCwd = false
92   var cwd = process.cwd()
93   if (!ownProp(options, "cwd"))
94     self.cwd = cwd
95   else {
96     self.cwd = path.resolve(options.cwd)
97     self.changedCwd = self.cwd !== cwd
98   }
99
100   self.root = options.root || path.resolve(self.cwd, "/")
101   self.root = path.resolve(self.root)
102   if (process.platform === "win32")
103     self.root = self.root.replace(/\\/g, "/")
104
105   self.cwdAbs = makeAbs(self, self.cwd)
106   self.nomount = !!options.nomount
107
108   // disable comments and negation in Minimatch.
109   // Note that they are not supported in Glob itself anyway.
110   options.nonegate = true
111   options.nocomment = true
112
113   self.minimatch = new Minimatch(pattern, options)
114   self.options = self.minimatch.options
115 }
116
117 function finish (self) {
118   var nou = self.nounique
119   var all = nou ? [] : Object.create(null)
120
121   for (var i = 0, l = self.matches.length; i < l; i ++) {
122     var matches = self.matches[i]
123     if (!matches || Object.keys(matches).length === 0) {
124       if (self.nonull) {
125         // do like the shell, and spit out the literal glob
126         var literal = self.minimatch.globSet[i]
127         if (nou)
128           all.push(literal)
129         else
130           all[literal] = true
131       }
132     } else {
133       // had matches
134       var m = Object.keys(matches)
135       if (nou)
136         all.push.apply(all, m)
137       else
138         m.forEach(function (m) {
139           all[m] = true
140         })
141     }
142   }
143
144   if (!nou)
145     all = Object.keys(all)
146
147   if (!self.nosort)
148     all = all.sort(self.nocase ? alphasorti : alphasort)
149
150   // at *some* point we statted all of these
151   if (self.mark) {
152     for (var i = 0; i < all.length; i++) {
153       all[i] = self._mark(all[i])
154     }
155     if (self.nodir) {
156       all = all.filter(function (e) {
157         var notDir = !(/\/$/.test(e))
158         var c = self.cache[e] || self.cache[makeAbs(self, e)]
159         if (notDir && c)
160           notDir = c !== 'DIR' && !Array.isArray(c)
161         return notDir
162       })
163     }
164   }
165
166   if (self.ignore.length)
167     all = all.filter(function(m) {
168       return !isIgnored(self, m)
169     })
170
171   self.found = all
172 }
173
174 function mark (self, p) {
175   var abs = makeAbs(self, p)
176   var c = self.cache[abs]
177   var m = p
178   if (c) {
179     var isDir = c === 'DIR' || Array.isArray(c)
180     var slash = p.slice(-1) === '/'
181
182     if (isDir && !slash)
183       m += '/'
184     else if (!isDir && slash)
185       m = m.slice(0, -1)
186
187     if (m !== p) {
188       var mabs = makeAbs(self, m)
189       self.statCache[mabs] = self.statCache[abs]
190       self.cache[mabs] = self.cache[abs]
191     }
192   }
193
194   return m
195 }
196
197 // lotta situps...
198 function makeAbs (self, f) {
199   var abs = f
200   if (f.charAt(0) === '/') {
201     abs = path.join(self.root, f)
202   } else if (isAbsolute(f) || f === '') {
203     abs = f
204   } else if (self.changedCwd) {
205     abs = path.resolve(self.cwd, f)
206   } else {
207     abs = path.resolve(f)
208   }
209
210   if (process.platform === 'win32')
211     abs = abs.replace(/\\/g, '/')
212
213   return abs
214 }
215
216
217 // Return true, if pattern ends with globstar '**', for the accompanying parent directory.
218 // Ex:- If node_modules/** is the pattern, add 'node_modules' to ignore list along with it's contents
219 function isIgnored (self, path) {
220   if (!self.ignore.length)
221     return false
222
223   return self.ignore.some(function(item) {
224     return item.matcher.match(path) || !!(item.gmatcher && item.gmatcher.match(path))
225   })
226 }
227
228 function childrenIgnored (self, path) {
229   if (!self.ignore.length)
230     return false
231
232   return self.ignore.some(function(item) {
233     return !!(item.gmatcher && item.gmatcher.match(path))
234   })
235 }