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 ignore option #27

Merged
merged 5 commits into from
Jul 27, 2016
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
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];

Copy link
Collaborator

@stefanpenner stefanpenner Jul 26, 2016

Choose a reason for hiding this comment

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

Should we simply do if (isIgnored) { continue; }, that should somewhat simplify the code the follows.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Absolutely, makes sense.

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();
});