common.js 5.9 KB

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