Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix bug: ignore with glob pattern (closes #1), add filter option
- Loading branch information
Showing
2 changed files
with
217 additions
and
178 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Oops, something went wrong.