common.js 6.0 KB

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