Skip to content

Commit

Permalink
Merge pull request #27 from alexlafroscia/add-ignore-option
Browse files Browse the repository at this point in the history
Add ignore option
  • Loading branch information
stefanpenner authored Jul 27, 2016
2 parents 9a4ecc6 + 4a2fc7c commit 9a2eb7a
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 6 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ entry.isDirectory() // => true if directory
// => ['one.txt', 'subdir/two.txt']
```

* `ignore`: An array of globs. Files and directories that match at least one
of the provided globs will be pruned while searching.

```js
var paths = walkSync('project', { ignore: ['subdir'] })
// => ['one.txt']
```

## Background

`walkSync(baseDir)` is a faster substitute for
Expand Down
22 changes: 16 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,30 @@ function _walkSync(baseDir, options, _relativePath) {
// https://github.com/joyent/node/pull/6929
var relativePath = handleRelativePath(_relativePath);
var globs = options.globs;
var m;
var ignorePatterns = options.ignore;
var globMatcher, ignoreMatcher;
var results = [];

if (ignorePatterns) {
ignoreMatcher = new MatcherCollection(ignorePatterns);
}

if (globs) {
m = new MatcherCollection(globs);
globMatcher = new MatcherCollection(globs);
}

var results = [];
if (m && !m.mayContain(relativePath)) {
if (globMatcher && !globMatcher.mayContain(relativePath)) {
return results;
}

var names = fs.readdirSync(baseDir + '/' + relativePath);
var entries = names.map(function (name) {
var entryRelativePath = relativePath + name;

if (ignoreMatcher && ignoreMatcher.match(entryRelativePath)) {
return;
}

var fullPath = baseDir + '/' + entryRelativePath;
var stats = getStat(fullPath);

Expand Down Expand Up @@ -86,12 +96,12 @@ function _walkSync(baseDir, options, _relativePath) {
var entry = sortedEntries[i];

if (entry.isDirectory()) {
if (options.directories !== false && (!m || m.match(entry.relativePath))) {
if (options.directories !== false && (!globMatcher || globMatcher.match(entry.relativePath))) {
results.push(entry);
}
results = results.concat(_walkSync(baseDir, options, entry.relativePath));
} else {
if (!m || m.match(entry.relativePath)) {
if (!globMatcher || globMatcher.match(entry.relativePath)) {
results.push(entry);
}
}
Expand Down
42 changes: 42 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,45 @@ test('walkSync with matchers', function (t) {

t.end();
});

test('walksync with ignore pattern', function (t) {
t.deepEqual(walkSync('test/fixtures', {
ignore: ['dir']
}), [
'foo.txt',
'foo/',
'foo/a.js',
'some-other-dir/',
'some-other-dir/qux.txt',
'symlink1/',
'symlink1/qux.txt',
'symlink2'
]);

t.deepEqual(walkSync('test/fixtures', {
ignore: ['**/subdir']
}), [
'dir/',
'dir/bar.txt',
'dir/zzz.txt',
'foo.txt',
'foo/',
'foo/a.js',
'some-other-dir/',
'some-other-dir/qux.txt',
'symlink1/',
'symlink1/qux.txt',
'symlink2'
]);

t.deepEqual(walkSync('test/fixtures', {
globs: ['**/*.txt'],
ignore: ['dir']
}), [
'foo.txt',
'some-other-dir/qux.txt',
'symlink1/qux.txt'
]);

t.end();
});

0 comments on commit 9a2eb7a

Please sign in to comment.