Skip to content

Commit

Permalink
The file.expand 'filter' option can now be either a string or a funct…
Browse files Browse the repository at this point in the history
…ion.

* The string has to be a valid a fs.Stats method name.
* The function is synchronous, accepts a relative filepath, and should return true or false.
  • Loading branch information
cowboy committed Jan 8, 2013
1 parent b60907b commit bacc8b5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
8 changes: 6 additions & 2 deletions lib/grunt/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,12 @@ file.expand = function() {
matches = matches.filter(function(filepath) {
filepath = path.join(options.cwd, filepath);
try {
// If the file is of the right type and exists, this should work.
return fs.statSync(filepath)[options.filter]();
if (typeof options.filter === 'function') {
return options.filter(filepath);
} else {
// If the file is of the right type and exists, this should work.
return fs.statSync(filepath)[options.filter]();
}
} catch(e) {
// Otherwise, it's probably not the right type.
return false;
Expand Down
6 changes: 5 additions & 1 deletion test/grunt/file_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ exports['file.expand*'] = {
test.done();
},
'filter': function(test) {
test.expect(4);
test.expect(5);
test.deepEqual(grunt.file.expand({filter: 'isFile'}, '**d*/**'), [
'deep/deep.txt',
'deep/deeper/deeper.txt',
Expand All @@ -155,6 +155,10 @@ exports['file.expand*'] = {
'deep/deeper',
'deep/deeper/deepest'
], 'should match directories only.');
test.deepEqual(grunt.file.expand({filter: function(filepath) { return (/deepest/).test(filepath); }}, '**'), [
'deep/deeper/deepest',
'deep/deeper/deepest/deepest.txt',
], 'should filter arbitrarily.');
test.deepEqual(grunt.file.expand({filter: 'isFile'}, 'js', 'css'), [], 'should fail to match.');
test.deepEqual(grunt.file.expand({filter: 'isDirectory'}, '**/*.js'), [], 'should fail to match.');
test.done();
Expand Down

0 comments on commit bacc8b5

Please sign in to comment.