Skip to content

Commit

Permalink
Refactor async glob, tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Nov 16, 2014
1 parent 335fe5e commit 5a40b67
Show file tree
Hide file tree
Showing 6 changed files with 498 additions and 684 deletions.
133 changes: 133 additions & 0 deletions common.js
@@ -1,6 +1,19 @@
exports.alphasort = alphasort
exports.alphasorti = alphasorti
exports.isAbsolute = process.platform === "win32" ? absWin : absUnix
exports.setopts = setopts
exports.ownProp = ownProp
exports.makeAbs = makeAbs
exports.finish = finish
exports.mark = mark

function ownProp (obj, field) {
return Object.prototype.hasOwnProperty.call(obj, field)
}

var path = require("path")
var minimatch = require("minimatch")
var Minimatch = minimatch.Minimatch

function absWin (p) {
if (absUnix(p)) return true
Expand Down Expand Up @@ -30,3 +43,123 @@ function alphasort (a, b) {
return a > b ? 1 : a < b ? -1 : 0
}


function setopts (self, pattern, options) {
if (!options)
options = {}

// base-matching: just use globstar for that.
if (options.matchBase && -1 === pattern.indexOf("/")) {
if (options.noglobstar) {
throw new Error("base matching requires globstar")
}
pattern = "**/" + pattern
}

self.pattern = pattern
self.strict = options.strict !== false
self.dot = !!options.dot
self.mark = !!options.mark
self.sync = !!options.sync
self.nounique = !!options.nounique
self.nonull = !!options.nonull
self.nosort = !!options.nosort
self.nocase = !!options.nocase
self.stat = !!options.stat
self.maxLength = options.maxLength || Infinity
self.cache = options.cache || Object.create(null)
self.statCache = options.statCache || Object.create(null)

self.changedCwd = false
var cwd = process.cwd()
if (!ownProp(options, "cwd"))
self.cwd = cwd
else {
self.cwd = options.cwd
self.changedCwd = path.resolve(options.cwd) !== cwd
}

self.root = options.root || path.resolve(self.cwd, "/")
self.root = path.resolve(self.root)
if (process.platform === "win32")
self.root = self.root.replace(/\\/g, "/")

self.nomount = !!options.nomount

self.minimatch = new Minimatch(pattern, options)
self.options = self.minimatch.options
}

function finish (self) {
var nou = self.nounique
var all = nou ? [] : Object.create(null)

for (var i = 0, l = self.matches.length; i < l; i ++) {
var matches = self.matches[i]
if (!matches) {
if (self.nonull) {
// do like the shell, and spit out the literal glob
var literal = self.minimatch.globSet[i]
if (nou)
all.push(literal)
else
all[literal] = true
}
} else {
// had matches
var m = Object.keys(matches)
if (nou)
all.push.apply(all, m)
else
m.forEach(function (m) {
all[m] = true
})
}
}

if (!nou)
all = Object.keys(all)

if (!self.nosort)
all = all.sort(self.nocase ? alphasorti : alphasort)

// at *some* point we statted all of these
if (self.mark)
all = all.map(self._mark, self)

self.found = all
}

function mark (self, p) {
var c = self.cache[p]
var m = p
if (c) {
var isDir = c === 'DIR' || Array.isArray(c)
var slash = p.slice(-1) === '/'

if (isDir && !slash)
m += '/'
else if (!isDir && slash)
m = m.slice(0, -1)

if (m !== p) {
self.statCache[m] = self.statCache[p]
self.cache[m] = self.cache[p]
}
}

return m
}

// lotta situps...
function makeAbs (self, f) {
var abs = f
if (f.charAt(0) === "/") {
abs = path.join(self.root, f)
} else if (exports.isAbsolute(f)) {
abs = f
} else if (self.changedCwd) {
abs = path.resolve(self.cwd, f)
}
return abs
}

0 comments on commit 5a40b67

Please sign in to comment.