Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add globbing support for include and exclude. #9

Merged
merged 1 commit into from
Dec 27, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 67 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ var path = require('path');
var rimraf = RSVP.denodeify(require('rimraf'));
var mkdirp = require('mkdirp');
var walkSync = require('walk-sync');
var Minimatch = require('minimatch').Minimatch;
var CoreObject = require('core-object');
var symlinkOrCopy = require('symlink-or-copy');
var generateRandomString = require('./lib/generate-random-string');


function makeDictionary() {
var cache = Object.create(null);

Expand Down Expand Up @@ -40,13 +42,8 @@ function Funnel(inputTree, options) {
throw new Error('Invalid files option, it must be an array.');
}

if (this.include && !Array.isArray(this.include)) {
throw new Error('Invalid include option, it must be an array.');
}

if (this.exclude && !Array.isArray(this.exclude)) {
throw new Error('Invalid exclude option, it must be an array.');
}
this._setupIncludes();
this._setupExcludes();

this._instantiatedStack = (new Error()).stack;
}
Expand All @@ -65,6 +62,52 @@ Funnel.prototype.setupDestPaths = function() {
}
};

Funnel.prototype._setupIncludes = function() {
if (!this.include) {
return;
}

if (!Array.isArray(this.include)) {
throw new Error('Invalid include option, it must be an array.');
}

for (var i = 0, l = this.include.length; i < l; i++) {
this.include[i] = this._processPattern(this.include[i]);
}
};

Funnel.prototype._setupExcludes = function() {
if (!this.exclude) {
return;
}

if (!Array.isArray(this.exclude)) {
throw new Error('Invalid exclude option, it must be an array.');
}

for (var i = 0, l = this.exclude.length; i < l; i++) {
this.exclude[i] = this._processPattern(this.exclude[i]);
}
};

Funnel.prototype._processPattern = function(pattern) {
if (pattern instanceof RegExp) {
return pattern;
}

var type = typeof pattern;

if (type === 'string') {
return new Minimatch(pattern);
}

if (type === 'function') {
return pattern;
}

throw new Error('include/exclude patterns can be a RegExp, glob string, or function. You supplied `' + typeof pattern +'`.');
};

Funnel.prototype.shouldLinkRoots = function() {
return !this.files && !this.include && !this.exclude && !this.getDestinationPath;
};
Expand Down Expand Up @@ -145,7 +188,7 @@ Funnel.prototype.includeFile = function(relativePath) {
return includeFileCache[relativePath] = false;
}

var i, l;
var i, l, pattern;

