diff options
author | Florian Dold <florian.dold@gmail.com> | 2016-11-03 01:33:53 +0100 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2016-11-03 01:33:53 +0100 |
commit | d1291f67551c58168af43698a359cb5ddfd266b0 (patch) | |
tree | 55a13ed29fe1915e3f42f1b1b7038dafa2e975a7 /node_modules/glob-stream/index.js | |
parent | d0a0695fb5d34996850723f7d4b1b59c3df909c2 (diff) |
node_modules
Diffstat (limited to 'node_modules/glob-stream/index.js')
-rw-r--r-- | node_modules/glob-stream/index.js | 206 |
1 files changed, 149 insertions, 57 deletions
diff --git a/node_modules/glob-stream/index.js b/node_modules/glob-stream/index.js index 0960c7c74..363a29dac 100644 --- a/node_modules/glob-stream/index.js +++ b/node_modules/glob-stream/index.js @@ -1,5 +1,3 @@ -/*jslint node: true */ - 'use strict'; var through2 = require('through2'); @@ -7,111 +5,205 @@ var Combine = require('ordered-read-streams'); var unique = require('unique-stream'); var glob = require('glob'); -var minimatch = require('minimatch'); -var glob2base = require('glob2base'); +var micromatch = require('micromatch'); +var resolveGlob = require('to-absolute-glob'); +var globParent = require('glob-parent'); var path = require('path'); +var extend = require('extend'); +var sepRe = (process.platform === 'win32' ? /[\/\\]/ : /\/+/); var gs = { - // creates a stream for a single glob or filter + // Creates a stream for a single glob or filter createStream: function(ourGlob, negatives, opt) { - if (!negatives) negatives = []; - if (!opt) opt = {}; - if (typeof opt.cwd !== 'string') opt.cwd = process.cwd(); - if (typeof opt.dot !== 'boolean') opt.dot = false; - if (typeof opt.silent !== 'boolean') opt.silent = true; - if (typeof opt.nonull !== 'boolean') opt.nonull = false; - if (typeof opt.cwdbase !== 'boolean') opt.cwdbase = false; - if (opt.cwdbase) opt.base = opt.cwd; - // remove path relativity to make globs make sense - ourGlob = unrelative(opt.cwd, ourGlob); - negatives = negatives.map(unrelative.bind(null, opt.cwd)); + var ourOpt = extend({}, opt); + delete ourOpt.root; + + // Extract base path from glob + var basePath = ourOpt.base || getBasePath(ourGlob, opt); + + // Remove path relativity to make globs make sense + ourGlob = resolveGlob(ourGlob, opt); - // create globbing stuff - var globber = new glob.Glob(ourGlob, opt); + // Create globbing stuff + var globber = new glob.Glob(ourGlob, ourOpt); - // extract base path from glob - var basePath = opt.base ? opt.base : glob2base(globber); + // Create stream and map events from globber to it + var stream = through2.obj(opt, + negatives.length ? filterNegatives : undefined); - // create stream and map events from globber to it - var stream = through2.obj(negatives.length ? filterNegatives : undefined); + var found = false; globber.on('error', stream.emit.bind(stream, 'error')); - globber.on('end', function(/* some args here so can't use bind directly */){ + globber.once('end', function() { + if (opt.allowEmpty !== true && !found && globIsSingular(globber)) { + stream.emit('error', + new Error('File not found with singular glob: ' + ourGlob)); + } + stream.end(); }); globber.on('match', function(filename) { + found = true; + stream.write({ cwd: opt.cwd, base: basePath, - path: path.resolve(opt.cwd, filename) + path: path.normalize(filename), }); }); return stream; function filterNegatives(filename, enc, cb) { - var matcha = isMatch.bind(null, filename, opt); + var matcha = isMatch.bind(null, filename); if (negatives.every(matcha)) { - cb(null, filename); // pass + cb(null, filename); // Pass } else { - cb(); // ignore + cb(); // Ignore } } }, - // creates a stream for multiple globs or filters + // Creates a stream for multiple globs or filters create: function(globs, opt) { - if (!opt) opt = {}; + if (!opt) { + opt = {}; + } + if (typeof opt.cwd !== 'string') { + opt.cwd = process.cwd(); + } + if (typeof opt.dot !== 'boolean') { + opt.dot = false; + } + if (typeof opt.silent !== 'boolean') { + opt.silent = true; + } + if (typeof opt.nonull !== 'boolean') { + opt.nonull = false; + } + if (typeof opt.cwdbase !== 'boolean') { + opt.cwdbase = false; + } + if (opt.cwdbase) { + opt.base = opt.cwd; + } + + // Only one glob no need to aggregate + if (!Array.isArray(globs)) { + globs = [globs]; + } - // only one glob no need to aggregate - if (!Array.isArray(globs)) return gs.createStream(globs, null, opt); + var positives = []; + var negatives = []; - var positives = globs.filter(isPositive); - var negatives = globs.filter(isNegative); + var ourOpt = extend({}, opt); + delete ourOpt.root; - if (positives.length === 0) throw new Error("Missing positive glob"); + globs.forEach(function(glob, index) { + if (typeof glob !== 'string' && !(glob instanceof RegExp)) { + throw new Error('Invalid glob at index ' + index); + } - // only one positive glob no need to aggregate - if (positives.length === 1) return gs.createStream(positives[0], negatives, opt); + var globArray = isNegative(glob) ? negatives : positives; - // create all individual streams - var streams = positives.map(function(glob){ - return gs.createStream(glob, negatives, opt); + // Create Minimatch instances for negative glob patterns + if (globArray === negatives && typeof glob === 'string') { + var ourGlob = resolveGlob(glob, opt); + glob = micromatch.matcher(ourGlob, ourOpt); + } + + globArray.push({ + index: index, + glob: glob, + }); }); - // then just pipe them to a single unique stream and return it + if (positives.length === 0) { + throw new Error('Missing positive glob'); + } + + // Only one positive glob no need to aggregate + if (positives.length === 1) { + return streamFromPositive(positives[0]); + } + + // Create all individual streams + var streams = positives.map(streamFromPositive); + + // Then just pipe them to a single unique stream and return it var aggregate = new Combine(streams); var uniqueStream = unique('path'); + var returnStream = aggregate.pipe(uniqueStream); - return aggregate.pipe(uniqueStream); - } + aggregate.on('error', function(err) { + returnStream.emit('error', err); + }); + + return returnStream; + + function streamFromPositive(positive) { + var negativeGlobs = negatives.filter(indexGreaterThan(positive.index)) + .map(toGlob); + return gs.createStream(positive.glob, negativeGlobs, opt); + } + }, }; -function isMatch(file, opt, pattern) { - if (typeof pattern === 'string') return minimatch(file.path, pattern, opt); - if (pattern instanceof RegExp) return pattern.test(file.path); - return true; // unknown glob type? +function isMatch(file, matcher) { + if (typeof matcher === 'function') { + return matcher(file.path); + } + if (matcher instanceof RegExp) { + return matcher.test(file.path); + } } function isNegative(pattern) { - if (typeof pattern !== 'string') return true; - if (pattern[0] === '!') return true; - return false; + if (typeof pattern === 'string') { + return pattern[0] === '!'; + } + if (pattern instanceof RegExp) { + return true; + } +} + +function indexGreaterThan(index) { + return function(obj) { + return obj.index > index; + }; } -function isPositive(pattern) { - return !isNegative(pattern); +function toGlob(obj) { + return obj.glob; } -function unrelative(cwd, glob) { - var mod = ''; - if (glob[0] === '!') { - mod = glob[0]; - glob = glob.slice(1); +function globIsSingular(glob) { + var globSet = glob.minimatch.set; + + if (globSet.length !== 1) { + return false; } - return mod+path.resolve(cwd, glob); + + return globSet[0].every(function isString(value) { + return typeof value === 'string'; + }); } +function getBasePath(ourGlob, opt) { + var basePath; + var parent = globParent(ourGlob); + + if (parent === '/' && opt && opt.root) { + basePath = path.normalize(opt.root); + } else { + basePath = resolveGlob(parent, opt); + } + + if (!sepRe.test(basePath.charAt(basePath.length - 1))) { + basePath += path.sep; + } + return basePath; +} module.exports = gs; |