From ac4c9c944707eeb86681f3fa843be5b803b9f64e Mon Sep 17 00:00:00 2001 From: Alex LaFroscia Date: Mon, 25 Jul 2016 20:59:54 -0700 Subject: [PATCH 1/5] Add (failing) tests for ignored directories --- test/test.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/test/test.js b/test/test.js index d91ad0e..cae425a 100644 --- a/test/test.js +++ b/test/test.js @@ -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(); +}); From 52637f37d334900a2b92f13e74676c25cdfb5d55 Mon Sep 17 00:00:00 2001 From: Alex LaFroscia Date: Mon, 25 Jul 2016 23:02:33 -0700 Subject: [PATCH 2/5] Prune paths that match an ignore pattern --- index.js | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index ff4c16d..5025bba 100644 --- a/index.js +++ b/index.js @@ -45,14 +45,19 @@ 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; } @@ -84,14 +89,15 @@ function _walkSync(baseDir, options, _relativePath) { for (var i=0; i Date: Mon, 25 Jul 2016 23:09:27 -0700 Subject: [PATCH 3/5] Add `ignore` option to the README --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 04640ce..dfd33a8 100644 --- a/README.md +++ b/README.md @@ -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 From c2f6ccca2b52ea5fd45bd780c9647baefef6a1ce Mon Sep 17 00:00:00 2001 From: Alex LaFroscia Date: Tue, 26 Jul 2016 13:14:49 -0700 Subject: [PATCH 4/5] Simplify skipping of ignored patterns --- index.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 5025bba..3a46174 100644 --- a/index.js +++ b/index.js @@ -89,15 +89,17 @@ function _walkSync(baseDir, options, _relativePath) { for (var i=0; i Date: Tue, 26 Jul 2016 15:45:23 -0700 Subject: [PATCH 5/5] Prune ignored paths earlier to save unnecessary work --- index.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 3a46174..4552f4a 100644 --- a/index.js +++ b/index.js @@ -64,6 +64,11 @@ function _walkSync(baseDir, options, _relativePath) { 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); @@ -89,9 +94,6 @@ function _walkSync(baseDir, options, _relativePath) { for (var i=0; i