3
// 1. Get the minimatch set
4
// 2. For each pattern in the set, PROCESS(pattern)
5
// 3. Store matches per-set, then uniq them
8
// Get the first [n] items from pattern that are all strings
9
// Join these together. This is PREFIX.
10
// If there is no more remaining, then stat(PREFIX) and
11
// add to matches if it succeeds. END.
12
// readdir(PREFIX) as ENTRIES
14
// If pattern[n] is GLOBSTAR
15
// // handle the case where the globstar match is empty
16
// // by pruning it out, and testing the resulting pattern
17
// PROCESS(pattern[0..n] + pattern[n+1 .. $])
18
// // handle other cases.
19
// for ENTRY in ENTRIES (not dotfiles)
20
// // attach globstar + tail onto the entry
21
// PROCESS(pattern[0..n] + ENTRY + pattern[n .. $])
23
// else // not globstar
24
// for ENTRY in ENTRIES (not dotfiles, unless pattern[n] is dot)
25
// Test ENTRY against pattern[n]
27
// If passes, PROCESS(pattern[0..n] + item + pattern[n+1 .. $])
30
// Cache all stats and readdirs results to minimize syscall. Since all
31
// we ever care about is existence and directory-ness, we can just keep
32
// `true` for files, and [children,...] for directories, or `false` for
33
// things that don't exist.
39
var fs = require("graceful-fs")
40
, minimatch = require("minimatch")
41
, Minimatch = minimatch.Minimatch
42
, inherits = require("inherits")
43
, EE = require("events").EventEmitter
44
, path = require("path")
46
, assert = require("assert").ok
48
function glob (pattern, options, cb) {
49
if (typeof options === "function") cb = options, options = {}
50
if (!options) options = {}
52
if (typeof options === "number") {
57
var g = new Glob(pattern, options, cb)
58
return g.sync ? g.found : g
61
glob.fnmatch = deprecated
63
function deprecated () {
64
throw new Error("glob's interface has changed. Please see the docs.")
68
function globSync (pattern, options) {
69
if (typeof options === "number") {
74
options = options || {}
76
return glob(pattern, options)
82
function Glob (pattern, options, cb) {
83
if (!(this instanceof Glob)) {
84
return new Glob(pattern, options, cb)
87
if (typeof cb === "function") {
89
this.on("end", function (matches) {
94
options = options || {}
99
this.maxDepth = options.maxDepth || 1000
100
this.maxLength = options.maxLength || Infinity
101
this.cache = options.cache || {}
102
this.statCache = options.statCache || {}
104
this.changedCwd = false
105
var cwd = process.cwd()
106
if (!options.hasOwnProperty("cwd")) this.cwd = cwd
108
this.cwd = options.cwd
109
this.changedCwd = path.resolve(options.cwd) !== cwd
112
this.root = options.root || path.resolve(this.cwd, "/")
113
this.root = path.resolve(this.root)
114
if (process.platform === "win32")
115
this.root = this.root.replace(/\\/g, "/")
117
this.nomount = !!options.nomount
120
throw new Error("must provide pattern")
123
// base-matching: just use globstar for that.
124
if (options.matchBase && -1 === pattern.indexOf("/")) {
125
if (options.noglobstar) {
126
throw new Error("base matching requires globstar")
128
pattern = "**/" + pattern
131
this.strict = options.strict !== false
132
this.dot = !!options.dot
133
this.mark = !!options.mark
134
this.sync = !!options.sync
135
this.nounique = !!options.nounique
136
this.nonull = !!options.nonull
137
this.nosort = !!options.nosort
138
this.nocase = !!options.nocase
139
this.stat = !!options.stat
141
this.debug = !!options.debug || !!options.globDebug
143
this.log = console.error
145
this.silent = !!options.silent
147
var mm = this.minimatch = new Minimatch(pattern, options)
148
this.options = mm.options
149
pattern = this.pattern = mm.pattern
154
// list of all the patterns that ** has resolved do, so
155
// we can avoid visiting multiple times.
160
// process each pattern in the minimatch set
161
var n = this.minimatch.set.length
163
// The matches are stored as {<filename>: true,...} so that
164
// duplicates are automagically pruned.
165
// Later, we do an Object.keys() on these.
166
// Keep them as a list so we can fill in when nonull is set.
167
this.matches = new Array(n)
169
this.minimatch.set.forEach(iterator.bind(this))
170
function iterator (pattern, i, set) {
171
this._process(pattern, 0, i, function (er) {
172
if (er) this.emit("error", er)
173
if (-- n <= 0) this._finish()
178
Glob.prototype.log = function () {}
180
Glob.prototype._finish = function () {
181
assert(this instanceof Glob)
183
var nou = this.nounique
184
, all = nou ? [] : {}
186
for (var i = 0, l = this.matches.length; i < l; i ++) {
187
var matches = this.matches[i]
188
this.log("matches[%d] =", i, matches)
189
// do like the shell, and spit out the literal glob
192
var literal = this.minimatch.globSet[i]
193
if (nou) all.push(literal)
194
else all[literal] = true
198
var m = Object.keys(matches)
199
if (nou) all.push.apply(all, m)
200
else m.forEach(function (m) {
206
if (!nou) all = Object.keys(all)
209
all = all.sort(this.nocase ? alphasorti : alphasort)
213
// at *some* point we statted all of these
214
all = all.map(function (m) {
215
var sc = this.cache[m]
218
var isDir = (Array.isArray(sc) || sc === 2)
219
if (isDir && m.slice(-1) !== "/") {
222
if (!isDir && m.slice(-1) === "/") {
223
return m.replace(/\/+$/, "")
229
this.log("emitting end", all)
231
this.EOF = this.found = all
232
this.emitMatch(this.EOF)
235
function alphasorti (a, b) {
238
return alphasort(a, b)
241
function alphasort (a, b) {
242
return a > b ? 1 : a < b ? -1 : 0
245
Glob.prototype.abort = function () {
250
Glob.prototype.pause = function () {
251
if (this.paused) return
253
this.emit("error", new Error("Can't pause/resume sync glob"))
258
Glob.prototype.resume = function () {
259
if (!this.paused) return
261
this.emit("error", new Error("Can't pause/resume sync glob"))
264
this._processEmitQueue()
265
//process.nextTick(this.emit.bind(this, "resume"))
268
Glob.prototype.emitMatch = function (m) {
269
if (!this.stat || this.statCache[m] || m === this.EOF) {
270
this._emitQueue.push(m)
271
this._processEmitQueue()
273
this._stat(m, function(exists, isDir) {
275
this._emitQueue.push(m)
276
this._processEmitQueue()
282
Glob.prototype._processEmitQueue = function (m) {
283
while (!this._processingEmitQueue &&
285
this._processingEmitQueue = true
286
var m = this._emitQueue.shift()
288
this._processingEmitQueue = false
292
this.log('emit!', m === this.EOF ? "end" : "match")
294
this.emit(m === this.EOF ? "end" : "match", m)
295
this._processingEmitQueue = false
299
Glob.prototype._process = function (pattern, depth, index, cb_) {
300
assert(this instanceof Glob)
302
var cb = function cb (er, res) {
303
assert(this instanceof Glob)
305
if (!this._processQueue) {
306
this._processQueue = []
307
this.once("resume", function () {
308
var q = this._processQueue
309
this._processQueue = null
310
q.forEach(function (cb) { cb() })
313
this._processQueue.push(cb_.bind(this, er, res))
315
cb_.call(this, er, res)
319
if (this.aborted) return cb()
321
if (depth > this.maxDepth) return cb()
323
// Get the first [n] parts of pattern that are all strings.
325
while (typeof pattern[n] === "string") {
328
// now n is the index of the first one that is *not* a string.
330
// see if there's anything else
333
// if not, then this is rather simple
335
prefix = pattern.join("/")
336
this._stat(prefix, function (exists, isDir) {
337
// either it's there, or it isn't.
338
// nothing more to do, either way.
340
if (prefix && isAbsolute(prefix) && !this.nomount) {
341
if (prefix.charAt(0) === "/") {
342
prefix = path.join(this.root, prefix)
344
prefix = path.resolve(this.root, prefix)
348
if (process.platform === "win32")
349
prefix = prefix.replace(/\\/g, "/")
351
this.matches[index] = this.matches[index] || {}
352
this.matches[index][prefix] = true
353
this.emitMatch(prefix)
360
// pattern *starts* with some non-trivial item.
361
// going to readdir(cwd), but not include the prefix in matches.
366
// pattern has some string bits in the front.
367
// whatever it starts with, whether that's "absolute" like /foo/bar,
368
// or "relative" like "../baz"
369
prefix = pattern.slice(0, n)
370
prefix = prefix.join("/")
374
// get the list of entries.
376
if (prefix === null) read = "."
377
else if (isAbsolute(prefix) || isAbsolute(pattern.join("/"))) {
378
if (!prefix || !isAbsolute(prefix)) {
379
prefix = path.join("/", prefix)
381
read = prefix = path.resolve(prefix)
383
// if (process.platform === "win32")
384
// read = prefix = prefix.replace(/^[a-zA-Z]:|\\/g, "/")
386
this.log('absolute: ', prefix, this.root, pattern, read)
391
this.log('readdir(%j)', read, this.cwd, this.root)
393
return this._readdir(read, function (er, entries) {
396
// this means that, whatever else comes after this, it can never match
400
// globstar is special
401
if (pattern[n] === minimatch.GLOBSTAR) {
402
// test without the globstar, and with every child both below
403
// and replacing the globstar.
404
var s = [ pattern.slice(0, n).concat(pattern.slice(n + 1)) ]
405
entries.forEach(function (e) {
406
if (e.charAt(0) === "." && !this.dot) return
407
// instead of the globstar
408
s.push(pattern.slice(0, n).concat(e).concat(pattern.slice(n + 1)))
409
// below the globstar
410
s.push(pattern.slice(0, n).concat(e).concat(pattern.slice(n)))
413
s = s.filter(function (pattern) {
414
var key = gsKey(pattern)
415
var seen = !this._globstars[key]
416
this._globstars[key] = true
423
// now asyncForEach over this
426
s.forEach(function (gsPattern) {
427
this._process(gsPattern, depth + 1, index, function (er) {
429
if (er) return cb(errState = er)
430
if (--l <= 0) return cb()
438
// It will only match dot entries if it starts with a dot, or if
439
// dot is set. Stuff like @(.foo|.bar) isn't allowed.
441
var rawGlob = pattern[n]._glob
442
, dotOk = this.dot || rawGlob.charAt(0) === "."
444
entries = entries.filter(function (e) {
445
return (e.charAt(0) !== "." || dotOk) &&
449
// If n === pattern.length - 1, then there's no need for the extra stat
450
// *unless* the user has specified "mark" or "stat" explicitly.
451
// We know that they exist, since the readdir returned them.
452
if (n === pattern.length - 1 &&
455
entries.forEach(function (e) {
457
if (prefix !== "/") e = prefix + "/" + e
460
if (e.charAt(0) === "/" && !this.nomount) {
461
e = path.join(this.root, e)
464
if (process.platform === "win32")
465
e = e.replace(/\\/g, "/")
467
this.matches[index] = this.matches[index] || {}
468
this.matches[index][e] = true
475
// now test all the remaining entries as stand-ins for that part
477
var l = entries.length
479
if (l === 0) return cb() // no matches possible
480
entries.forEach(function (e) {
481
var p = pattern.slice(0, n).concat(e).concat(pattern.slice(n + 1))
482
this._process(p, depth + 1, index, function (er) {
484
if (er) return cb(errState = er)
485
if (--l === 0) return cb.call(this)
492
function gsKey (pattern) {
493
return '**' + pattern.map(function (p) {
494
return (p === minimatch.GLOBSTAR) ? '**' : (''+p)
498
Glob.prototype._stat = function (f, cb) {
499
assert(this instanceof Glob)
501
if (f.charAt(0) === "/") {
502
abs = path.join(this.root, f)
503
} else if (this.changedCwd) {
504
abs = path.resolve(this.cwd, f)
507
if (f.length > this.maxLength) {
508
var er = new Error("Path name too long")
509
er.code = "ENAMETOOLONG"
511
return this._afterStat(f, abs, cb, er)
514
this.log('stat', [this.cwd, f, '=', abs])
516
if (!this.stat && this.cache.hasOwnProperty(f)) {
517
var exists = this.cache[f]
518
, isDir = exists && (Array.isArray(exists) || exists === 2)
519
if (this.sync) return cb.call(this, !!exists, isDir)
520
return process.nextTick(cb.bind(this, !!exists, isDir))
523
var stat = this.statCache[abs]
524
if (this.sync || stat) {
527
stat = fs.statSync(abs)
531
this._afterStat(f, abs, cb, er, stat)
533
fs.stat(abs, this._afterStat.bind(this, f, abs, cb))
537
Glob.prototype._afterStat = function (f, abs, cb, er, stat) {
539
assert(this instanceof Glob)
541
if (abs.slice(-1) === "/" && stat && !stat.isDirectory()) {
542
this.log("should be ENOTDIR, fake it")
544
er = new Error("ENOTDIR, not a directory '" + abs + "'")
550
var emit = !this.statCache[abs]
551
this.statCache[abs] = stat
556
exists = stat.isDirectory() ? 2 : 1
558
this.emit('stat', f, stat)
560
this.cache[f] = this.cache[f] || exists
561
cb.call(this, !!exists, exists === 2)
564
Glob.prototype._readdir = function (f, cb) {
565
assert(this instanceof Glob)
567
if (f.charAt(0) === "/") {
568
abs = path.join(this.root, f)
569
} else if (isAbsolute(f)) {
571
} else if (this.changedCwd) {
572
abs = path.resolve(this.cwd, f)
575
if (f.length > this.maxLength) {
576
var er = new Error("Path name too long")
577
er.code = "ENAMETOOLONG"
579
return this._afterReaddir(f, abs, cb, er)
582
this.log('readdir', [this.cwd, f, abs])
583
if (this.cache.hasOwnProperty(f)) {
584
var c = this.cache[f]
585
if (Array.isArray(c)) {
586
if (this.sync) return cb.call(this, null, c)
587
return process.nextTick(cb.bind(this, null, c))
591
// either ENOENT or ENOTDIR
592
var code = c ? "ENOTDIR" : "ENOENT"
593
, er = new Error((c ? "Not a directory" : "Not found") + ": " + f)
597
if (this.sync) return cb.call(this, er)
598
return process.nextTick(cb.bind(this, er))
601
// at this point, c === 2, meaning it's a dir, but we haven't
602
// had to read it yet, or c === true, meaning it's *something*
603
// but we don't have any idea what. Need to read it, either way.
609
entries = fs.readdirSync(abs)
613
return this._afterReaddir(f, abs, cb, er, entries)
616
fs.readdir(abs, this._afterReaddir.bind(this, f, abs, cb))
619
Glob.prototype._afterReaddir = function (f, abs, cb, er, entries) {
620
assert(this instanceof Glob)
621
if (entries && !er) {
622
this.cache[f] = entries
623
// if we haven't asked to stat everything for suresies, then just
624
// assume that everything in there exists, so we can avoid
625
// having to stat it a second time. This also gets us one step
626
// further into ELOOP territory.
627
if (!this.mark && !this.stat) {
628
entries.forEach(function (e) {
629
if (f === "/") e = f + e
635
return cb.call(this, er, entries)
638
// now handle errors, and cache the information
639
if (er) switch (er.code) {
640
case "ENOTDIR": // totally normal. means it *does* exist.
642
return cb.call(this, er)
643
case "ENOENT": // not terribly unusual
647
this.cache[f] = false
648
return cb.call(this, er)
649
default: // some unusual error. Treat as failure.
650
this.cache[f] = false
651
if (this.strict) this.emit("error", er)
652
if (!this.silent) console.error("glob error", er)
653
return cb.call(this, er)
657
var isAbsolute = process.platform === "win32" ? absWin : absUnix
659
function absWin (p) {
660
if (absUnix(p)) return true
661
// pull off the device/UNC bit from a windows path.
662
// from node's lib/path.js
664
/^([a-zA-Z]:|[\\\/]{2}[^\\\/]+[\\\/]+[^\\\/]+)?([\\\/])?([\s\S]*?)$/
665
, result = splitDeviceRe.exec(p)
666
, device = result[1] || ''
667
, isUnc = device && device.charAt(1) !== ':'
668
, isAbsolute = !!result[2] || isUnc // UNC paths are always absolute
673
function absUnix (p) {
674
return p.charAt(0) === "/" || p === ""