Advertisement
Guest User

Untitled

a guest
Jul 29th, 2015
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.15 KB | None | 0 0
  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 WinPath (p) {
  21. if (!(this instanceof WinPath))
  22. return new WinPath(p)
  23. // pull off the device/UNC bit from a windows path.
  24. // from node's lib/path.js
  25. var splitDeviceRe =
  26. /^([a-zA-Z]:|[\\\/]{2}[^\\\/]+[\\\/]+[^\\\/]+)?([\\\/])?([\s\S]*?)$/
  27. var result = splitDeviceRe.exec(p)
  28. this.device = result[1] || ''
  29. this.sep = result[2] || ''
  30. this.tail = result[3] || ''
  31. this.isUnc = !!this.device && this.device.charAt(1) !== ':'
  32. this.isAbsolute = !!this.sep || this.isUnc // UNC paths are always absolute
  33. }
  34.  
  35. function absWin (p) {
  36. if (absUnix(p)) return true
  37. var winPath = new WinPath(p)
  38. return winPath.isAbsolute
  39. }
  40.  
  41. function absUnix (p) {
  42. return p.charAt(0) === "/" || p === ""
  43. }
  44.  
  45. function alphasorti (a, b) {
  46. return a.toLowerCase().localeCompare(b.toLowerCase())
  47. }
  48.  
  49. function alphasort (a, b) {
  50. return a.localeCompare(b)
  51. }
  52.  
  53. function setupIgnores (self, options) {
  54. self.ignore = options.ignore || []
  55.  
  56. if (!Array.isArray(self.ignore))
  57. self.ignore = [self.ignore]
  58.  
  59. if (self.ignore.length) {
  60. self.ignore = self.ignore.map(ignoreMap)
  61. }
  62. }
  63.  
  64. function ignoreMap (pattern) {
  65. var gmatcher = null
  66. if (pattern.slice(-3) === '/**') {
  67. var gpattern = pattern.replace(/(\/\*\*)+$/, '')
  68. gmatcher = new Minimatch(gpattern, { nonegate: true })
  69. }
  70.  
  71. return {
  72. matcher: new Minimatch(pattern, { nonegate: true }),
  73. gmatcher: gmatcher
  74. }
  75. }
  76.  
  77. function setopts (self, pattern, options) {
  78. if (!options)
  79. options = {}
  80.  
  81. // base-matching: just use globstar for that.
  82. if (options.matchBase && -1 === pattern.indexOf("/")) {
  83. if (options.noglobstar) {
  84. throw new Error("base matching requires globstar")
  85. }
  86. pattern = "**/" + pattern
  87. }
  88.  
  89. self.pattern = pattern
  90. self.strict = options.strict !== false
  91. self.realpath = !!options.realpath
  92. self.realpathCache = options.realpathCache || Object.create(null)
  93. self.follow = !!options.follow
  94. self.dot = !!options.dot
  95. self.mark = !!options.mark
  96. self.nodir = !!options.nodir
  97. if (self.nodir)
  98. self.mark = true
  99. self.sync = !!options.sync
  100. self.nounique = !!options.nounique
  101. self.nonull = !!options.nonull
  102. self.nosort = !!options.nosort
  103. self.nocase = !!options.nocase
  104. self.stat = !!options.stat
  105. self.noprocess = !!options.noprocess
  106.  
  107. self.maxLength = options.maxLength || Infinity
  108. self.cache = options.cache || Object.create(null)
  109. self.statCache = options.statCache || Object.create(null)
  110. self.symlinks = options.symlinks || Object.create(null)
  111.  
  112. setupIgnores(self, options)
  113.  
  114. self.changedCwd = false
  115. var cwd = process.cwd()
  116. if (!ownProp(options, "cwd"))
  117. self.cwd = cwd
  118. else {
  119. self.cwd = options.cwd
  120. self.changedCwd = path.resolve(options.cwd) !== cwd
  121. }
  122. if (process.platform === "win32") {
  123. var winPath = new WinPath(pattern)
  124. if (winPath.isAbsolute) {
  125. options.root = winPath.device
  126. pattern = winPath.sep + winPath.tail
  127. }
  128. }
  129.  
  130. self.root = options.root || path.resolve(self.cwd, "/")
  131. self.root = path.resolve(self.root)
  132. if (process.platform === "win32")
  133. self.root = self.root.replace(/\\/g, "/")
  134.  
  135. self.nomount = !!options.nomount
  136.  
  137. self.minimatch = new Minimatch(pattern, options)
  138. self.options = self.minimatch.options
  139. }
  140.  
  141. function finish (self) {
  142. var nou = self.nounique
  143. var all = nou ? [] : Object.create(null)
  144.  
  145. for (var i = 0, l = self.matches.length; i < l; i ++) {
  146. var matches = self.matches[i]
  147. if (!matches || Object.keys(matches).length === 0) {
  148. if (self.nonull) {
  149. // do like the shell, and spit out the literal glob
  150. var literal = self.minimatch.globSet[i]
  151. if (nou)
  152. all.push(literal)
  153. else
  154. all[literal] = true
  155. }
  156. } else {
  157. // had matches
  158. var m = Object.keys(matches)
  159. if (nou)
  160. all.push.apply(all, m)
  161. else
  162. m.forEach(function (m) {
  163. all[m] = true
  164. })
  165. }
  166. }
  167.  
  168. if (!nou)
  169. all = Object.keys(all)
  170.  
  171. if (!self.nosort)
  172. all = all.sort(self.nocase ? alphasorti : alphasort)
  173.  
  174. // at *some* point we statted all of these
  175. if (self.mark) {
  176. for (var i = 0; i < all.length; i++) {
  177. all[i] = self._mark(all[i])
  178. }
  179. if (self.nodir) {
  180. all = all.filter(function (e) {
  181. return !(/\/$/.test(e))
  182. })
  183. }
  184. }
  185.  
  186. if (self.ignore.length)
  187. all = all.filter(function(m) {
  188. return !isIgnored(self, m)
  189. })
  190.  
  191. self.found = all
  192. }
  193.  
  194. function mark (self, p) {
  195. var abs = makeAbs(self, p)
  196. var c = self.cache[abs]
  197. var m = p
  198. if (c) {
  199. var isDir = c === 'DIR' || Array.isArray(c)
  200. var slash = p.slice(-1) === '/'
  201.  
  202. if (isDir && !slash)
  203. m += '/'
  204. else if (!isDir && slash)
  205. m = m.slice(0, -1)
  206.  
  207. if (m !== p) {
  208. var mabs = makeAbs(self, m)
  209. self.statCache[mabs] = self.statCache[abs]
  210. self.cache[mabs] = self.cache[abs]
  211. }
  212. }
  213.  
  214. return m
  215. }
  216.  
  217. // lotta situps...
  218. function makeAbs (self, f) {
  219. var abs = f
  220. if (f.charAt(0) === '/') {
  221. abs = path.join(self.root, f)
  222. } else if (exports.isAbsolute(f)) {
  223. abs = f
  224. } else if (self.changedCwd) {
  225. abs = path.resolve(self.cwd, f)
  226. } else if (self.realpath) {
  227. abs = path.resolve(f)
  228. }
  229. return abs
  230. }
  231.  
  232.  
  233. // Return true, if pattern ends with globstar '**', for the accompanying parent directory.
  234. // Ex:- If node_modules/** is the pattern, add 'node_modules' to ignore list along with it's contents
  235. function isIgnored (self, path) {
  236. if (!self.ignore.length)
  237. return false
  238.  
  239. return self.ignore.some(function(item) {
  240. return item.matcher.match(path) || !!(item.gmatcher && item.gmatcher.match(path))
  241. })
  242. }
  243.  
  244. function childrenIgnored (self, path) {
  245. if (!self.ignore.length)
  246. return false
  247.  
  248. return self.ignore.some(function(item) {
  249. return !!(item.gmatcher && item.gmatcher.match(path))
  250. })
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement