common.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. function ownProp (obj, field) {
  11. return Object.prototype.hasOwnProperty.call(obj, field)
  12. }
  13. var path = require("path")
  14. var minimatch = require("minimatch")
  15. var isAbsolute = require("path-is-absolute")
  16. var Minimatch = minimatch.Minimatch
  17. function alphasorti (a, b) {
  18. return a.toLowerCase().localeCompare(b.toLowerCase())
  19. }
  20. function alphasort (a, b) {
  21. return a.localeCompare(b)
  22. }
  23. function setupIgnores (self, options) {
  24. self.ignore = options.ignore || []
  25. if (!Array.isArray(self.ignore))
  26. self.ignore = [self.ignore]
  27. if (self.ignore.length) {
  28. self.ignore = self.ignore.map(ignoreMap)
  29. }
  30. }
  31. // ignore patterns are always in dot:true mode.
  32. function ignoreMap (pattern) {
  33. var gmatcher = null
  34. if (pattern.slice(-3) === '/**') {
  35. var gpattern = pattern.replace(/(\/\*\*)+$/, '')
  36. gmatcher = new Minimatch(gpattern, { dot: true })
  37. }
  38. return {
  39. matcher: new Minimatch(pattern, { dot: true }),
  40. gmatcher: gmatcher
  41. }
  42. }
  43. function setopts (self, pattern, options) {
  44. if (!options)
  45. options = {}
  46. // base-matching: just use globstar for that.
  47. if (options.matchBase && -1 === pattern.indexOf("/")) {
  48. if (options.noglobstar) {
  49. throw new Error("base matching requires globstar")
  50. }
  51. pattern = "**/" + pattern
  52. }
  53. self.silent = !!options.silent
  54. self.pattern = pattern
  55. self.strict = options.strict !== false
  56. self.realpath = !!options.realpath
  57. self.realpathCache = options.realpathCache || Object.create(null)
  58. self.follow = !!options.follow
  59. self.dot = !!options.dot
  60. self.mark = !!options.mark
  61. self.nodir = !!options.nodir
  62. if (self.nodir)
  63. self.mark = true
  64. self.sync = !!options.sync
  65. self.nounique = !!options.nounique
  66. self.nonull = !!options.nonull
  67. self.nosort = !!options.nosort
  68. self.nocase = !!options.nocase
  69. self.stat = !!options.stat
  70. self.noprocess = !!options.noprocess
  71. self.maxLength = options.maxLength || Infinity
  72. self.cache = options.cache || Object.create(null)
  73. self.statCache = options.statCache || Object.create(null)
  74. self.symlinks = options.symlinks || Object.create(null)
  75. setupIgnores(self, options)
  76. self.changedCwd = false
  77. var cwd = process.cwd()
  78. if (!ownProp(options, "cwd"))
  79. self.cwd = cwd
  80. else {
  81. self.cwd = path.resolve(options.cwd)
  82. self.changedCwd = self.cwd !== cwd
  83. }
  84. self.root = options.root || path.resolve(self.cwd, "/")
  85. self.root = path.resolve(self.root)
  86. if (process.platform === "win32")
  87. self.root = self.root.replace(/\\/g, "/")
  88. self.cwdAbs = makeAbs(self, self.cwd)
  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(self.nocase ? alphasorti : 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. }