Skip to content

Commit

Permalink
Breaking: Remove createStream external API (closes #74)
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Feb 21, 2017
1 parent 755b7b4 commit 9911598
Show file tree
Hide file tree
Showing 3 changed files with 669 additions and 680 deletions.
10 changes: 3 additions & 7 deletions README.md
Expand Up @@ -15,7 +15,7 @@ A wrapper around [node-glob][node-glob-url] to make it streamy.
```javascript
var gs = require('glob-stream');

var stream = gs.create('./files/**/*.coffee', { /* options */ });
var stream = gs('./files/**/*.coffee', { /* options */ });

stream.on('data', function(file){
// file has path, base, and cwd attrs
Expand All @@ -26,14 +26,10 @@ You can pass any combination of globs. One caveat is that you can not only pass

## API

### create(globs, options)
### globStream(globs, options)

Returns a stream for multiple globs or filters.

### createStream(positiveGlob, negativeGlobs, options)

Returns a stream for a single glob or filter.

### Options

- cwd
Expand All @@ -53,7 +49,7 @@ This argument is passed directly to [node-glob][node-glob-url] so check there fo
### Glob

```js
var stream = gs.create(['./**/*.js', '!./node_modules/**/*']);
var stream = gs(['./**/*.js', '!./node_modules/**/*']);
```

Globs are executed in order, so negations should follow positive globs. For example:
Expand Down
198 changes: 97 additions & 101 deletions index.js
Expand Up @@ -13,128 +13,124 @@ var path = require('path');
var extend = require('extend');
var sepRe = (process.platform === 'win32' ? /[\/\\]/ : /\/+/);

var gs = {
// Creates a stream for a single glob or filter
createStream: function(ourGlob, negatives, opt) {
function resolveNegatives(negative) {
return resolveGlob(negative, opt);
}
function globStream(globs, 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];
}

var ourOpt = extend({}, opt);
delete ourOpt.root;
var positives = [];
var negatives = [];

if (Array.isArray(opt.ignore)) {
negatives = opt.ignore.concat(negatives);
globs.forEach(function(globString, index) {
if (typeof globString !== 'string') {
throw new Error('Invalid glob at index ' + index);
}
var ourNegatives = negatives.map(resolveNegatives);
ourOpt.ignore = ourNegatives;

// Extract base path from glob
var basePath = ourOpt.base || getBasePath(ourGlob, opt);
var glob = isNegatedGlob(globString);
var globArray = glob.negated ? negatives : positives;

// Remove path relativity to make globs make sense
ourGlob = resolveGlob(ourGlob, opt);
globArray.push({
index: index,
glob: glob.pattern,
});
});

// Create globbing stuff
var globber = new glob.Glob(ourGlob, ourOpt);
if (positives.length === 0) {
throw new Error('Missing positive glob');
}

// Create stream and map events from globber to it
var stream = through2.obj(ourOpt);
// Only one positive glob no need to aggregate
if (positives.length === 1) {
return streamFromPositive(positives[0]);
}

var found = false;
// Create all individual streams
var streams = positives.map(streamFromPositive);

globber.on('error', stream.emit.bind(stream, 'error'));
globber.once('end', function() {
if (opt.allowEmpty !== true && !found && globIsSingular(globber)) {
stream.emit('error',
new Error('File not found with singular glob: ' + ourGlob));
}
// Then just pipe them to a single unique stream and return it
var aggregate = new Combine(streams);
var uniqueStream = unique('path');

stream.end();
});
globber.on('match', function(filename) {
found = true;

stream.write({
cwd: opt.cwd,
base: basePath,
path: path.normalize(filename),
});
});
return stream;
},
return pumpify.obj(aggregate, uniqueStream);

// Creates a stream for multiple globs or filters
create: function(globs, 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;
}
function streamFromPositive(positive) {
var negativeGlobs = negatives.filter(indexGreaterThan(positive.index))
.map(toGlob);
return createStream(positive.glob, negativeGlobs, opt);
}
}

// Only one glob no need to aggregate
if (!Array.isArray(globs)) {
globs = [globs];
}
function createStream(ourGlob, negatives, opt) {
function resolveNegatives(negative) {
return resolveGlob(negative, opt);
}

var positives = [];
var negatives = [];
var ourOpt = extend({}, opt);
delete ourOpt.root;

globs.forEach(function(globString, index) {
if (typeof globString !== 'string') {
throw new Error('Invalid glob at index ' + index);
}
if (Array.isArray(opt.ignore)) {
negatives = opt.ignore.concat(negatives);
}
var ourNegatives = negatives.map(resolveNegatives);
ourOpt.ignore = ourNegatives;

var glob = isNegatedGlob(globString);
var globArray = glob.negated ? negatives : positives;
// Extract base path from glob
var basePath = ourOpt.base || getBasePath(ourGlob, opt);

globArray.push({
index: index,
glob: glob.pattern,
});
});
// Remove path relativity to make globs make sense
ourGlob = resolveGlob(ourGlob, opt);

if (positives.length === 0) {
throw new Error('Missing positive glob');
}
// Create globbing stuff
var globber = new glob.Glob(ourGlob, ourOpt);

// Only one positive glob no need to aggregate
if (positives.length === 1) {
return streamFromPositive(positives[0]);
}
// Create stream and map events from globber to it
var stream = through2.obj(ourOpt);

// Create all individual streams
var streams = positives.map(streamFromPositive);
var found = false;

// Then just pipe them to a single unique stream and return it
var aggregate = new Combine(streams);
var uniqueStream = unique('path');
globber.on('error', stream.emit.bind(stream, 'error'));
globber.once('end', function() {
if (opt.allowEmpty !== true && !found && globIsSingular(globber)) {
stream.emit('error',
new Error('File not found with singular glob: ' + ourGlob));
}

return pumpify.obj(aggregate, uniqueStream);
stream.end();
});
globber.on('match', function(filename) {
found = true;

function streamFromPositive(positive) {
var negativeGlobs = negatives.filter(indexGreaterThan(positive.index))
.map(toGlob);
return gs.createStream(positive.glob, negativeGlobs, opt);
}
},
};
stream.write({
cwd: opt.cwd,
base: basePath,
path: path.normalize(filename),
});
});
return stream;
}

function indexGreaterThan(index) {
return function(obj) {
Expand Down Expand Up @@ -173,4 +169,4 @@ function getBasePath(ourGlob, opt) {
return basePath;
}

module.exports = gs;
module.exports = globStream;

0 comments on commit 9911598

Please sign in to comment.