Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from manidlou/add-filter-opt
remove ignore option, add filter option
- Loading branch information
Showing
6 changed files
with
290 additions
and
266 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
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
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
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,50 @@ | ||
'use strict' | ||
var path = require('path') | ||
var mm = require('micromatch') | ||
var fs | ||
const path = require('path') | ||
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.filter) { | ||
if (opts.filter(item)) { | ||
ls.push(item) | ||
ls = klawSync(pathItem, opts, ls) | ||
} else { | ||
if (!opts.noRecurseOnFailedFilter) ls = klawSync(pathItem, opts, ls) | ||
} | ||
} else { | ||
if (!opts.nodir) ls.push(item) | ||
ls = klawSync(pathItem, opts, ls) | ||
} | ||
} else { | ||
if (opts.filter) { | ||
if (opts.filter(item)) ls.push(item) | ||
} else { | ||
if (!opts.nofile) 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 |
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
Oops, something went wrong.