react / react-0.13.3 / examples / basic-commonjs / node_modules / browserify / node_modules / glob / sync.js
80708 viewsmodule.exports = globSync1globSync.GlobSync = GlobSync23var fs = require('fs')4var minimatch = require('minimatch')5var Minimatch = minimatch.Minimatch6var Glob = require('./glob.js').Glob7var util = require('util')8var path = require('path')9var assert = require('assert')10var common = require('./common.js')11var alphasort = common.alphasort12var alphasorti = common.alphasorti13var isAbsolute = common.isAbsolute14var setopts = common.setopts15var ownProp = common.ownProp16var childrenIgnored = common.childrenIgnored1718function globSync (pattern, options) {19if (typeof options === 'function' || arguments.length === 3)20throw new TypeError('callback provided to sync glob\n'+21'See: https://github.com/isaacs/node-glob/issues/167')2223return new GlobSync(pattern, options).found24}2526function GlobSync (pattern, options) {27if (!pattern)28throw new Error('must provide pattern')2930if (typeof options === 'function' || arguments.length === 3)31throw new TypeError('callback provided to sync glob\n'+32'See: https://github.com/isaacs/node-glob/issues/167')3334if (!(this instanceof GlobSync))35return new GlobSync(pattern, options)3637setopts(this, pattern, options)3839if (this.noprocess)40return this4142var n = this.minimatch.set.length43this.matches = new Array(n)44for (var i = 0; i < n; i ++) {45this._process(this.minimatch.set[i], i, false)46}47this._finish()48}4950GlobSync.prototype._finish = function () {51assert(this instanceof GlobSync)52if (this.realpath) {53var self = this54this.matches.forEach(function (matchset, index) {55var set = self.matches[index] = Object.create(null)56for (var p in matchset) {57try {58p = self._makeAbs(p)59var real = fs.realpathSync(p, this.realpathCache)60set[real] = true61} catch (er) {62if (er.syscall === 'stat')63set[self._makeAbs(p)] = true64else65throw er66}67}68})69}70common.finish(this)71}727374GlobSync.prototype._process = function (pattern, index, inGlobStar) {75assert(this instanceof GlobSync)7677// Get the first [n] parts of pattern that are all strings.78var n = 079while (typeof pattern[n] === 'string') {80n ++81}82// now n is the index of the first one that is *not* a string.8384// See if there's anything else85var prefix86switch (n) {87// if not, then this is rather simple88case pattern.length:89this._processSimple(pattern.join('/'), index)90return9192case 0:93// pattern *starts* with some non-trivial item.94// going to readdir(cwd), but not include the prefix in matches.95prefix = null96break9798default: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'102prefix = pattern.slice(0, n).join('/')103break104}105106var remain = pattern.slice(n)107108// get the list of entries.109var read110if (prefix === null)111read = '.'112else if (isAbsolute(prefix) || isAbsolute(pattern.join('/'))) {113if (!prefix || !isAbsolute(prefix))114prefix = '/' + prefix115read = prefix116} else117read = prefix118119var abs = this._makeAbs(read)120121//if ignored, skip processing122if (childrenIgnored(this, read))123return124125var isGlobStar = remain[0] === minimatch.GLOBSTAR126if (isGlobStar)127this._processGlobStar(prefix, read, abs, remain, index, inGlobStar)128else129this._processReaddir(prefix, read, abs, remain, index, inGlobStar)130}131132133GlobSync.prototype._processReaddir = function (prefix, read, abs, remain, index, inGlobStar) {134var entries = this._readdir(abs, inGlobStar)135136// if the abs isn't a dir, then nothing can match!137if (!entries)138return139140// It will only match dot entries if it starts with a dot, or if141// dot is set. Stuff like @(.foo|.bar) isn't allowed.142var pn = remain[0]143var negate = !!this.minimatch.negate144var rawGlob = pn._glob145var dotOk = this.dot || rawGlob.charAt(0) === '.'146147var matchedEntries = []148for (var i = 0; i < entries.length; i++) {149var e = entries[i]150if (e.charAt(0) !== '.' || dotOk) {151var m152if (negate && !prefix) {153m = !e.match(pn)154} else {155m = e.match(pn)156}157if (m)158matchedEntries.push(e)159}160}161162var len = matchedEntries.length163// If there are no matched entries, then nothing matches.164if (len === 0)165return166167// if this is the last remaining pattern bit, then no need for168// an additional stat *unless* the user has specified mark or169// stat explicitly. We know they exist, since readdir returned170// them.171172if (remain.length === 1 && !this.mark && !this.stat) {173if (!this.matches[index])174this.matches[index] = Object.create(null)175176for (var i = 0; i < len; i ++) {177var e = matchedEntries[i]178if (prefix) {179if (prefix.slice(-1) !== '/')180e = prefix + '/' + e181else182e = prefix + e183}184185if (e.charAt(0) === '/' && !this.nomount) {186e = path.join(this.root, e)187}188this.matches[index][e] = true189}190// This was the last one, and no stats were needed191return192}193194// now test all matched entries as stand-ins for that part195// of the pattern.196remain.shift()197for (var i = 0; i < len; i ++) {198var e = matchedEntries[i]199var newPattern200if (prefix)201newPattern = [prefix, e]202else203newPattern = [e]204this._process(newPattern.concat(remain), index, inGlobStar)205}206}207208209GlobSync.prototype._emitMatch = function (index, e) {210var abs = this._makeAbs(e)211if (this.mark)212e = this._mark(e)213214if (this.matches[index][e])215return216217if (this.nodir) {218var c = this.cache[this._makeAbs(e)]219if (c === 'DIR' || Array.isArray(c))220return221}222223this.matches[index][e] = true224if (this.stat)225this._stat(e)226}227228229GlobSync.prototype._readdirInGlobStar = function (abs) {230// follow all symlinked directories forever231// just proceed as if this is a non-globstar situation232if (this.follow)233return this._readdir(abs, false)234235var entries236var lstat237var stat238try {239lstat = fs.lstatSync(abs)240} catch (er) {241// lstat failed, doesn't exist242return null243}244245var isSym = lstat.isSymbolicLink()246this.symlinks[abs] = isSym247248// If it's not a symlink or a dir, then it's definitely a regular file.249// don't bother doing a readdir in that case.250if (!isSym && !lstat.isDirectory())251this.cache[abs] = 'FILE'252else253entries = this._readdir(abs, false)254255return entries256}257258GlobSync.prototype._readdir = function (abs, inGlobStar) {259var entries260261if (inGlobStar && !ownProp(this.symlinks, abs))262return this._readdirInGlobStar(abs)263264if (ownProp(this.cache, abs)) {265var c = this.cache[abs]266if (!c || c === 'FILE')267return null268269if (Array.isArray(c))270return c271}272273try {274return this._readdirEntries(abs, fs.readdirSync(abs))275} catch (er) {276this._readdirError(abs, er)277return null278}279}280281GlobSync.prototype._readdirEntries = function (abs, entries) {282// if we haven't asked to stat everything, then just283// assume that everything in there exists, so we can avoid284// having to stat it a second time.285if (!this.mark && !this.stat) {286for (var i = 0; i < entries.length; i ++) {287var e = entries[i]288if (abs === '/')289e = abs + e290else291e = abs + '/' + e292this.cache[e] = true293}294}295296this.cache[abs] = entries297298// mark and cache dir-ness299return entries300}301302GlobSync.prototype._readdirError = function (f, er) {303// handle errors, and cache the information304switch (er.code) {305case 'ENOTDIR': // totally normal. means it *does* exist.306this.cache[this._makeAbs(f)] = 'FILE'307break308309case 'ENOENT': // not terribly unusual310case 'ELOOP':311case 'ENAMETOOLONG':312case 'UNKNOWN':313this.cache[this._makeAbs(f)] = false314break315316default: // some unusual error. Treat as failure.317this.cache[this._makeAbs(f)] = false318if (this.strict) throw er319if (!this.silent) console.error('glob error', er)320break321}322}323324GlobSync.prototype._processGlobStar = function (prefix, read, abs, remain, index, inGlobStar) {325326var entries = this._readdir(abs, inGlobStar)327328// no entries means not a dir, so it can never have matches329// foo.txt/** doesn't match foo.txt330if (!entries)331return332333// test without the globstar, and with every child both below334// and replacing the globstar.335var remainWithoutGlobStar = remain.slice(1)336var gspref = prefix ? [ prefix ] : []337var noGlobStar = gspref.concat(remainWithoutGlobStar)338339// the noGlobStar pattern exits the inGlobStar state340this._process(noGlobStar, index, false)341342var len = entries.length343var isSym = this.symlinks[abs]344345// If it's a symlink, and we're in a globstar, then stop346if (isSym && inGlobStar)347return348349for (var i = 0; i < len; i++) {350var e = entries[i]351if (e.charAt(0) === '.' && !this.dot)352continue353354// these two cases enter the inGlobStar state355var instead = gspref.concat(entries[i], remainWithoutGlobStar)356this._process(instead, index, true)357358var below = gspref.concat(entries[i], remain)359this._process(below, index, true)360}361}362363GlobSync.prototype._processSimple = function (prefix, index) {364// XXX review this. Shouldn't it be doing the mounting etc365// before doing stat? kinda weird?366var exists = this._stat(prefix)367368if (!this.matches[index])369this.matches[index] = Object.create(null)370371// If it doesn't exist, then just mark the lack of results372if (!exists)373return374375if (prefix && isAbsolute(prefix) && !this.nomount) {376var trail = /[\/\\]$/.test(prefix)377if (prefix.charAt(0) === '/') {378prefix = path.join(this.root, prefix)379} else {380prefix = path.resolve(this.root, prefix)381if (trail)382prefix += '/'383}384}385386if (process.platform === 'win32')387prefix = prefix.replace(/\\/g, '/')388389// Mark this as a match390this.matches[index][prefix] = true391}392393// Returns either 'DIR', 'FILE', or false394GlobSync.prototype._stat = function (f) {395var abs = this._makeAbs(f)396var needDir = f.slice(-1) === '/'397398if (f.length > this.maxLength)399return false400401if (!this.stat && ownProp(this.cache, abs)) {402var c = this.cache[abs]403404if (Array.isArray(c))405c = 'DIR'406407// It exists, but maybe not how we need it408if (!needDir || c === 'DIR')409return c410411if (needDir && c === 'FILE')412return false413414// otherwise we have to stat, because maybe c=true415// if we know it exists, but not what it is.416}417418var exists419var stat = this.statCache[abs]420if (!stat) {421var lstat422try {423lstat = fs.lstatSync(abs)424} catch (er) {425return false426}427428if (lstat.isSymbolicLink()) {429try {430stat = fs.statSync(abs)431} catch (er) {432stat = lstat433}434} else {435stat = lstat436}437}438439this.statCache[abs] = stat440441var c = stat.isDirectory() ? 'DIR' : 'FILE'442this.cache[abs] = this.cache[abs] || c443444if (needDir && c !== 'DIR')445return false446447return c448}449450GlobSync.prototype._mark = function (p) {451return common.mark(this, p)452}453454GlobSync.prototype._makeAbs = function (f) {455return common.makeAbs(this, f)456}457458459