123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484 |
- module.exports = globSync
- globSync.GlobSync = GlobSync
- var fs = require('fs')
- var rp = require('fs.realpath')
- var minimatch = require('minimatch')
- var Minimatch = minimatch.Minimatch
- var Glob = require('./glob.js').Glob
- var util = require('util')
- var path = require('path')
- var assert = require('assert')
- var isAbsolute = require('path-is-absolute')
- var common = require('./common.js')
- var setopts = common.setopts
- var ownProp = common.ownProp
- var childrenIgnored = common.childrenIgnored
- var isIgnored = common.isIgnored
- function globSync (pattern, options) {
- if (typeof options === 'function' || arguments.length === 3)
- throw new TypeError('callback provided to sync glob\n'+
- 'See: https://github.com/isaacs/node-glob/issues/167')
- return new GlobSync(pattern, options).found
- }
- function GlobSync (pattern, options) {
- if (!pattern)
- throw new Error('must provide pattern')
- if (typeof options === 'function' || arguments.length === 3)
- throw new TypeError('callback provided to sync glob\n'+
- 'See: https://github.com/isaacs/node-glob/issues/167')
- if (!(this instanceof GlobSync))
- return new GlobSync(pattern, options)
- setopts(this, pattern, options)
- if (this.noprocess)
- return this
- var n = this.minimatch.set.length
- this.matches = new Array(n)
- for (var i = 0; i < n; i ++) {
- this._process(this.minimatch.set[i], i, false)
- }
- this._finish()
- }
- GlobSync.prototype._finish = function () {
- assert(this instanceof GlobSync)
- if (this.realpath) {
- var self = this
- this.matches.forEach(function (matchset, index) {
- var set = self.matches[index] = Object.create(null)
- for (var p in matchset) {
- try {
- p = self._makeAbs(p)
- var real = rp.realpathSync(p, self.realpathCache)
- set[real] = true
- } catch (er) {
- if (er.syscall === 'stat')
- set[self._makeAbs(p)] = true
- else
- throw er
- }
- }
- })
- }
- common.finish(this)
- }
- GlobSync.prototype._process = function (pattern, index, inGlobStar) {
- assert(this instanceof GlobSync)
-
- var n = 0
- while (typeof pattern[n] === 'string') {
- n ++
- }
-
-
- var prefix
- switch (n) {
-
- case pattern.length:
- this._processSimple(pattern.join('/'), index)
- return
- case 0:
-
-
- prefix = null
- break
- default:
-
-
-
- prefix = pattern.slice(0, n).join('/')
- break
- }
- var remain = pattern.slice(n)
-
- var read
- if (prefix === null)
- read = '.'
- else if (isAbsolute(prefix) || isAbsolute(pattern.join('/'))) {
- if (!prefix || !isAbsolute(prefix))
- prefix = '/' + prefix
- read = prefix
- } else
- read = prefix
- var abs = this._makeAbs(read)
-
- if (childrenIgnored(this, read))
- return
- var isGlobStar = remain[0] === minimatch.GLOBSTAR
- if (isGlobStar)
- this._processGlobStar(prefix, read, abs, remain, index, inGlobStar)
- else
- this._processReaddir(prefix, read, abs, remain, index, inGlobStar)
- }
- GlobSync.prototype._processReaddir = function (prefix, read, abs, remain, index, inGlobStar) {
- var entries = this._readdir(abs, inGlobStar)
-
- if (!entries)
- return
-
-
- var pn = remain[0]
- var negate = !!this.minimatch.negate
- var rawGlob = pn._glob
- var dotOk = this.dot || rawGlob.charAt(0) === '.'
- var matchedEntries = []
- for (var i = 0; i < entries.length; i++) {
- var e = entries[i]
- if (e.charAt(0) !== '.' || dotOk) {
- var m
- if (negate && !prefix) {
- m = !e.match(pn)
- } else {
- m = e.match(pn)
- }
- if (m)
- matchedEntries.push(e)
- }
- }
- var len = matchedEntries.length
-
- if (len === 0)
- return
-
-
-
-
- if (remain.length === 1 && !this.mark && !this.stat) {
- if (!this.matches[index])
- this.matches[index] = Object.create(null)
- for (var i = 0; i < len; i ++) {
- var e = matchedEntries[i]
- if (prefix) {
- if (prefix.slice(-1) !== '/')
- e = prefix + '/' + e
- else
- e = prefix + e
- }
- if (e.charAt(0) === '/' && !this.nomount) {
- e = path.join(this.root, e)
- }
- this._emitMatch(index, e)
- }
-
- return
- }
-
-
- remain.shift()
- for (var i = 0; i < len; i ++) {
- var e = matchedEntries[i]
- var newPattern
- if (prefix)
- newPattern = [prefix, e]
- else
- newPattern = [e]
- this._process(newPattern.concat(remain), index, inGlobStar)
- }
- }
- GlobSync.prototype._emitMatch = function (index, e) {
- if (isIgnored(this, e))
- return
- var abs = this._makeAbs(e)
- if (this.mark)
- e = this._mark(e)
- if (this.absolute) {
- e = abs
- }
- if (this.matches[index][e])
- return
- if (this.nodir) {
- var c = this.cache[abs]
- if (c === 'DIR' || Array.isArray(c))
- return
- }
- this.matches[index][e] = true
- if (this.stat)
- this._stat(e)
- }
- GlobSync.prototype._readdirInGlobStar = function (abs) {
-
-
- if (this.follow)
- return this._readdir(abs, false)
- var entries
- var lstat
- var stat
- try {
- lstat = fs.lstatSync(abs)
- } catch (er) {
- if (er.code === 'ENOENT') {
-
- return null
- }
- }
- var isSym = lstat && lstat.isSymbolicLink()
- this.symlinks[abs] = isSym
-
-
- if (!isSym && lstat && !lstat.isDirectory())
- this.cache[abs] = 'FILE'
- else
- entries = this._readdir(abs, false)
- return entries
- }
- GlobSync.prototype._readdir = function (abs, inGlobStar) {
- var entries
- if (inGlobStar && !ownProp(this.symlinks, abs))
- return this._readdirInGlobStar(abs)
- if (ownProp(this.cache, abs)) {
- var c = this.cache[abs]
- if (!c || c === 'FILE')
- return null
- if (Array.isArray(c))
- return c
- }
- try {
- return this._readdirEntries(abs, fs.readdirSync(abs))
- } catch (er) {
- this._readdirError(abs, er)
- return null
- }
- }
- GlobSync.prototype._readdirEntries = function (abs, entries) {
-
-
-
- if (!this.mark && !this.stat) {
- for (var i = 0; i < entries.length; i ++) {
- var e = entries[i]
- if (abs === '/')
- e = abs + e
- else
- e = abs + '/' + e
- this.cache[e] = true
- }
- }
- this.cache[abs] = entries
-
- return entries
- }
- GlobSync.prototype._readdirError = function (f, er) {
-
- switch (er.code) {
- case 'ENOTSUP':
- case 'ENOTDIR':
- var abs = this._makeAbs(f)
- this.cache[abs] = 'FILE'
- if (abs === this.cwdAbs) {
- var error = new Error(er.code + ' invalid cwd ' + this.cwd)
- error.path = this.cwd
- error.code = er.code
- throw error
- }
- break
- case 'ENOENT':
- case 'ELOOP':
- case 'ENAMETOOLONG':
- case 'UNKNOWN':
- this.cache[this._makeAbs(f)] = false
- break
- default:
- this.cache[this._makeAbs(f)] = false
- if (this.strict)
- throw er
- if (!this.silent)
- console.error('glob error', er)
- break
- }
- }
- GlobSync.prototype._processGlobStar = function (prefix, read, abs, remain, index, inGlobStar) {
- var entries = this._readdir(abs, inGlobStar)
-
-
- if (!entries)
- return
-
-
- var remainWithoutGlobStar = remain.slice(1)
- var gspref = prefix ? [ prefix ] : []
- var noGlobStar = gspref.concat(remainWithoutGlobStar)
-
- this._process(noGlobStar, index, false)
- var len = entries.length
- var isSym = this.symlinks[abs]
-
- if (isSym && inGlobStar)
- return
- for (var i = 0; i < len; i++) {
- var e = entries[i]
- if (e.charAt(0) === '.' && !this.dot)
- continue
-
- var instead = gspref.concat(entries[i], remainWithoutGlobStar)
- this._process(instead, index, true)
- var below = gspref.concat(entries[i], remain)
- this._process(below, index, true)
- }
- }
- GlobSync.prototype._processSimple = function (prefix, index) {
-
-
- var exists = this._stat(prefix)
- if (!this.matches[index])
- this.matches[index] = Object.create(null)
-
- if (!exists)
- return
- if (prefix && isAbsolute(prefix) && !this.nomount) {
- var trail = /[\/\\]$/.test(prefix)
- if (prefix.charAt(0) === '/') {
- prefix = path.join(this.root, prefix)
- } else {
- prefix = path.resolve(this.root, prefix)
- if (trail)
- prefix += '/'
- }
- }
- if (process.platform === 'win32')
- prefix = prefix.replace(/\\/g, '/')
-
- this._emitMatch(index, prefix)
- }
- GlobSync.prototype._stat = function (f) {
- var abs = this._makeAbs(f)
- var needDir = f.slice(-1) === '/'
- if (f.length > this.maxLength)
- return false
- if (!this.stat && ownProp(this.cache, abs)) {
- var c = this.cache[abs]
- if (Array.isArray(c))
- c = 'DIR'
-
- if (!needDir || c === 'DIR')
- return c
- if (needDir && c === 'FILE')
- return false
-
-
- }
- var exists
- var stat = this.statCache[abs]
- if (!stat) {
- var lstat
- try {
- lstat = fs.lstatSync(abs)
- } catch (er) {
- if (er && (er.code === 'ENOENT' || er.code === 'ENOTDIR')) {
- this.statCache[abs] = false
- return false
- }
- }
- if (lstat && lstat.isSymbolicLink()) {
- try {
- stat = fs.statSync(abs)
- } catch (er) {
- stat = lstat
- }
- } else {
- stat = lstat
- }
- }
- this.statCache[abs] = stat
- var c = true
- if (stat)
- c = stat.isDirectory() ? 'DIR' : 'FILE'
- this.cache[abs] = this.cache[abs] || c
- if (needDir && c === 'FILE')
- return false
- return c
- }
- GlobSync.prototype._mark = function (p) {
- return common.mark(this, p)
- }
- GlobSync.prototype._makeAbs = function (f) {
- return common.makeAbs(this, f)
- }
|