wallet-core/node_modules/glob-stream/index.js

210 lines
4.9 KiB
JavaScript
Raw Normal View History

2016-10-10 03:43:44 +02:00
'use strict';
var through2 = require('through2');
var Combine = require('ordered-read-streams');
var unique = require('unique-stream');
var glob = require('glob');
2016-11-03 01:33:53 +01:00
var micromatch = require('micromatch');
var resolveGlob = require('to-absolute-glob');
var globParent = require('glob-parent');
2016-10-10 03:43:44 +02:00
var path = require('path');
2016-11-03 01:33:53 +01:00
var extend = require('extend');
var sepRe = (process.platform === 'win32' ? /[\/\\]/ : /\/+/);
2016-10-10 03:43:44 +02:00
var gs = {
2016-11-03 01:33:53 +01:00
// Creates a stream for a single glob or filter
2016-10-10 03:43:44 +02:00
createStream: function(ourGlob, negatives, opt) {
2016-11-03 01:33:53 +01:00
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);
2016-10-10 03:43:44 +02:00
2016-11-03 01:33:53 +01:00
// Create globbing stuff
var globber = new glob.Glob(ourGlob, ourOpt);
2016-10-10 03:43:44 +02:00
2016-11-03 01:33:53 +01:00
// Create stream and map events from globber to it
var stream = through2.obj(opt,
negatives.length ? filterNegatives : undefined);
2016-10-10 03:43:44 +02:00
2016-11-03 01:33:53 +01:00
var found = false;
2016-10-10 03:43:44 +02:00
globber.on('error', stream.emit.bind(stream, 'error'));
2016-11-03 01:33:53 +01:00
globber.once('end', function() {
if (opt.allowEmpty !== true && !found && globIsSingular(globber)) {
stream.emit('error',
new Error('File not found with singular glob: ' + ourGlob));
}
2016-10-10 03:43:44 +02:00
stream.end();
});
globber.on('match', function(filename) {
2016-11-03 01:33:53 +01:00
found = true;
2016-10-10 03:43:44 +02:00
stream.write({
cwd: opt.cwd,
base: basePath,
2016-11-03 01:33:53 +01:00
path: path.normalize(filename),
2016-10-10 03:43:44 +02:00
});
});
return stream;
function filterNegatives(filename, enc, cb) {
2016-11-03 01:33:53 +01:00
var matcha = isMatch.bind(null, filename);
2016-10-10 03:43:44 +02:00
if (negatives.every(matcha)) {
2016-11-03 01:33:53 +01:00
cb(null, filename); // Pass
2016-10-10 03:43:44 +02:00
} else {
2016-11-03 01:33:53 +01:00
cb(); // Ignore
2016-10-10 03:43:44 +02:00
}
}
},
2016-11-03 01:33:53 +01:00
// Creates a stream for multiple globs or filters
2016-10-10 03:43:44 +02:00
create: function(globs, opt) {
2016-11-03 01:33:53 +01:00
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];
}
2016-10-10 03:43:44 +02:00
2016-11-03 01:33:53 +01:00
var positives = [];
var negatives = [];
2016-10-10 03:43:44 +02:00
2016-11-03 01:33:53 +01:00
var ourOpt = extend({}, opt);
delete ourOpt.root;
2016-10-10 03:43:44 +02:00
2016-11-03 01:33:53 +01:00
globs.forEach(function(glob, index) {
if (typeof glob !== 'string' && !(glob instanceof RegExp)) {
throw new Error('Invalid glob at index ' + index);
}
2016-10-10 03:43:44 +02:00
2016-11-03 01:33:53 +01:00
var globArray = isNegative(glob) ? negatives : positives;
2016-10-10 03:43:44 +02:00
2016-11-03 01:33:53 +01:00
// 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,
});
2016-10-10 03:43:44 +02:00
});
2016-11-03 01:33:53 +01:00
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
2016-10-10 03:43:44 +02:00
var aggregate = new Combine(streams);
var uniqueStream = unique('path');
2016-11-03 01:33:53 +01:00
var returnStream = aggregate.pipe(uniqueStream);
2016-10-10 03:43:44 +02:00
2016-11-03 01:33:53 +01:00
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);
}
},
2016-10-10 03:43:44 +02:00
};
2016-11-03 01:33:53 +01:00
function isMatch(file, matcher) {
if (typeof matcher === 'function') {
return matcher(file.path);
}
if (matcher instanceof RegExp) {
return matcher.test(file.path);
}
2016-10-10 03:43:44 +02:00
}
function isNegative(pattern) {
2016-11-03 01:33:53 +01:00
if (typeof pattern === 'string') {
return pattern[0] === '!';
}
if (pattern instanceof RegExp) {
return true;
}
}
function indexGreaterThan(index) {
return function(obj) {
return obj.index > index;
};
2016-10-10 03:43:44 +02:00
}
2016-11-03 01:33:53 +01:00
function toGlob(obj) {
return obj.glob;
2016-10-10 03:43:44 +02:00
}
2016-11-03 01:33:53 +01:00
function globIsSingular(glob) {
var globSet = glob.minimatch.set;
if (globSet.length !== 1) {
return false;
2016-10-10 03:43:44 +02:00
}
2016-11-03 01:33:53 +01:00
return globSet[0].every(function isString(value) {
return typeof value === 'string';
});
2016-10-10 03:43:44 +02:00
}
2016-11-03 01:33:53 +01:00
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;
}
2016-10-10 03:43:44 +02:00
module.exports = gs;