Created
July 9, 2015 14:51
-
-
Save etiktin/ff222bb73904189a1aa7 to your computer and use it in GitHub Desktop.
Merging the changes made to glob/common.js by @staticshock to glob@4.5.3 (this is the glob version that is used by gulp@3.9.0). The changes fix the support for "nocase" on Windows and add basic support for UNC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
exports.isIgnored = isIgnored | |
exports.childrenIgnored = childrenIgnored | |
function ownProp (obj, field) { | |
return Object.prototype.hasOwnProperty.call(obj, field) | |
} | |
var path = require("path") | |
var minimatch = require("minimatch") | |
var Minimatch = minimatch.Minimatch | |
function WinPath (p) { | |
if (!(this instanceof WinPath)) | |
return new WinPath(p) | |
// pull off the device/UNC bit from a windows path. | |
// from node's lib/path.js | |
var splitDeviceRe = | |
/^([a-zA-Z]:|[\\\/]{2}[^\\\/]+[\\\/]+[^\\\/]+)?([\\\/])?([\s\S]*?)$/ | |
var result = splitDeviceRe.exec(p) | |
this.device = result[1] || '' | |
this.sep = result[2] || '' | |
this.tail = result[3] || '' | |
this.isUnc = !!this.device && this.device.charAt(1) !== ':' | |
this.isAbsolute = !!this.sep || this.isUnc // UNC paths are always absolute | |
} | |
function absWin (p) { | |
if (absUnix(p)) return true | |
var winPath = new WinPath(p) | |
return winPath.isAbsolute | |
} | |
function absUnix (p) { | |
return p.charAt(0) === "/" || p === "" | |
} | |
function alphasorti (a, b) { | |
return a.toLowerCase().localeCompare(b.toLowerCase()) | |
} | |
function alphasort (a, b) { | |
return a.localeCompare(b) | |
} | |
function setupIgnores (self, options) { | |
self.ignore = options.ignore || [] | |
if (!Array.isArray(self.ignore)) | |
self.ignore = [self.ignore] | |
if (self.ignore.length) { | |
self.ignore = self.ignore.map(ignoreMap) | |
} | |
} | |
function ignoreMap (pattern) { | |
var gmatcher = null | |
if (pattern.slice(-3) === '/**') { | |
var gpattern = pattern.replace(/(\/\*\*)+$/, '') | |
gmatcher = new Minimatch(gpattern, { nonegate: true }) | |
} | |
return { | |
matcher: new Minimatch(pattern, { nonegate: true }), | |
gmatcher: gmatcher | |
} | |
} | |
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.realpath = !!options.realpath | |
self.realpathCache = options.realpathCache || Object.create(null) | |
self.follow = !!options.follow | |
self.dot = !!options.dot | |
self.mark = !!options.mark | |
self.nodir = !!options.nodir | |
if (self.nodir) | |
self.mark = true | |
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.noprocess = !!options.noprocess | |
self.maxLength = options.maxLength || Infinity | |
self.cache = options.cache || Object.create(null) | |
self.statCache = options.statCache || Object.create(null) | |
self.symlinks = options.symlinks || Object.create(null) | |
setupIgnores(self, options) | |
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 | |
} | |
if (process.platform === "win32") { | |
var winPath = new WinPath(pattern) | |
if (winPath.isAbsolute) { | |
options.root = winPath.device | |
pattern = winPath.sep + winPath.tail | |
} | |
} | |
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 || Object.keys(matches).length === 0) { | |
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) { | |
for (var i = 0; i < all.length; i++) { | |
all[i] = self._mark(all[i]) | |
} | |
if (self.nodir) { | |
all = all.filter(function (e) { | |
return !(/\/$/.test(e)) | |
}) | |
} | |
} | |
if (self.ignore.length) | |
all = all.filter(function(m) { | |
return !isIgnored(self, m) | |
}) | |
self.found = all | |
} | |
function mark (self, p) { | |
var abs = makeAbs(self, p) | |
var c = self.cache[abs] | |
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) { | |
var mabs = makeAbs(self, m) | |
self.statCache[mabs] = self.statCache[abs] | |
self.cache[mabs] = self.cache[abs] | |
} | |
} | |
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) | |
} else if (self.realpath) { | |
abs = path.resolve(f) | |
} | |
return abs | |
} | |
// Return true, if pattern ends with globstar '**', for the accompanying parent directory. | |
// Ex:- If node_modules/** is the pattern, add 'node_modules' to ignore list along with it's contents | |
function isIgnored (self, path) { | |
if (!self.ignore.length) | |
return false | |
return self.ignore.some(function(item) { | |
return item.matcher.match(path) || !!(item.gmatcher && item.gmatcher.match(path)) | |
}) | |
} | |
function childrenIgnored (self, path) { | |
if (!self.ignore.length) | |
return false | |
return self.ignore.some(function(item) { | |
return !!(item.gmatcher && item.gmatcher.match(path)) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment