1 module.exports = globSync
2 globSync.GlobSync = GlobSync
5 var rp = require('fs.realpath')
6 var minimatch = require('minimatch')
7 var Minimatch = minimatch.Minimatch
8 var Glob = require('./glob.js').Glob
9 var util = require('util')
10 var path = require('path')
11 var assert = require('assert')
12 var isAbsolute = require('path-is-absolute')
13 var common = require('./common.js')
14 var alphasort = common.alphasort
15 var alphasorti = common.alphasorti
16 var setopts = common.setopts
17 var ownProp = common.ownProp
18 var childrenIgnored = common.childrenIgnored
20 function globSync (pattern, options) {
21 if (typeof options === 'function' || arguments.length === 3)
22 throw new TypeError('callback provided to sync glob\n'+
23 'See: https://github.com/isaacs/node-glob/issues/167')
25 return new GlobSync(pattern, options).found
28 function GlobSync (pattern, options) {
30 throw new Error('must provide pattern')
32 if (typeof options === 'function' || arguments.length === 3)
33 throw new TypeError('callback provided to sync glob\n'+
34 'See: https://github.com/isaacs/node-glob/issues/167')
36 if (!(this instanceof GlobSync))
37 return new GlobSync(pattern, options)
39 setopts(this, pattern, options)
44 var n = this.minimatch.set.length
45 this.matches = new Array(n)
46 for (var i = 0; i < n; i ++) {
47 this._process(this.minimatch.set[i], i, false)
52 GlobSync.prototype._finish = function () {
53 assert(this instanceof GlobSync)
56 this.matches.forEach(function (matchset, index) {
57 var set = self.matches[index] = Object.create(null)
58 for (var p in matchset) {
61 var real = rp.realpathSync(p, self.realpathCache)
64 if (er.syscall === 'stat')
65 set[self._makeAbs(p)] = true
76 GlobSync.prototype._process = function (pattern, index, inGlobStar) {
77 assert(this instanceof GlobSync)
79 // Get the first [n] parts of pattern that are all strings.
81 while (typeof pattern[n] === 'string') {
84 // now n is the index of the first one that is *not* a string.
86 // See if there's anything else
89 // if not, then this is rather simple
91 this._processSimple(pattern.join('/'), index)
95 // pattern *starts* with some non-trivial item.
96 // going to readdir(cwd), but not include the prefix in matches.
101 // pattern has some string bits in the front.
102 // whatever it starts with, whether that's 'absolute' like /foo/bar,
103 // or 'relative' like '../baz'
104 prefix = pattern.slice(0, n).join('/')
108 var remain = pattern.slice(n)
110 // get the list of entries.
114 else if (isAbsolute(prefix) || isAbsolute(pattern.join('/'))) {
115 if (!prefix || !isAbsolute(prefix))
116 prefix = '/' + prefix
121 var abs = this._makeAbs(read)
123 //if ignored, skip processing
124 if (childrenIgnored(this, read))
127 var isGlobStar = remain[0] === minimatch.GLOBSTAR
129 this._processGlobStar(prefix, read, abs, remain, index, inGlobStar)
131 this._processReaddir(prefix, read, abs, remain, index, inGlobStar)
135 GlobSync.prototype._processReaddir = function (prefix, read, abs, remain, index, inGlobStar) {
136 var entries = this._readdir(abs, inGlobStar)
138 // if the abs isn't a dir, then nothing can match!
142 // It will only match dot entries if it starts with a dot, or if
143 // dot is set. Stuff like @(.foo|.bar) isn't allowed.
145 var negate = !!this.minimatch.negate
146 var rawGlob = pn._glob
147 var dotOk = this.dot || rawGlob.charAt(0) === '.'
149 var matchedEntries = []
150 for (var i = 0; i < entries.length; i++) {
152 if (e.charAt(0) !== '.' || dotOk) {
154 if (negate && !prefix) {
160 matchedEntries.push(e)
164 var len = matchedEntries.length
165 // If there are no matched entries, then nothing matches.
169 // if this is the last remaining pattern bit, then no need for
170 // an additional stat *unless* the user has specified mark or
171 // stat explicitly. We know they exist, since readdir returned
174 if (remain.length === 1 && !this.mark && !this.stat) {
175 if (!this.matches[index])
176 this.matches[index] = Object.create(null)
178 for (var i = 0; i < len; i ++) {
179 var e = matchedEntries[i]
181 if (prefix.slice(-1) !== '/')
187 if (e.charAt(0) === '/' && !this.nomount) {
188 e = path.join(this.root, e)
190 this.matches[index][e] = true
192 // This was the last one, and no stats were needed
196 // now test all matched entries as stand-ins for that part
199 for (var i = 0; i < len; i ++) {
200 var e = matchedEntries[i]
203 newPattern = [prefix, e]
206 this._process(newPattern.concat(remain), index, inGlobStar)
211 GlobSync.prototype._emitMatch = function (index, e) {
212 var abs = this._makeAbs(e)
216 if (this.matches[index][e])
220 var c = this.cache[this._makeAbs(e)]
221 if (c === 'DIR' || Array.isArray(c))
225 this.matches[index][e] = true
231 GlobSync.prototype._readdirInGlobStar = function (abs) {
232 // follow all symlinked directories forever
233 // just proceed as if this is a non-globstar situation
235 return this._readdir(abs, false)
241 lstat = fs.lstatSync(abs)
243 // lstat failed, doesn't exist
247 var isSym = lstat.isSymbolicLink()
248 this.symlinks[abs] = isSym
250 // If it's not a symlink or a dir, then it's definitely a regular file.
251 // don't bother doing a readdir in that case.
252 if (!isSym && !lstat.isDirectory())
253 this.cache[abs] = 'FILE'
255 entries = this._readdir(abs, false)
260 GlobSync.prototype._readdir = function (abs, inGlobStar) {
263 if (inGlobStar && !ownProp(this.symlinks, abs))
264 return this._readdirInGlobStar(abs)
266 if (ownProp(this.cache, abs)) {
267 var c = this.cache[abs]
268 if (!c || c === 'FILE')
271 if (Array.isArray(c))
276 return this._readdirEntries(abs, fs.readdirSync(abs))
278 this._readdirError(abs, er)
283 GlobSync.prototype._readdirEntries = function (abs, entries) {
284 // if we haven't asked to stat everything, then just
285 // assume that everything in there exists, so we can avoid
286 // having to stat it a second time.
287 if (!this.mark && !this.stat) {
288 for (var i = 0; i < entries.length; i ++) {
298 this.cache[abs] = entries
300 // mark and cache dir-ness
304 GlobSync.prototype._readdirError = function (f, er) {
305 // handle errors, and cache the information
307 case 'ENOTSUP': // https://github.com/isaacs/node-glob/issues/205
308 case 'ENOTDIR': // totally normal. means it *does* exist.
309 var abs = this._makeAbs(f)
310 this.cache[abs] = 'FILE'
311 if (abs === this.cwdAbs) {
312 var error = new Error(er.code + ' invalid cwd ' + this.cwd)
313 error.path = this.cwd
319 case 'ENOENT': // not terribly unusual
323 this.cache[this._makeAbs(f)] = false
326 default: // some unusual error. Treat as failure.
327 this.cache[this._makeAbs(f)] = false
331 console.error('glob error', er)
336 GlobSync.prototype._processGlobStar = function (prefix, read, abs, remain, index, inGlobStar) {
338 var entries = this._readdir(abs, inGlobStar)
340 // no entries means not a dir, so it can never have matches
341 // foo.txt/** doesn't match foo.txt
345 // test without the globstar, and with every child both below
346 // and replacing the globstar.
347 var remainWithoutGlobStar = remain.slice(1)
348 var gspref = prefix ? [ prefix ] : []
349 var noGlobStar = gspref.concat(remainWithoutGlobStar)
351 // the noGlobStar pattern exits the inGlobStar state
352 this._process(noGlobStar, index, false)
354 var len = entries.length
355 var isSym = this.symlinks[abs]
357 // If it's a symlink, and we're in a globstar, then stop
358 if (isSym && inGlobStar)
361 for (var i = 0; i < len; i++) {
363 if (e.charAt(0) === '.' && !this.dot)
366 // these two cases enter the inGlobStar state
367 var instead = gspref.concat(entries[i], remainWithoutGlobStar)
368 this._process(instead, index, true)
370 var below = gspref.concat(entries[i], remain)
371 this._process(below, index, true)
375 GlobSync.prototype._processSimple = function (prefix, index) {
376 // XXX review this. Shouldn't it be doing the mounting etc
377 // before doing stat? kinda weird?
378 var exists = this._stat(prefix)
380 if (!this.matches[index])
381 this.matches[index] = Object.create(null)
383 // If it doesn't exist, then just mark the lack of results
387 if (prefix && isAbsolute(prefix) && !this.nomount) {
388 var trail = /[\/\\]$/.test(prefix)
389 if (prefix.charAt(0) === '/') {
390 prefix = path.join(this.root, prefix)
392 prefix = path.resolve(this.root, prefix)
398 if (process.platform === 'win32')
399 prefix = prefix.replace(/\\/g, '/')
401 // Mark this as a match
402 this.matches[index][prefix] = true
405 // Returns either 'DIR', 'FILE', or false
406 GlobSync.prototype._stat = function (f) {
407 var abs = this._makeAbs(f)
408 var needDir = f.slice(-1) === '/'
410 if (f.length > this.maxLength)
413 if (!this.stat && ownProp(this.cache, abs)) {
414 var c = this.cache[abs]
416 if (Array.isArray(c))
419 // It exists, but maybe not how we need it
420 if (!needDir || c === 'DIR')
423 if (needDir && c === 'FILE')
426 // otherwise we have to stat, because maybe c=true
427 // if we know it exists, but not what it is.
431 var stat = this.statCache[abs]
435 lstat = fs.lstatSync(abs)
440 if (lstat.isSymbolicLink()) {
442 stat = fs.statSync(abs)
451 this.statCache[abs] = stat
453 var c = stat.isDirectory() ? 'DIR' : 'FILE'
454 this.cache[abs] = this.cache[abs] || c
456 if (needDir && c !== 'DIR')
462 GlobSync.prototype._mark = function (p) {
463 return common.mark(this, p)
466 GlobSync.prototype._makeAbs = function (f) {
467 return common.makeAbs(this, f)