sync.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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(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(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) || isAbsolute(pattern.join('/'))) {
  95. if (!prefix || !isAbsolute(prefix))
  96. prefix = '/' + prefix
  97. read = prefix
  98. } else
  99. read = prefix
  100. var abs = this._makeAbs(read)
  101. //if ignored, skip processing
  102. if (childrenIgnored(this, read))
  103. return
  104. var isGlobStar = remain[0] === minimatch.GLOBSTAR
  105. if (isGlobStar)
  106. this._processGlobStar(prefix, read, abs, remain, index, inGlobStar)
  107. else
  108. this._processReaddir(prefix, read, abs, remain, index, inGlobStar)
  109. }
  110. GlobSync.prototype._processReaddir = function (prefix, read, abs, remain, index, inGlobStar) {
  111. var entries = this._readdir(abs, inGlobStar)
  112. // if the abs isn't a dir, then nothing can match!
  113. if (!entries)
  114. return
  115. // It will only match dot entries if it starts with a dot, or if
  116. // dot is set. Stuff like @(.foo|.bar) isn't allowed.
  117. var pn = remain[0]
  118. var negate = !!this.minimatch.negate
  119. var rawGlob = pn._glob
  120. var dotOk = this.dot || rawGlob.charAt(0) === '.'
  121. var matchedEntries = []
  122. for (var i = 0; i < entries.length; i++) {
  123. var e = entries[i]
  124. if (e.charAt(0) !== '.' || dotOk) {
  125. var m
  126. if (negate && !prefix) {
  127. m = !e.match(pn)
  128. } else {
  129. m = e.match(pn)
  130. }
  131. if (m)
  132. matchedEntries.push(e)
  133. }
  134. }
  135. var len = matchedEntries.length
  136. // If there are no matched entries, then nothing matches.
  137. if (len === 0)
  138. return
  139. // if this is the last remaining pattern bit, then no need for
  140. // an additional stat *unless* the user has specified mark or
  141. // stat explicitly. We know they exist, since readdir returned
  142. // them.
  143. if (remain.length === 1 && !this.mark && !this.stat) {
  144. if (!this.matches[index])
  145. this.matches[index] = Object.create(null)
  146. for (var i = 0; i < len; i ++) {
  147. var e = matchedEntries[i]
  148. if (prefix) {
  149. if (prefix.slice(-1) !== '/')
  150. e = prefix + '/' + e
  151. else
  152. e = prefix + e
  153. }
  154. if (e.charAt(0) === '/' && !this.nomount) {
  155. e = path.join(this.root, e)
  156. }
  157. this._emitMatch(index, e)
  158. }
  159. // This was the last one, and no stats were needed
  160. return
  161. }
  162. // now test all matched entries as stand-ins for that part
  163. // of the pattern.
  164. remain.shift()
  165. for (var i = 0; i < len; i ++) {
  166. var e = matchedEntries[i]
  167. var newPattern
  168. if (prefix)
  169. newPattern = [prefix, e]
  170. else
  171. newPattern = [e]
  172. this._process(newPattern.concat(remain), index, inGlobStar)
  173. }
  174. }
  175. GlobSync.prototype._emitMatch = function (index, e) {
  176. if (isIgnored(this, e))
  177. return
  178. var abs = this._makeAbs(e)
  179. if (this.mark)
  180. e = this._mark(e)
  181. if (this.absolute) {
  182. e = abs
  183. }
  184. if (this.matches[index][e])
  185. return
  186. if (this.nodir) {
  187. var c = this.cache[abs]
  188. if (c === 'DIR' || Array.isArray(c))
  189. return
  190. }
  191. this.matches[index][e] = true
  192. if (this.stat)
  193. this._stat(e)
  194. }
  195. GlobSync.prototype._readdirInGlobStar = function (abs) {
  196. // follow all symlinked directories forever
  197. // just proceed as if this is a non-globstar situation
  198. if (this.follow)
  199. return this._readdir(abs, false)
  200. var entries
  201. var lstat
  202. var stat
  203. try {
  204. lstat = this.fs.lstatSync(abs)
  205. } catch (er) {
  206. if (er.code === 'ENOENT') {
  207. // lstat failed, doesn't exist
  208. return null
  209. }
  210. }
  211. var isSym = lstat && lstat.isSymbolicLink()
  212. this.symlinks[abs] = isSym
  213. // If it's not a symlink or a dir, then it's definitely a regular file.
  214. // don't bother doing a readdir in that case.
  215. if (!isSym && lstat && !lstat.isDirectory())
  216. this.cache[abs] = 'FILE'
  217. else
  218. entries = this._readdir(abs, false)
  219. return entries
  220. }
  221. GlobSync.prototype._readdir = function (abs, inGlobStar) {
  222. var entries
  223. if (inGlobStar && !ownProp(this.symlinks, abs))
  224. return this._readdirInGlobStar(abs)
  225. if (ownProp(this.cache, abs)) {
  226. var c = this.cache[abs]
  227. if (!c || c === 'FILE')
  228. return null
  229. if (Array.isArray(c))
  230. return c
  231. }
  232. try {
  233. return this._readdirEntries(abs, this.fs.readdirSync(abs))
  234. } catch (er) {
  235. this._readdirError(abs, er)
  236. return null
  237. }
  238. }
  239. GlobSync.prototype._readdirEntries = function (abs, entries) {
  240. // if we haven't asked to stat everything, then just
  241. // assume that everything in there exists, so we can avoid
  242. // having to stat it a second time.
  243. if (!this.mark && !this.stat) {
  244. for (var i = 0; i < entries.length; i ++) {
  245. var e = entries[i]
  246. if (abs === '/')
  247. e = abs + e
  248. else
  249. e = abs + '/' + e
  250. this.cache[e] = true
  251. }
  252. }
  253. this.cache[abs] = entries
  254. // mark and cache dir-ness
  255. return entries
  256. }
  257. GlobSync.prototype._readdirError = function (f, er) {
  258. // handle errors, and cache the information
  259. switch (er.code) {
  260. case 'ENOTSUP': // https://github.com/isaacs/node-glob/issues/205
  261. case 'ENOTDIR': // totally normal. means it *does* exist.
  262. var abs = this._makeAbs(f)
  263. this.cache[abs] = 'FILE'
  264. if (abs === this.cwdAbs) {
  265. var error = new Error(er.code + ' invalid cwd ' + this.cwd)
  266. error.path = this.cwd
  267. error.code = er.code
  268. throw error
  269. }
  270. break
  271. case 'ENOENT': // not terribly unusual
  272. case 'ELOOP':
  273. case 'ENAMETOOLONG':
  274. case 'UNKNOWN':
  275. this.cache[this._makeAbs(f)] = false
  276. break
  277. default: // some unusual error. Treat as failure.
  278. this.cache[this._makeAbs(f)] = false
  279. if (this.strict)
  280. throw er
  281. if (!this.silent)
  282. console.error('glob error', er)
  283. break
  284. }
  285. }
  286. GlobSync.prototype._processGlobStar = function (prefix, read, abs, remain, index, inGlobStar) {
  287. var entries = this._readdir(abs, inGlobStar)
  288. // no entries means not a dir, so it can never have matches
  289. // foo.txt/** doesn't match foo.txt
  290. if (!entries)
  291. return
  292. // test without the globstar, and with every child both below
  293. // and replacing the globstar.
  294. var remainWithoutGlobStar = remain.slice(1)
  295. var gspref = prefix ? [ prefix ] : []
  296. var noGlobStar = gspref.concat(remainWithoutGlobStar)
  297. // the noGlobStar pattern exits the inGlobStar state
  298. this._process(noGlobStar, index, false)
  299. var len = entries.length
  300. var isSym = this.symlinks[abs]
  301. // If it's a symlink, and we're in a globstar, then stop
  302. if (isSym && inGlobStar)
  303. return
  304. for (var i = 0; i < len; i++) {
  305. var e = entries[i]
  306. if (e.charAt(0) === '.' && !this.dot)
  307. continue
  308. // these two cases enter the inGlobStar state
  309. var instead = gspref.concat(entries[i], remainWithoutGlobStar)
  310. this._process(instead, index, true)
  311. var below = gspref.concat(entries[i], remain)
  312. this._process(below, index, true)
  313. }
  314. }
  315. GlobSync.prototype._processSimple = function (prefix, index) {
  316. // XXX review this. Shouldn't it be doing the mounting etc
  317. // before doing stat? kinda weird?
  318. var exists = this._stat(prefix)
  319. if (!this.matches[index])
  320. this.matches[index] = Object.create(null)
  321. // If it doesn't exist, then just mark the lack of results
  322. if (!exists)
  323. return
  324. if (prefix && isAbsolute(prefix) && !this.nomount) {
  325. var trail = /[\/\\]$/.test(prefix)
  326. if (prefix.charAt(0) === '/') {
  327. prefix = path.join(this.root, prefix)
  328. } else {
  329. prefix = path.resolve(this.root, prefix)
  330. if (trail)
  331. prefix += '/'
  332. }
  333. }
  334. if (process.platform === 'win32')
  335. prefix = prefix.replace(/\\/g, '/')
  336. // Mark this as a match
  337. this._emitMatch(index, prefix)
  338. }
  339. // Returns either 'DIR', 'FILE', or false
  340. GlobSync.prototype._stat = function (f) {
  341. var abs = this._makeAbs(f)
  342. var needDir = f.slice(-1) === '/'
  343. if (f.length > this.maxLength)
  344. return false
  345. if (!this.stat && ownProp(this.cache, abs)) {
  346. var c = this.cache[abs]
  347. if (Array.isArray(c))
  348. c = 'DIR'
  349. // It exists, but maybe not how we need it
  350. if (!needDir || c === 'DIR')
  351. return c
  352. if (needDir && c === 'FILE')
  353. return false
  354. // otherwise we have to stat, because maybe c=true
  355. // if we know it exists, but not what it is.
  356. }
  357. var exists
  358. var stat = this.statCache[abs]
  359. if (!stat) {
  360. var lstat
  361. try {
  362. lstat = this.fs.lstatSync(abs)
  363. } catch (er) {
  364. if (er && (er.code === 'ENOENT' || er.code === 'ENOTDIR')) {
  365. this.statCache[abs] = false
  366. return false
  367. }
  368. }
  369. if (lstat && lstat.isSymbolicLink()) {
  370. try {
  371. stat = this.fs.statSync(abs)
  372. } catch (er) {
  373. stat = lstat
  374. }
  375. } else {
  376. stat = lstat
  377. }
  378. }
  379. this.statCache[abs] = stat
  380. var c = true
  381. if (stat)
  382. c = stat.isDirectory() ? 'DIR' : 'FILE'
  383. this.cache[abs] = this.cache[abs] || c
  384. if (needDir && c === 'FILE')
  385. return false
  386. return c
  387. }
  388. GlobSync.prototype._mark = function (p) {
  389. return common.mark(this, p)
  390. }
  391. GlobSync.prototype._makeAbs = function (f) {
  392. return common.makeAbs(this, f)
  393. }