1 module.exports = globSync
2 globSync.GlobSync = GlobSync
4 var rp = require('fs.realpath')
5 var minimatch = require('minimatch')
6 var Minimatch = minimatch.Minimatch
7 var Glob = require('./glob.js').Glob
8 var util = require('util')
9 var path = require('path')
10 var assert = require('assert')
11 var isAbsolute = require('path-is-absolute')
12 var common = require('./common.js')
13 var setopts = common.setopts
14 var ownProp = common.ownProp
15 var childrenIgnored = common.childrenIgnored
16 var isIgnored = common.isIgnored
18 function globSync (pattern, options) {
19 if (typeof options === 'function' || arguments.length === 3)
20 throw new TypeError('callback provided to sync glob\n'+
21 'See: https://github.com/isaacs/node-glob/issues/167')
23 return new GlobSync(pattern, options).found
26 function GlobSync (pattern, options) {
28 throw new Error('must provide pattern')
30 if (typeof options === 'function' || arguments.length === 3)
31 throw new TypeError('callback provided to sync glob\n'+
32 'See: https://github.com/isaacs/node-glob/issues/167')
34 if (!(this instanceof GlobSync))
35 return new GlobSync(pattern, options)
37 setopts(this, pattern, options)
42 var n = this.minimatch.set.length
43 this.matches = new Array(n)
44 for (var i = 0; i < n; i ++) {
45 this._process(this.minimatch.set[i], i, false)
50 GlobSync.prototype._finish = function () {
51 assert(this instanceof GlobSync)
54 this.matches.forEach(function (matchset, index) {
55 var set = self.matches[index] = Object.create(null)
56 for (var p in matchset) {
59 var real = rp.realpathSync(p, self.realpathCache)
62 if (er.syscall === 'stat')
63 set[self._makeAbs(p)] = true
74 GlobSync.prototype._process = function (pattern, index, inGlobStar) {
75 assert(this instanceof GlobSync)
77 // Get the first [n] parts of pattern that are all strings.
79 while (typeof pattern[n] === 'string') {
82 // now n is the index of the first one that is *not* a string.
84 // See if there's anything else
87 // if not, then this is rather simple
89 this._processSimple(pattern.join('/'), index)
93 // pattern *starts* with some non-trivial item.
94 // going to readdir(cwd), but not include the prefix in matches.
99 // pattern has some string bits in the front.
100 // whatever it starts with, whether that's 'absolute' like /foo/bar,
101 // or 'relative' like '../baz'
102 prefix = pattern.slice(0, n).join('/')
106 var remain = pattern.slice(n)
108 // get the list of entries.
112 else if (isAbsolute(prefix) || isAbsolute(pattern.join('/'))) {
113 if (!prefix || !isAbsolute(prefix))
114 prefix = '/' + prefix
119 var abs = this._makeAbs(read)
121 //if ignored, skip processing
122 if (childrenIgnored(this, read))
125 var isGlobStar = remain[0] === minimatch.GLOBSTAR
127 this._processGlobStar(prefix, read, abs, remain, index, inGlobStar)
129 this._processReaddir(prefix, read, abs, remain, index, inGlobStar)
133 GlobSync.prototype._processReaddir = function (prefix, read, abs, remain, index, inGlobStar) {
134 var entries = this._readdir(abs, inGlobStar)
136 // if the abs isn't a dir, then nothing can match!
140 // It will only match dot entries if it starts with a dot, or if
141 // dot is set. Stuff like @(.foo|.bar) isn't allowed.
143 var negate = !!this.minimatch.negate
144 var rawGlob = pn._glob
145 var dotOk = this.dot || rawGlob.charAt(0) === '.'
147 var matchedEntries = []
148 for (var i = 0; i < entries.length; i++) {
150 if (e.charAt(0) !== '.' || dotOk) {
152 if (negate && !prefix) {
158 matchedEntries.push(e)
162 var len = matchedEntries.length
163 // If there are no matched entries, then nothing matches.
167 // if this is the last remaining pattern bit, then no need for
168 // an additional stat *unless* the user has specified mark or
169 // stat explicitly. We know they exist, since readdir returned
172 if (remain.length === 1 && !this.mark && !this.stat) {
173 if (!this.matches[index])
174 this.matches[index] = Object.create(null)
176 for (var i = 0; i < len; i ++) {
177 var e = matchedEntries[i]
179 if (prefix.slice(-1) !== '/')
185 if (e.charAt(0) === '/' && !this.nomount) {
186 e = path.join(this.root, e)
188 this._emitMatch(index, e)
190 // This was the last one, and no stats were needed
194 // now test all matched entries as stand-ins for that part
197 for (var i = 0; i < len; i ++) {
198 var e = matchedEntries[i]
201 newPattern = [prefix, e]
204 this._process(newPattern.concat(remain), index, inGlobStar)
209 GlobSync.prototype._emitMatch = function (index, e) {
210 if (isIgnored(this, e))
213 var abs = this._makeAbs(e)
222 if (this.matches[index][e])
226 var c = this.cache[abs]
227 if (c === 'DIR' || Array.isArray(c))
231 this.matches[index][e] = true
238 GlobSync.prototype._readdirInGlobStar = function (abs) {
239 // follow all symlinked directories forever
240 // just proceed as if this is a non-globstar situation
242 return this._readdir(abs, false)
248 lstat = this.fs.lstatSync(abs)
250 if (er.code === 'ENOENT') {
251 // lstat failed, doesn't exist
256 var isSym = lstat && lstat.isSymbolicLink()
257 this.symlinks[abs] = isSym
259 // If it's not a symlink or a dir, then it's definitely a regular file.
260 // don't bother doing a readdir in that case.
261 if (!isSym && lstat && !lstat.isDirectory())
262 this.cache[abs] = 'FILE'
264 entries = this._readdir(abs, false)
269 GlobSync.prototype._readdir = function (abs, inGlobStar) {
272 if (inGlobStar && !ownProp(this.symlinks, abs))
273 return this._readdirInGlobStar(abs)
275 if (ownProp(this.cache, abs)) {
276 var c = this.cache[abs]
277 if (!c || c === 'FILE')
280 if (Array.isArray(c))
285 return this._readdirEntries(abs, this.fs.readdirSync(abs))
287 this._readdirError(abs, er)
292 GlobSync.prototype._readdirEntries = function (abs, entries) {
293 // if we haven't asked to stat everything, then just
294 // assume that everything in there exists, so we can avoid
295 // having to stat it a second time.
296 if (!this.mark && !this.stat) {
297 for (var i = 0; i < entries.length; i ++) {
307 this.cache[abs] = entries
309 // mark and cache dir-ness
313 GlobSync.prototype._readdirError = function (f, er) {
314 // handle errors, and cache the information
316 case 'ENOTSUP': // https://github.com/isaacs/node-glob/issues/205
317 case 'ENOTDIR': // totally normal. means it *does* exist.
318 var abs = this._makeAbs(f)
319 this.cache[abs] = 'FILE'
320 if (abs === this.cwdAbs) {
321 var error = new Error(er.code + ' invalid cwd ' + this.cwd)
322 error.path = this.cwd
328 case 'ENOENT': // not terribly unusual
332 this.cache[this._makeAbs(f)] = false
335 default: // some unusual error. Treat as failure.
336 this.cache[this._makeAbs(f)] = false
340 console.error('glob error', er)
345 GlobSync.prototype._processGlobStar = function (prefix, read, abs, remain, index, inGlobStar) {
347 var entries = this._readdir(abs, inGlobStar)
349 // no entries means not a dir, so it can never have matches
350 // foo.txt/** doesn't match foo.txt
354 // test without the globstar, and with every child both below
355 // and replacing the globstar.
356 var remainWithoutGlobStar = remain.slice(1)
357 var gspref = prefix ? [ prefix ] : []
358 var noGlobStar = gspref.concat(remainWithoutGlobStar)
360 // the noGlobStar pattern exits the inGlobStar state
361 this._process(noGlobStar, index, false)
363 var len = entries.length
364 var isSym = this.symlinks[abs]
366 // If it's a symlink, and we're in a globstar, then stop
367 if (isSym && inGlobStar)
370 for (var i = 0; i < len; i++) {
372 if (e.charAt(0) === '.' && !this.dot)
375 // these two cases enter the inGlobStar state
376 var instead = gspref.concat(entries[i], remainWithoutGlobStar)
377 this._process(instead, index, true)
379 var below = gspref.concat(entries[i], remain)
380 this._process(below, index, true)
384 GlobSync.prototype._processSimple = function (prefix, index) {
385 // XXX review this. Shouldn't it be doing the mounting etc
386 // before doing stat? kinda weird?
387 var exists = this._stat(prefix)
389 if (!this.matches[index])
390 this.matches[index] = Object.create(null)
392 // If it doesn't exist, then just mark the lack of results
396 if (prefix && isAbsolute(prefix) && !this.nomount) {
397 var trail = /[\/\\]$/.test(prefix)
398 if (prefix.charAt(0) === '/') {
399 prefix = path.join(this.root, prefix)
401 prefix = path.resolve(this.root, prefix)
407 if (process.platform === 'win32')
408 prefix = prefix.replace(/\\/g, '/')
410 // Mark this as a match
411 this._emitMatch(index, prefix)
414 // Returns either 'DIR', 'FILE', or false
415 GlobSync.prototype._stat = function (f) {
416 var abs = this._makeAbs(f)
417 var needDir = f.slice(-1) === '/'
419 if (f.length > this.maxLength)
422 if (!this.stat && ownProp(this.cache, abs)) {
423 var c = this.cache[abs]
425 if (Array.isArray(c))
428 // It exists, but maybe not how we need it
429 if (!needDir || c === 'DIR')
432 if (needDir && c === 'FILE')
435 // otherwise we have to stat, because maybe c=true
436 // if we know it exists, but not what it is.
440 var stat = this.statCache[abs]
444 lstat = this.fs.lstatSync(abs)
446 if (er && (er.code === 'ENOENT' || er.code === 'ENOTDIR')) {
447 this.statCache[abs] = false
452 if (lstat && lstat.isSymbolicLink()) {
454 stat = this.fs.statSync(abs)
463 this.statCache[abs] = stat
467 c = stat.isDirectory() ? 'DIR' : 'FILE'
469 this.cache[abs] = this.cache[abs] || c
471 if (needDir && c === 'FILE')
477 GlobSync.prototype._mark = function (p) {
478 return common.mark(this, p)
481 GlobSync.prototype._makeAbs = function (f) {
482 return common.makeAbs(this, f)