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 6.0KB

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