// Check for specific files listing
if (this.files) {
Expand All @@ -156,7 +199,9 @@ Funnel.prototype.includeFile = function(relativePath) {
if (this.exclude) {
for (i = 0, l = this.exclude.length; i < l; i++) {
// An exclude pattern that returns true should be ignored
if (this.exclude[i].test(relativePath) === true) {
pattern = this.exclude[i];

if (this._matchesPattern(pattern, relativePath)) {
return includeFileCache[relativePath] = false;
}
}
Expand All @@ -167,7 +212,9 @@ Funnel.prototype.includeFile = function(relativePath) {
for (i = 0, l = this.include.length; i < l; i++) {
// An include pattern that returns true (and wasn't excluded at all)
// should _not_ be ignored
if (this.include[i].test(relativePath) === true) {
pattern = this.include[i];

if (this._matchesPattern(pattern, relativePath)) {
return includeFileCache[relativePath] = true;
}
}
Expand All @@ -180,6 +227,16 @@ Funnel.prototype.includeFile = function(relativePath) {
return includeFileCache[relativePath] = true;
};

Funnel.prototype._matchesPattern = function(pattern, relativePath) {
if (pattern.test) {
return pattern.test(relativePath);
} else if (pattern.match) {
return pattern.match(relativePath);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should likely throw if neither of these branches are hit.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to throw.


throw new Error('Pattern `' + pattern + '` was not a RegExp or Glob.');
};

Funnel.prototype.processFile = function(sourcePath, destPath /*, relativePath */) {
this._copy(sourcePath, destPath);
};
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
],
"dependencies": {
"core-object": "0.0.2",
"minimatch": "^2.0.1",
"mkdirp": "^0.5.0",
"rimraf": "^2.2.8",
"rsvp": "^3.0.14",
Expand Down
138 changes: 61 additions & 77 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,22 @@ describe('broccoli-funnel', function(){
});

describe('with filtering options', function() {
function testFiltering(includes, excludes, files, expected) {
var inputPath = path.join(fixturePath, 'dir1');
var tree = new Funnel(inputPath, {
include: includes,
exclude: excludes,
files: files
});

builder = new broccoli.Builder(tree);
return builder.build()
.then(function(results) {
var outputPath = results.directory;

expect(walkSync(outputPath)).to.eql(expected);
});
}

describe('filtering with `files`', function() {
it('can take a list of files', function() {
Expand Down Expand Up @@ -166,96 +182,64 @@ describe('broccoli-funnel', function(){
});

describe('include filtering', function() {
it('can take a pattern', function() {
var inputPath = path.join(fixturePath, 'dir1');
var tree = new Funnel(inputPath, {
include: [ /.png$/ ]
function testAllIncludeMatchers(glob, regexp, func, expected) {
it('can take a glob string', function() {
testFiltering(glob, null, null, expected);
});

builder = new broccoli.Builder(tree);
return builder.build()
.then(function(results) {
var outputPath = results.directory;

var expected = [
'subdir1/',
'subdir1/subsubdir1/',
'subdir1/subsubdir1/foo.png'
];

expect(walkSync(outputPath)).to.eql(expected);
});
});

it('can take multiple patterns', function() {
var inputPath = path.join(fixturePath, 'dir1');
var tree = new Funnel(inputPath, {
include: [ /.png$/, /.js$/ ]
it('can take a regexp pattern', function() {
testFiltering(regexp, null, null, expected);
});

builder = new broccoli.Builder(tree);
return builder.build()
.then(function(results) {
var outputPath = results.directory;

var expected = [
'subdir1/',
'subdir1/subsubdir1/',
'subdir1/subsubdir1/foo.png',
'subdir1/subsubdir2/',
'subdir1/subsubdir2/some.js'
];

expect(walkSync(outputPath)).to.eql(expected);
it.skip('can take a function', function() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

skip

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left it there intentionally as a placeholder for #10.

testFiltering(func, null, null, expected);
});
});
}

testAllIncludeMatchers([ '**/*.png' ], [ /.png$/ ], null, [
'subdir1/',
'subdir1/subsubdir1/',
'subdir1/subsubdir1/foo.png'
]);

testAllIncludeMatchers([ '**/*.png', '**/*.js' ], [ /.png$/, /.js$/ ], null, [
'subdir1/',
'subdir1/subsubdir1/',
'subdir1/subsubdir1/foo.png',
'subdir1/subsubdir2/',
'subdir1/subsubdir2/some.js'
]);
});

describe('exclude filtering', function() {
it('can take a pattern', function() {
var inputPath = path.join(fixturePath, 'dir1');
var tree = new Funnel(inputPath, {
exclude: [ /.png$/ ]
function testAllExcludeMatchers(glob, regexp, func, expected) {
it('can take a glob string', function() {
testFiltering(null, glob, null, expected);
});

builder = new broccoli.Builder(tree);
return builder.build()
.then(function(results) {
var outputPath = results.directory;

var expected = [
'root-file.txt',
'subdir1/',
'subdir1/subsubdir2/',
'subdir1/subsubdir2/some.js',
'subdir2/',
'subdir2/bar.css'
];

expect(walkSync(outputPath)).to.eql(expected);
it('can take a regexp pattern', function() {
testFiltering(null, regexp, null, expected);
});
});

it('can take multiple patterns', function() {
var inputPath = path.join(fixturePath, 'dir1');
var tree = new Funnel(inputPath, {
exclude: [ /.png$/, /.js$/ ]
});

builder = new broccoli.Builder(tree);
return builder.build()
.then(function(results) {
var outputPath = results.directory;

var expected = [
'root-file.txt',
'subdir2/',
'subdir2/bar.css'
];

expect(walkSync(outputPath)).to.eql(expected);
it.skip('can take a function', function() {
testFiltering(null, func, null, expected);
});
});
}

testAllExcludeMatchers([ '**/*.png' ], [ /.png$/ ], null, [
'root-file.txt',
'subdir1/',
'subdir1/subsubdir2/',
'subdir1/subsubdir2/some.js',
'subdir2/',
'subdir2/bar.css'
]);

testAllExcludeMatchers([ '**/*.png', '**/*.js' ], [ /.png$/, /.js$/ ], null, [
'root-file.txt',
'subdir2/',
'subdir2/bar.css'
]);
});

it('combined filtering', function() {
Expand Down