Skip to content

Commit a8c98ab

Browse files
committed
fix bug: ignore with glob pattern (closes #1), add filter option
1 parent ab77725 commit a8c98ab

File tree

2 files changed

+217
-178
lines changed

2 files changed

+217
-178
lines changed

klaw-sync.js

Lines changed: 39 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,54 @@
11
'use strict'
2-
var path = require('path')
3-
var mm = require('micromatch')
4-
var fs
2+
const path = require('path')
3+
const mm = require('micromatch')
4+
let fs
55
try {
66
fs = require('graceful-fs')
77
} catch (e) {
88
fs = require('fs')
99
}
1010

11-
function _procPath (dir, pathItem, opts, list) {
12-
var nestedPath
13-
var stat
14-
// here since dir already resolved, we use string concatenation
15-
// which showed faster performance than path.join() and path.resolve()
16-
if (path.sep === '/') {
17-
nestedPath = dir + '/' + pathItem
18-
} else {
19-
nestedPath = dir + '\\' + pathItem
20-
}
21-
stat = fs.lstatSync(nestedPath)
22-
if (stat.isDirectory()) {
23-
if (!opts.nodir) {
24-
list.push({path: nestedPath, stats: stat})
25-
}
26-
list = walkSync(nestedPath, opts, list)
27-
} else {
28-
if (!opts.nofile) {
29-
list.push({path: nestedPath, stats: stat})
11+
function klawSync (dir, opts, ls) {
12+
function procPath (pathItem) {
13+
const stat = fs.lstatSync(pathItem)
14+
const item = {path: pathItem, stats: stat}
15+
if (stat.isDirectory()) {
16+
if (!opts.nodir) {
17+
if (opts.ignore) {
18+
if (mm(pathItem, opts.ignore).length === 0) ls.push(item)
19+
} else if (opts.filter) {
20+
if (opts.filter(item)) ls.push(item)
21+
} else {
22+
ls.push(item)
23+
}
24+
}
25+
ls = klawSync(pathItem, opts, ls)
26+
} else {
27+
if (!opts.nofile) {
28+
if (opts.ignore) {
29+
if (mm(pathItem, opts.ignore).length === 0) ls.push(item)
30+
} else if (opts.filter) {
31+
if (opts.filter(item)) ls.push(item)
32+
} else {
33+
ls.push(item)
34+
}
35+
}
3036
}
3137
}
32-
}
3338

34-
function walkSync (dir, opts, list) {
35-
var files
36-
var ignore = []
3739
opts = opts || {}
38-
list = list || []
40+
ls = ls || []
3941
dir = path.resolve(dir)
40-
try {
41-
files = fs.readdirSync(dir)
42-
if (opts.ignore) {
43-
ignore = mm(files, opts.ignore)
44-
}
45-
} catch (er) {
46-
throw er
47-
}
48-
49-
for (var i = 0; i < files.length; i += 1) {
50-
var file = files[i]
51-
if (ignore.length > 0) {
52-
if (ignore.indexOf(file) === -1) _procPath(dir, file, opts, list)
53-
} else {
54-
_procPath(dir, file, opts, list)
55-
}
42+
const files = fs.readdirSync(dir)
43+
for (let i = 0; i < files.length; i += 1) {
44+
// here dir already resolved, we use string concatenation since
45+
// showed better performance than path.join() and path.resolve()
46+
let pathItem
47+
if (path.sep === '/') pathItem = dir + '/' + files[i]
48+
else pathItem = dir + '\\' + files[i]
49+
procPath(pathItem)
5650
}
57-
return list
51+
return ls
5852
}
5953

60-
module.exports = walkSync
54+
module.exports = klawSync

0 commit comments

Comments
 (0)