Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

common.js 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 = options.cwd
  82. self.changedCwd = path.resolve(options.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.nomount = !!options.nomount
  89. // disable comments and negation in Minimatch.
  90. // Note that they are not supported in Glob itself anyway.
  91. options.nonegate = true
  92. options.nocomment = true
  93. self.minimatch = new Minimatch(pattern, options)
  94. self.options = self.minimatch.options
  95. }
  96. function finish (self) {
  97. var nou = self.nounique
  98. var all = nou ? [] : Object.create(null)
  99. for (var i = 0, l = self.matches.length; i < l; i ++) {
  100. var matches = self.matches[i]
  101. if (!matches || Object.keys(matches).length === 0) {
  102. if (self.nonull) {
  103. // do like the shell, and spit out the literal glob
  104. var literal = self.minimatch.globSet[i]
  105. if (nou)
  106. all.push(literal)
  107. else
  108. all[literal] = true
  109. }
  110. } else {
  111. // had matches
  112. var m = Object.keys(matches)
  113. if (nou)
  114. all.push.apply(all, m)
  115. else
  116. m.forEach(function (m) {
  117. all[m] = true
  118. })
  119. }
  120. }
  121. if (!nou)
  122. all = Object.keys(all)
  123. if (!self.nosort)
  124. all = all.sort(self.nocase ? alphasorti : alphasort)
  125. // at *some* point we statted all of these
  126. if (self.mark) {
  127. for (var i = 0; i < all.length; i++) {
  128. all[i] = self._mark(all[i])
  129. }
  130. if (self.nodir) {
  131. all = all.filter(function (e) {
  132. return !(/\/$/.test(e))
  133. })
  134. }
  135. }
  136. if (self.ignore.length)
  137. all = all.filter(function(m) {
  138. return !isIgnored(self, m)
  139. })
  140. self.found = all
  141. }
  142. function mark (self, p) {
  143. var abs = makeAbs(self, p)
  144. var c = self.cache[abs]
  145. var m = p
  146. if (c) {
  147. var isDir = c === 'DIR' || Array.isArray(c)
  148. var slash = p.slice(-1) === '/'
  149. if (isDir && !slash)
  150. m += '/'
  151. else if (!isDir && slash)
  152. m = m.slice(0, -1)
  153. if (m !== p) {
  154. var mabs = makeAbs(self, m)
  155. self.statCache[mabs] = self.statCache[abs]
  156. self.cache[mabs] = self.cache[abs]
  157. }
  158. }
  159. return m
  160. }
  161. // lotta situps...
  162. function makeAbs (self, f) {
  163. var abs = f
  164. if (f.charAt(0) === '/') {
  165. abs = path.join(self.root, f)
  166. } else if (isAbsolute(f) || f === '') {
  167. abs = f
  168. } else if (self.changedCwd) {
  169. abs = path.resolve(self.cwd, f)
  170. } else {
  171. abs = path.resolve(f)
  172. }
  173. return abs
  174. }
  175. // Return true, if pattern ends with globstar '**', for the accompanying parent directory.
  176. // Ex:- If node_modules/** is the pattern, add 'node_modules' to ignore list along with it's contents
  177. function isIgnored (self, path) {
  178. if (!self.ignore.length)
  179. return false
  180. return self.ignore.some(function(item) {
  181. return item.matcher.match(path) || !!(item.gmatcher && item.gmatcher.match(path))
  182. })
  183. }
  184. function childrenIgnored (self, path) {
  185. if (!self.ignore.length)
  186. return false
  187. return self.ignore.some(function(item) {
  188. return !!(item.gmatcher && item.gmatcher.match(path))
  189. })
  190. }