Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. module.exports = globSync
  2. globSync.GlobSync = GlobSync
  3. var rp = require('fs.realpath')
  4. var minimatch = require('minimatch')
  5. var Minimatch = minimatch.Minimatch
  6. var Glob = require('./glob.js').Glob
  7. var util = require('util')
  8. var path = require('path')
  9. var assert = require('assert')
  10. var isAbsolute = require('path-is-absolute')
  11. var common = require('./common.js')
  12. var setopts = common.setopts
  13. var ownProp = common.ownProp
  14. var childrenIgnored = common.childrenIgnored
  15. var isIgnored = common.isIgnored
  16. function globSync (pattern, options) {
  17. if (typeof options === 'function' || arguments.length === 3)
  18. throw new TypeError('callback provided to sync glob\n'+
  19. 'See: https://github.com/isaacs/node-glob/issues/167')
  20. return new GlobSync(pattern, options).found
  21. }
  22. function GlobSync (pattern, options) {
  23. if (!pattern)
  24. throw new Error('must provide pattern')
  25. if (typeof options === 'function' || arguments.length === 3)
  26. throw new TypeError('callback provided to sync glob\n'+
  27. 'See: https://github.com/isaacs/node-glob/issues/167')
  28. if (!(this instanceof GlobSync))
  29. return new GlobSync(pattern, options)
  30. setopts(this, pattern, options)
  31. if (this.noprocess)
  32. return this
  33. var n = this.minimatch.set.length
  34. this.matches = new Array(n)
  35. for (var i = 0; i < n; i ++) {
  36. this._process(this.minimatch.set[i], i, false)
  37. }
  38. this._finish()
  39. }
  40. GlobSync.prototype._finish = function () {
  41. assert.ok(this instanceof GlobSync)
  42. if (this.realpath) {
  43. var self = this
  44. this.matches.forEach(function (matchset, index) {
  45. var set = self.matches[index] = Object.create(null)
  46. for (var p in matchset) {
  47. try {
  48. p = self._makeAbs(p)
  49. var real = rp.realpathSync(p, self.realpathCache)
  50. set[real] = true
  51. } catch (er) {
  52. if (er.syscall === 'stat')
  53. set[self._makeAbs(p)] = true
  54. else
  55. throw er
  56. }
  57. }
  58. })
  59. }
  60. common.finish(this)
  61. }
  62. GlobSync.prototype._process = function (pattern, index, inGlobStar) {
  63. assert.ok(this instanceof GlobSync)
  64. // Get the first [n] parts of pattern that are all strings.
  65. var n = 0
  66. while (typeof pattern[n] === 'string') {
  67. n ++
  68. }
  69. // now n is the index of the first one that is *not* a string.
  70. // See if there's anything else
  71. var prefix
  72. switch (n) {
  73. // if not, then this is rather simple
  74. case pattern.length:
  75. this._processSimple(pattern.join('/'), index)
  76. return
  77. case 0:
  78. // pattern *starts* with some non-trivial item.
  79. // going to readdir(cwd), but not include the prefix in matches.
  80. prefix = null
  81. break
  82. default:
  83. // pattern has some string bits in the front.
  84. // whatever it starts with, whether that's 'absolute' like /foo/bar,
  85. // or 'relative' like '../baz'
  86. prefix = pattern.slice(0, n).join('/')
  87. break
  88. }
  89. var remain = pattern.slice(n)
  90. // get the list of entries.
  91. var read
  92. if (prefix === null)
  93. read = '.'
  94. else if (isAbsolute(prefix) ||
  95. isAbsolute(pattern.map(function (p) {
  96. return typeof p === 'string' ? p : '[*]'
  97. }).join('/'))) {
  98. if (!prefix || !isAbsolute(prefix))
  99. prefix = '/' + prefix
  100. read = prefix
  101. } else
  102. read = prefix
  103. var abs = this._makeAbs(read)
  104. //if ignored, skip processing
  105. if (childrenIgnored(this, read))
  106. return
  107. var isGlobStar = remain[0] === minimatch.GLOBSTAR
  108. if (isGlobStar)
  109. this._processGlobStar(prefix, read, abs, remain, index, inGlobStar)
  110. else
  111. this._processReaddir(prefix, read, abs, remain, index, inGlobStar)
  112. }
  113. GlobSync.prototype._processReaddir = function (prefix, read, abs, remain, index, inGlobStar) {
  114. var entries = this._readdir(abs, inGlobStar)
  115. // if the abs isn't a dir, then nothing can match!
  116. if (!entries)
  117. return
  118. // It will only match dot entries if it starts with a dot, or if
  119. // dot is set. Stuff like @(.foo|.bar) isn't allowed.
  120. var pn = remain[0]
  121. var negate = !!this.minimatch.negate
  122. var rawGlob = pn._glob
  123. var dotOk = this.dot || rawGlob.charAt(0) === '.'
  124. var matchedEntries = []
  125. for (var i = 0; i < entries.length; i++) {
  126. var e = entries[i]
  127. if (e.charAt(0) !== '.' || dotOk) {
  128. var m
  129. if (negate && !prefix) {
  130. m = !e.match(pn)
  131. } else {
  132. m = e.match(pn)
  133. }
  134. if (m)
  135. matchedEntries.push(e)
  136. }
  137. }
  138. var len = matchedEntries.length
  139. // If there are no matched entries, then nothing matches.
  140. if (len === 0)
  141. return
  142. // if this is the last remaining pattern bit, then no need for
  143. // an additional stat *unless* the user has specified mark or
  144. // stat explicitly. We know they exist, since readdir returned
  145. // them.
  146. if (remain.length === 1 && !this.mark && !this.stat) {
  147. if (!this.matches[index])
  148. this.matches[index] = Object.create(null)
  149. for (var i = 0; i < len; i ++) {
  150. var e = matchedEntries[i]
  151. if (prefix) {
  152. if (prefix.slice(-1) !== '/')
  153. e = prefix + '/' + e
  154. else
  155. e = prefix + e
  156. }
  157. if (e.charAt(0) === '/' && !this.nomount) {
  158. e = path.join(this.root, e)
  159. }
  160. this._emitMatch(index, e)
  161. }
  162. // This was the last one, and no stats were needed
  163. return
  164. }
  165. // now test all matched entries as stand-ins for that part
  166. // of the pattern.
  167. remain.shift()
  168. for (var i = 0; i < len; i ++) {
  169. var e = matchedEntries[i]
  170. var newPattern
  171. if (prefix)
  172. newPattern = [prefix, e]
  173. else
  174. newPattern = [e]
  175. this._process(newPattern.concat(remain), index, inGlobStar)
  176. }
  177. }
  178. GlobSync.prototype._emitMatch = function (index, e) {
  179. if (isIgnored(this, e))
  180. return
  181. var abs = this._makeAbs(e)
  182. if (this.mark)
  183. e = this._mark(e)
  184. if (this.absolute) {
  185. e = abs
  186. }
  187. if (this.matches[index][e])
  188. return
  189. if (this.nodir) {
  190. var c = this.cache[abs]
  191. if (c === 'DIR' || Array.isArray(c))
  192. return
  193. }
  194. this.matches[index][e] = true
  195. if (this.stat)
  196. this._stat(e)
  197. }
  198. GlobSync.prototype._readdirInGlobStar = function (abs) {
  199. // follow all symlinked directories forever
  200. // just proceed as if this is a non-globstar situation
  201. if (this.follow)
  202. return this._readdir(abs, false)
  203. var entries
  204. var lstat
  205. var stat
  206. try {
  207. lstat = this.fs.lstatSync(abs)
  208. } catch (er) {
  209. if (er.code === 'ENOENT') {
  210. // lstat failed, doesn't exist
  211. return null
  212. }
  213. }
  214. var isSym = lstat && lstat.isSymbolicLink()
  215. this.symlinks[abs] = isSym
  216. // If it's not a symlink or a dir, then it's definitely a regular file.
  217. // don't bother doing a readdir in that case.
  218. if (!isSym && lstat && !lstat.isDirectory())
  219. this.cache[abs] = 'FILE'
  220. else
  221. entries = this._readdir(abs, false)
  222. return entries
  223. }
  224. GlobSync.prototype._readdir = function (abs, inGlobStar) {
  225. var entries
  226. if (inGlobStar && !ownProp(this.symlinks, abs))
  227. return this._readdirInGlobStar(abs)
  228. if (ownProp(this.cache, abs)) {
  229. var c = this.cache[abs]
  230. if (!c || c === 'FILE')
  231. return null
  232. if (Array.isArray(c))
  233. return c
  234. }
  235. try {
  236. return this._readdirEntries(abs, this.fs.readdirSync(abs))
  237. } catch (er) {
  238. this._readdirError(abs, er)
  239. return null
  240. }
  241. }
  242. GlobSync.prototype._readdirEntries = function (abs, entries) {
  243. // if we haven't asked to stat everything, then just
  244. // assume that everything in there exists, so we can avoid
  245. // having to stat it a second time.
  246. if (!this.mark && !this.stat) {
  247. for (var i = 0; i < entries.length; i ++) {
  248. var e = entries[i]
  249. if (abs === '/')
  250. e = abs + e
  251. else
  252. e = abs + '/' + e
  253. this.cache[e] = true
  254. }
  255. }
  256. this.cache[abs] = entries
  257. // mark and cache dir-ness
  258. return entries
  259. }
  260. GlobSync.prototype._readdirError = function (f, er) {
  261. // handle errors, and cache the information
  262. switch (er.code) {
  263. case 'ENOTSUP': // https://github.com/isaacs/node-glob/issues/205
  264. case 'ENOTDIR': // totally normal. means it *does* exist.
  265. var abs = this._makeAbs(f)
  266. this.cache[abs] = 'FILE'
  267. if (abs === this.cwdAbs) {
  268. var error = new Error(er.code + ' invalid cwd ' + this.cwd)
  269. error.path = this.cwd
  270. error.code = er.code
  271. throw error
  272. }
  273. break
  274. case 'ENOENT': // not terribly unusual
  275. case 'ELOOP':
  276. case 'ENAMETOOLONG':
  277. case 'UNKNOWN':
  278. this.cache[this._makeAbs(f)] = false
  279. break
  280. default: // some unusual error. Treat as failure.
  281. this.cache[this._makeAbs(f)] = false
  282. if (this.strict)
  283. throw er
  284. if (!this.silent)
  285. console.error('glob error', er)
  286. break
  287. }
  288. }
  289. GlobSync.prototype._processGlobStar = function (prefix, read, abs, remain, index, inGlobStar) {
  290. var entries = this._readdir(abs, inGlobStar)
  291. // no entries means not a dir, so it can never have matches
  292. // foo.txt/** doesn't match foo.txt
  293. if (!entries)
  294. return
  295. // test without the globstar, and with every child both below
  296. // and replacing the globstar.
  297. var remainWithoutGlobStar = remain.slice(1)
  298. var gspref = prefix ? [ prefix ] : []
  299. var noGlobStar = gspref.concat(remainWithoutGlobStar)
  300. // the noGlobStar pattern exits the inGlobStar state
  301. this._process(noGlobStar, index, false)
  302. var len = entries.length
  303. var isSym = this.symlinks[abs]
  304. // If it's a symlink, and we're in a globstar, then stop
  305. if (isSym && inGlobStar)
  306. return
  307. for (var i = 0; i < len; i++) {
  308. var e = entries[i]
  309. if (e.charAt(0) === '.' && !this.dot)
  310. continue
  311. // these two cases enter the inGlobStar state
  312. var instead = gspref.concat(entries[i], remainWithoutGlobStar)
  313. this._process(instead, index, true)
  314. var below = gspref.concat(entries[i], remain)
  315. this._process(below, index, true)
  316. }
  317. }
  318. GlobSync.prototype._processSimple = function (prefix, index) {
  319. // XXX review this. Shouldn't it be doing the mounting etc
  320. // before doing stat? kinda weird?
  321. var exists = this._stat(prefix)
  322. if (!this.matches[index])
  323. this.matches[index] = Object.create(null)
  324. // If it doesn't exist, then just mark the lack of results
  325. if (!exists)
  326. return
  327. if (prefix && isAbsolute(prefix) && !this.nomount) {
  328. var trail = /[\/\\]$/.test(prefix)
  329. if (prefix.charAt(0) === '/') {
  330. prefix = path.join(this.root, prefix)
  331. } else {
  332. prefix = path.resolve(this.root, prefix)
  333. if (trail)
  334. prefix += '/'
  335. }
  336. }
  337. if (process.platform === 'win32')
  338. prefix = prefix.replace(/\\/g, '/')
  339. // Mark this as a match
  340. this._emitMatch(index, prefix)
  341. }
  342. // Returns either 'DIR', 'FILE', or false
  343. GlobSync.prototype._stat = function (f) {
  344. var abs = this._makeAbs(f)
  345. var needDir = f.slice(-1) === '/'
  346. if (f.length > this.maxLength)
  347. return false
  348. if (!this.stat && ownProp(this.cache, abs)) {
  349. var c = this.cache[abs]
  350. if (Array.isArray(c))
  351. c = 'DIR'
  352. // It exists, but maybe not how we need it
  353. if (!needDir || c === 'DIR')
  354. return c
  355. if (needDir && c === 'FILE')
  356. return false
  357. // otherwise we have to stat, because maybe c=true
  358. // if we know it exists, but not what it is.
  359. }
  360. var exists
  361. var stat = this.statCache[abs]
  362. if (!stat) {
  363. var lstat
  364. try {
  365. lstat = this.fs.lstatSync(abs)
  366. } catch (er) {
  367. if (er && (er.code === 'ENOENT' || er.code === 'ENOTDIR')) {
  368. this.statCache[abs] = false
  369. return false
  370. }
  371. }
  372. if (lstat && lstat.isSymbolicLink()) {
  373. try {
  374. stat = this.fs.statSync(abs)
  375. } catch (er) {
  376. stat = lstat
  377. }
  378. } else {
  379. stat = lstat
  380. }
  381. }
  382. this.statCache[abs] = stat
  383. var c = true
  384. if (stat)
  385. c = stat.isDirectory() ? 'DIR' : 'FILE'
  386. this.cache[abs] = this.cache[abs] || c
  387. if (needDir && c === 'FILE')
  388. return false
  389. return c
  390. }
  391. GlobSync.prototype._mark = function (p) {
  392. return common.mark(this, p)
  393. }
  394. GlobSync.prototype._makeAbs = function (f) {
  395. return common.makeAbs(this, f)
  396. }