Skip to content

Commit

Permalink
remove ignore option, remove micromatch, refactor filter option, upda…
Browse files Browse the repository at this point in the history
…te bm.js
  • Loading branch information
manidlou committed Apr 23, 2017
1 parent a8c98ab commit 739a6e3
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 99 deletions.
60 changes: 25 additions & 35 deletions benchmark/bm.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,24 @@ const klawSync = require('../klaw-sync.js')

function help () {
console.log(`Usage examples:\n`)
console.log(`npm run benchmark -- --dir=<rootdir> (basic usage without anything to ignore)`)
console.log(`npm run benchmark -- --dir=<rootdir> -i "{node_modules,.git}" (ignore node_modules and .git directories)`)
console.log(`npm run benchmark -- --dir=<rootdir> -i "node_modules" -i "*.js" (ignore node_modules and all js files)`)
console.log(`npm run benchmark -- --dir=<rootdir>`)
console.log(`npm run benchmark -- --dir=<rootdir> --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) {
Expand All @@ -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) {
Expand All @@ -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
}
19 changes: 10 additions & 9 deletions klaw-sync.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'
const path = require('path')
const mm = require('micromatch')
let fs
try {
fs = require('graceful-fs')
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
},
"homepage": "https://github.com/manidlou/node-klaw-sync#readme",
"dependencies": {
"benchmark": "^2.1.4",
"micromatch": "^2.3.11"
},
"devDependencies": {
Expand Down
136 changes: 81 additions & 55 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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])
Expand All @@ -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])
Expand All @@ -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])
Expand All @@ -168,69 +168,95 @@ 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])
assert.strictEqual(p.path, paths[i].path)
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)
})
})
})
})

0 comments on commit 739a6e3

Please sign in to comment.