123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- 'use strict';
- var inherits = require('util').inherits;
- var glob = require('glob');
- var extend = require('extend');
- var Readable = require('readable-stream').Readable;
- var globParent = require('glob-parent');
- var toAbsoluteGlob = require('to-absolute-glob');
- var removeTrailingSeparator = require('remove-trailing-separator');
- var globErrMessage1 = 'File not found with singular glob: ';
- var globErrMessage2 = ' (if this was purposeful, use `allowEmpty` option)';
- function getBasePath(ourGlob, opt) {
- return globParent(toAbsoluteGlob(ourGlob, opt));
- }
- function globIsSingular(glob) {
- var globSet = glob.minimatch.set;
- if (globSet.length !== 1) {
- return false;
- }
- return globSet[0].every(function isString(value) {
- return typeof value === 'string';
- });
- }
- function GlobStream(ourGlob, negatives, opt) {
- if (!(this instanceof GlobStream)) {
- return new GlobStream(ourGlob, negatives, opt);
- }
- var ourOpt = extend({}, opt);
- Readable.call(this, {
- objectMode: true,
- highWaterMark: ourOpt.highWaterMark || 16,
- });
-
- delete ourOpt.highWaterMark;
- var self = this;
- function resolveNegatives(negative) {
- return toAbsoluteGlob(negative, ourOpt);
- }
- var ourNegatives = negatives.map(resolveNegatives);
- ourOpt.ignore = ourNegatives;
- var cwd = ourOpt.cwd;
- var allowEmpty = ourOpt.allowEmpty || false;
-
- var basePath = ourOpt.base || getBasePath(ourGlob, ourOpt);
-
- ourGlob = toAbsoluteGlob(ourGlob, ourOpt);
-
- delete ourOpt.root;
- var globber = new glob.Glob(ourGlob, ourOpt);
- this._globber = globber;
- var found = false;
- globber.on('match', function(filepath) {
- found = true;
- var obj = {
- cwd: cwd,
- base: basePath,
- path: removeTrailingSeparator(filepath),
- };
- if (!self.push(obj)) {
- globber.pause();
- }
- });
- globber.once('end', function() {
- if (allowEmpty !== true && !found && globIsSingular(globber)) {
- var err = new Error(globErrMessage1 + ourGlob + globErrMessage2);
- return self.destroy(err);
- }
- self.push(null);
- });
- function onError(err) {
- self.destroy(err);
- }
- globber.once('error', onError);
- }
- inherits(GlobStream, Readable);
- GlobStream.prototype._read = function() {
- this._globber.resume();
- };
- GlobStream.prototype.destroy = function(err) {
- var self = this;
- this._globber.abort();
- process.nextTick(function() {
- if (err) {
- self.emit('error', err);
- }
- self.emit('close');
- });
- };
- module.exports = GlobStream;
|