Skip to content

Commit

Permalink
Fix: Resolve globs against root option & avoid passing root option to…
Browse files Browse the repository at this point in the history
… node-glob (fixes #37)
  • Loading branch information
Janpot authored and phated committed Feb 21, 2017
1 parent 5f5fc22 commit 55dcf96
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
23 changes: 18 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@ var glob = require('glob');
var Minimatch = require('minimatch').Minimatch;
var glob2base = require('glob2base');
var path = require('path');
var extend = require('extend');

var gs = {
// creates a stream for a single glob or filter
createStream: function(ourGlob, negatives, opt) {

// remove path relativity to make globs make sense
ourGlob = unrelative(opt.cwd, ourGlob);
ourGlob = resolveGlob(ourGlob, opt);
var ourOpt = extend({}, opt);
delete ourOpt.root;

// create globbing stuff
var globber = new glob.Glob(ourGlob, opt);
var globber = new glob.Glob(ourGlob, ourOpt);

// extract base path from glob
var basePath = opt.base || glob2base(globber);
Expand Down Expand Up @@ -74,6 +78,9 @@ var gs = {
var positives = [];
var negatives = [];

var ourOpt = extend({}, opt);
delete ourOpt.root;

globs.forEach(function(glob, index) {
if (typeof glob !== 'string' && !(glob instanceof RegExp)) {
throw new Error('Invalid glob at index ' + index);
Expand All @@ -83,7 +90,8 @@ var gs = {

// create Minimatch instances for negative glob patterns
if (globArray === negatives && typeof glob === 'string') {
glob = new Minimatch(unrelative(opt.cwd, glob), opt);
var ourGlob = resolveGlob(glob, opt);
glob = new Minimatch(ourGlob, ourOpt);
}

globArray.push({
Expand Down Expand Up @@ -128,13 +136,18 @@ function isNegative(pattern) {
if (pattern instanceof RegExp) return true;
}

function unrelative(cwd, glob) {
function resolveGlob(glob, opt) {
var mod = '';
if (glob[0] === '!') {
mod = glob[0];
glob = glob.slice(1);
}
return mod+path.resolve(cwd, glob);
if (opt.root && glob[0] === '/') {
glob = path.resolve(opt.root, '.'+glob);
} else {
glob = path.resolve(opt.cwd, glob);
}
return mod+glob;
}

function indexGreaterThan(index) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"index.js"
],
"dependencies": {
"extend": "^2.0.0",
"glob": "^5.0.3",
"minimatch": "^2.0.1",
"ordered-read-streams": "^0.2.0",
Expand Down
36 changes: 36 additions & 0 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,42 @@ describe('glob-stream', function() {
});
});

it('should resolve relative paths when root option is given', function(done) {
var stream = gs.create('./fixtures/test.coffee', {cwd: __dirname, root: __dirname + '/fixtures'});
should.exist(stream);
stream.on('error', function(err) {
throw err;
});
stream.on('data', function(file) {
should.exist(file);
should.exist(file.path);
should.exist(file.base);
should.exist(file.cwd);
String(file.cwd).should.equal(__dirname);
String(file.base).should.equal(join(__dirname, 'fixtures'+sep));
String(join(file.path,'')).should.equal(join(__dirname, './fixtures/test.coffee'));
done();
});
});

it('should resolve absolute paths when root option is given', function(done) {
var stream = gs.create('/test.coffee', {cwd: __dirname, root: __dirname + '/fixtures'});
should.exist(stream);
stream.on('error', function(err) {
throw err;
});
stream.on('data', function(file) {
should.exist(file);
should.exist(file.path);
should.exist(file.base);
should.exist(file.cwd);
String(file.cwd).should.equal(__dirname);
String(file.base).should.equal(join(__dirname, 'fixtures'+sep));
String(join(file.path,'')).should.equal(join(__dirname, './fixtures/test.coffee'));
done();
});
});

it('should not emit error on glob containing {} when not found', function(done) {
var stream = gs.create('notfound{a,b}');
should.exist(stream);
Expand Down

0 comments on commit 55dcf96

Please sign in to comment.