diff --git a/benchmark/bm.js b/benchmark/bm.js index cc41a17..511b024 100644 --- a/benchmark/bm.js +++ b/benchmark/bm.js @@ -8,26 +8,24 @@ const klawSync = require('../klaw-sync.js') function help () { console.log(`Usage examples:\n`) - console.log(`npm run benchmark -- --dir= (basic usage without anything to ignore)`) - console.log(`npm run benchmark -- --dir= -i "{node_modules,.git}" (ignore node_modules and .git directories)`) - console.log(`npm run benchmark -- --dir= -i "node_modules" -i "*.js" (ignore node_modules and all js files)`) + console.log(`npm run benchmark -- --dir=`) + console.log(`npm run benchmark -- --dir= --nodir=true (ignore all directories)`) } -function perf (root, ign) { - var suite = Benchmark.Suite() - if (ign) { +function run (root, opts) { + if (!opts) { + const suite = Benchmark.Suite() suite.add('walk-sync', function () { - walkSync(root, {ignore: ign}) + walkSync(root) }).add('glob.sync', function () { globSync('**', { cwd: root, dot: true, mark: true, - strict: true, - ignore: ign + strict: true }) }).add('klaw-sync', function () { - klawSync(root, {ignore: ign}) + klawSync(root) }).on('error', function (er) { return er }).on('cycle', function (ev) { @@ -36,17 +34,19 @@ function perf (root, ign) { console.log('\nSummary: Fastest is ' + this.filter('fastest').map('name')) }).run({ 'async': false }) } else { + const suite = Benchmark.Suite() suite.add('walk-sync', function () { - walkSync(root) + walkSync(root, {directories: false}) }).add('glob.sync', function () { globSync('**', { cwd: root, dot: true, mark: true, - strict: true + strict: true, + nodir: true }) }).add('klaw-sync', function () { - klawSync(root) + klawSync(root, {nodir: true}) }).on('error', function (er) { return er }).on('cycle', function (ev) { @@ -57,28 +57,18 @@ function perf (root, ign) { } } -try { - if (!argv.dir) { - console.log('err: root dir must be specified.') - help() - process.exit(1) - } - var dir = path.resolve(argv.dir) - console.log('Running benchmark tests...\n') - console.log('root dir: ', argv.dir) - if (argv.i) { - process.stdout.write('ignore: ') - console.dir(argv.i) - console.log() - // convert ignore args to array - if (typeof argv.i === 'string') { - perf(dir, [argv.i]) - } else { - perf(dir, argv.i) - } +if (!argv.dir) { + console.log('err: root dir cannot be null.') + help() +} else { + const dir = path.resolve(argv.dir) + console.log('Running benchmark tests..') + if (argv.nodir) { + console.log(`root dir: ${dir}`) + console.log('option.nodir: true\n') + run(dir, {nodir: true}) } else { - perf(dir) + console.log(`root dir: ${dir}\n`) + run(dir) } -} catch (er) { - throw er } diff --git a/klaw-sync.js b/klaw-sync.js index 1bf5825..f7456da 100644 --- a/klaw-sync.js +++ b/klaw-sync.js @@ -1,6 +1,5 @@ 'use strict' const path = require('path') -const mm = require('micromatch') let fs try { fs = require('graceful-fs') @@ -14,20 +13,22 @@ function klawSync (dir, opts, ls) { const item = {path: pathItem, stats: stat} if (stat.isDirectory()) { if (!opts.nodir) { - if (opts.ignore) { - if (mm(pathItem, opts.ignore).length === 0) ls.push(item) - } else if (opts.filter) { - if (opts.filter(item)) ls.push(item) + if (opts.filter) { + if (opts.filter(item)) { + ls.push(item) + ls = klawSync(pathItem, opts, ls) + } + if (!opts.noRecursiveOnFilter) ls = klawSync(pathItem, opts, ls) } else { ls.push(item) + ls = klawSync(pathItem, opts, ls) } + } else { + ls = klawSync(pathItem, opts, ls) } - ls = klawSync(pathItem, opts, ls) } else { if (!opts.nofile) { - if (opts.ignore) { - if (mm(pathItem, opts.ignore).length === 0) ls.push(item) - } else if (opts.filter) { + if (opts.filter) { if (opts.filter(item)) ls.push(item) } else { ls.push(item) diff --git a/package.json b/package.json index d6af143..16ce01e 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ }, "homepage": "https://github.com/manidlou/node-klaw-sync#readme", "dependencies": { + "benchmark": "^2.1.4", "micromatch": "^2.3.11" }, "devDependencies": { diff --git a/test/test.js b/test/test.js index a23a912..1205d28 100644 --- a/test/test.js +++ b/test/test.js @@ -87,7 +87,7 @@ describe('klaw-sync', () => { assert.deepEqual(dir.stats, dirsOnly[i].stats) }) }) - + /* it('should ignore if opts.ignore is path name', () => { const dirToIgnore = path.join(TEST_DIR, 'node_modules') fs.ensureDirSync(dirToIgnore) @@ -100,7 +100,7 @@ describe('klaw-sync', () => { {path: FILES[1], stats: fs.statSync(FILES[1])}, {path: FILES[2], stats: fs.statSync(FILES[2])} ] - const items = klawSync(TEST_DIR, {ignore: '**/node_modules'}) + const items = klawSync(TEST_DIR, {ignore: 'node_modules'}) assert.equal(items.length, paths.length) items.forEach((p, i) => { assert.deepEqual(p, paths[i]) @@ -123,7 +123,7 @@ describe('klaw-sync', () => { {path: FILES[1], stats: fs.statSync(FILES[1])}, {path: FILES[2], stats: fs.statSync(FILES[2])} ] - const items = klawSync(TEST_DIR, {ignore: '{**/node_modules,**/.git}'}) + const items = klawSync(TEST_DIR, {ignore: '{node_modules,.git}'}) assert.equal(items.length, paths.length) items.forEach((p, i) => { assert.deepEqual(p, paths[i]) @@ -150,7 +150,7 @@ describe('klaw-sync', () => { {path: FILES[1], stats: fs.statSync(FILES[1])}, {path: FILES[2], stats: fs.statSync(FILES[2])} ] - const items = klawSync(TEST_DIR, {ignore: ['**/node_modules', '**/.git', '**/*.md']}) + const items = klawSync(TEST_DIR, {ignore: ['node_modules', '.git', '.md']}) assert.equal(items.length, paths.length) items.forEach((p, i) => { assert.deepEqual(p, paths[i]) @@ -168,7 +168,7 @@ describe('klaw-sync', () => { {path: f1, stats: fs.statSync(f1)}, {path: f2, stats: fs.statSync(f2)} ] - const items = klawSync(TEST_DIR, {ignore: '!**/*.js'}) + const items = klawSync(TEST_DIR, {ignore: '.js'}) assert.equal(items.length, paths.length) items.forEach((p, i) => { assert.deepEqual(p, paths[i]) @@ -176,61 +176,87 @@ describe('klaw-sync', () => { assert.deepEqual(p.stats, paths[i].stats) }) }) + */ - it('should filter if opts.filter is true based on path', () => { - const f1 = path.join(TEST_DIR, 'dir1', 'foo.js') - const f2 = path.join(TEST_DIR, 'dir2', 'dir2_1', 'bar.js') - fs.ensureFileSync(f1) - fs.ensureFileSync(f2) - const paths = [ - {path: f1, stats: fs.statSync(f1)}, - {path: f2, stats: fs.statSync(f2)} - ] - const filterFunc = (i) => path.extname(i.path) === '.js' - const items = klawSync(TEST_DIR, {filter: filterFunc}) - assert.equal(items.length, paths.length) - items.forEach((p, i) => { - assert.deepEqual(p, paths[i]) - assert.strictEqual(p.path, paths[i].path) - assert.deepEqual(p.stats, paths[i].stats) + describe('when opts.filter is true', () => { + it('should filter based on path', () => { + const f1 = path.join(TEST_DIR, 'dir1', 'foo.js') + const f2 = path.join(TEST_DIR, 'dir2', 'dir2_1', 'bar.js') + fs.ensureFileSync(f1) + fs.ensureFileSync(f2) + const paths = [ + {path: f1, stats: fs.statSync(f1)}, + {path: f2, stats: fs.statSync(f2)} + ] + const filterFunc = (i) => path.extname(i.path) === '.js' + const items = klawSync(TEST_DIR, {filter: filterFunc}) + assert.equal(items.length, paths.length) + items.forEach((p, i) => { + assert.deepEqual(p, paths[i]) + assert.strictEqual(p.path, paths[i].path) + assert.deepEqual(p.stats, paths[i].stats) + }) }) - }) - it('should filter if opts.filter is true based on stats', () => { - const f1 = path.join(TEST_DIR, 'dir1', 'foo.js') - const f2 = path.join(TEST_DIR, 'dir2', 'dir2_1', 'bar.js') - fs.outputFileSync(f1, 'test file 1 contents') - fs.outputFileSync(f2, 'test file 2 contents') - const paths = [ - {path: f1, stats: fs.statSync(f1)}, - {path: f2, stats: fs.statSync(f2)} - ] - const filterFunc = (i) => i.stats.isFile() && i.stats.size > 0 - const items = klawSync(TEST_DIR, {filter: filterFunc}) - assert.equal(items.length, paths.length) - items.forEach((p, i) => { - assert.deepEqual(p, paths[i]) - assert.strictEqual(p.path, paths[i].path) - assert.deepEqual(p.stats, paths[i].stats) + it('should filter based on stats', () => { + const f1 = path.join(TEST_DIR, 'dir1', 'foo.js') + const f2 = path.join(TEST_DIR, 'dir2', 'dir2_1', 'bar.js') + fs.outputFileSync(f1, 'test file 1 contents') + fs.outputFileSync(f2, 'test file 2 contents') + const paths = [ + {path: f1, stats: fs.statSync(f1)}, + {path: f2, stats: fs.statSync(f2)} + ] + const filterFunc = (i) => i.stats.isFile() && i.stats.size > 0 + const items = klawSync(TEST_DIR, {filter: filterFunc}) + assert.equal(items.length, paths.length) + items.forEach((p, i) => { + assert.deepEqual(p, paths[i]) + assert.strictEqual(p.path, paths[i].path) + assert.deepEqual(p.stats, paths[i].stats) + }) }) - }) - it('should filter if opts.filter is true based on both path and stats', () => { - const f1 = path.join(TEST_DIR, 'dir1', 'foo.js') - const f2 = path.join(TEST_DIR, 'dir2', 'dir2_1', 'bar.js') - fs.outputFileSync(f1, 'test file 1 contents') - fs.outputFileSync(f2, 'test file 2 contents') - const paths = [ - {path: f1, stats: fs.statSync(f1)}, - {path: f2, stats: fs.statSync(f2)} - ] - const filterFunc = (i) => path.extname(i.path) === '.js' && i.stats.size > 0 - const items = klawSync(TEST_DIR, {filter: filterFunc}) - assert.equal(items.length, paths.length) - items.forEach((p, i) => { - assert.deepEqual(p, paths[i]) - assert.strictEqual(p.path, paths[i].path) - assert.deepEqual(p.stats, paths[i].stats) + it('should filter based on both path and stats', () => { + const f1 = path.join(TEST_DIR, 'dir1', 'foo.js') + const f2 = path.join(TEST_DIR, 'dir2', 'dir2_1', 'bar.js') + fs.outputFileSync(f1, 'test file 1 contents') + fs.outputFileSync(f2, 'test file 2 contents') + const paths = [ + {path: f1, stats: fs.statSync(f1)}, + {path: f2, stats: fs.statSync(f2)} + ] + const filterFunc = (i) => path.extname(i.path) === '.js' && i.stats.size > 0 + const items = klawSync(TEST_DIR, {filter: filterFunc}) + assert.equal(items.length, paths.length) + items.forEach((p, i) => { + assert.deepEqual(p, paths[i]) + assert.strictEqual(p.path, paths[i].path) + assert.deepEqual(p.stats, paths[i].stats) + }) + }) + + it('should filter but not recurse if noRecursiveOnFilter is true', () => { + const dirToIgnore1 = path.join(TEST_DIR, 'node_modules') + const dirToIgnore2 = path.join(dirToIgnore1, 'somepkg') + fs.ensureDirSync(dirToIgnore2) + const paths = [ + {path: DIRS[0], stats: fs.statSync(DIRS[0])}, + {path: FILES[0], stats: fs.statSync(FILES[0])}, + {path: DIRS[1], stats: fs.statSync(DIRS[1])}, + {path: DIRS[2], stats: fs.statSync(DIRS[2])}, + {path: DIRS[3], stats: fs.statSync(DIRS[3])}, + {path: FILES[1], stats: fs.statSync(FILES[1])}, + {path: FILES[2], stats: fs.statSync(FILES[2])} + ] + const filterFunc = i => i.path.indexOf('node_modules') < 0 + const items = klawSync(TEST_DIR, {filter: filterFunc, noRecursiveOnFilter: true}) + assert.equal(items.length, paths.length) + items.forEach((p, i) => { + assert.deepEqual(p, paths[i]) + assert.strictEqual(p.path, paths[i].path) + assert.deepEqual(p.stats, paths[i].stats) + }) }) }) })