sync.js 11 KB

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