Skip to content

Commit

Permalink
fix bug: ignore with glob pattern (closes #1), add filter option
Browse files Browse the repository at this point in the history
  • Loading branch information
manidlou committed Apr 21, 2017
1 parent ab77725 commit a8c98ab
Show file tree
Hide file tree
Showing 2 changed files with 217 additions and 178 deletions.
84 changes: 39 additions & 45 deletions klaw-sync.js
@@ -1,60 +1,54 @@
'use strict'
var path = require('path')
var mm = require('micromatch')
var fs
const path = require('path')
const mm = require('micromatch')
let fs
try {
fs = require('graceful-fs')
} catch (e) {
fs = require('fs')
}

function _procPath (dir, pathItem, opts, list) {
var nestedPath
var stat
// here since dir already resolved, we use string concatenation
// which showed faster performance than path.join() and path.resolve()
if (path.sep === '/') {
nestedPath = dir + '/' + pathItem
} else {
nestedPath = dir + '\\' + pathItem
}
stat = fs.lstatSync(nestedPath)
if (stat.isDirectory()) {
if (!opts.nodir) {
list.push({path: nestedPath, stats: stat})
}
list = walkSync(nestedPath, opts, list)
} else {
if (!opts.nofile) {
list.push({path: nestedPath, stats: stat})
function klawSync (dir, opts, ls) {
function procPath (pathItem) {
const stat = fs.lstatSync(pathItem)
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)
} else {
ls.push(item)
}
}
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(item)) ls.push(item)
} else {
ls.push(item)
}
}
}
}
}

function walkSync (dir, opts, list) {
var files
var ignore = []
opts = opts || {}
list = list || []
ls = ls || []
dir = path.resolve(dir)
try {
files = fs.readdirSync(dir)
if (opts.ignore) {
ignore = mm(files, opts.ignore)
}
} catch (er) {
throw er
}

for (var i = 0; i < files.length; i += 1) {
var file = files[i]
if (ignore.length > 0) {
if (ignore.indexOf(file) === -1) _procPath(dir, file, opts, list)
} else {
_procPath(dir, file, opts, list)
}
const files = fs.readdirSync(dir)
for (let i = 0; i < files.length; i += 1) {
// here dir already resolved, we use string concatenation since
// showed better performance than path.join() and path.resolve()
let pathItem
if (path.sep === '/') pathItem = dir + '/' + files[i]
else pathItem = dir + '\\' + files[i]
procPath(pathItem)
}
return list
return ls
}

module.exports = walkSync
module.exports = klawSync

0 comments on commit a8c98ab

Please sign in to comment.