Comparing version 3.2.1 to 4.0.0
75
index.js
"use strict"; | ||
const fs = require("fs"); | ||
const {promisify} = require("util"); | ||
const {readdir, stat, lstat} = require("fs").promises; | ||
const {readdirSync, statSync, lstatSync} = require("fs"); | ||
const {join, basename} = require("path"); | ||
const minimatch = require("minimatch"); | ||
const picomatch = require("picomatch"); | ||
const readdir = promisify(fs.readdir); | ||
const stat = promisify(fs.stat); | ||
const lstat = promisify(fs.lstat); | ||
const defaults = { | ||
@@ -19,3 +15,3 @@ encoding: "utf8", | ||
include: [], | ||
minimatch: { | ||
match: { | ||
dot: true, | ||
@@ -25,21 +21,10 @@ } | ||
function isExcluded(path, opts) { | ||
if (!opts || !opts.exclude || !opts.exclude.length) return false; | ||
const name = basename(path); | ||
for (const pattern of opts.exclude) { | ||
if (minimatch(name, pattern, opts.minimatch)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
function isExcluded(path, matcher) { | ||
if (!matcher) return false; | ||
return matcher(basename(path)); | ||
} | ||
function isIncluded(entry, opts) { | ||
if (!opts || !opts.include || !opts.include.length || entry.isDirectory()) return true; | ||
for (const pattern of opts.include) { | ||
if (minimatch(entry.name, pattern, opts.minimatch)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
function isIncluded(entry, matcher) { | ||
if (!matcher || entry.isDirectory()) return true; | ||
return matcher(entry.name); | ||
} | ||
@@ -59,4 +44,20 @@ | ||
function makeMatchers(opts) { | ||
let includeMatcher = null; | ||
let excludeMatcher = null; | ||
if (opts && opts.include && opts.include.length) { | ||
includeMatcher = picomatch(opts.include, opts.match); | ||
} | ||
if (opts && opts.exclude && opts.exclude.length) { | ||
excludeMatcher = picomatch(opts.exclude, opts.match); | ||
} | ||
return {includeMatcher, excludeMatcher}; | ||
} | ||
const rrdir = module.exports = async (dir, opts) => { | ||
if (isExcluded(dir, opts)) return []; | ||
const {includeMatcher, excludeMatcher} = makeMatchers(opts); | ||
if (isExcluded(dir, excludeMatcher)) return []; | ||
opts = Object.assign({}, defaults, opts); | ||
@@ -79,4 +80,4 @@ const results = []; | ||
const path = join(dir, entry.name); | ||
if (isExcluded(path, opts)) continue; | ||
if (!isIncluded(entry, opts)) continue; | ||
if (isExcluded(path, excludeMatcher)) continue; | ||
if (!isIncluded(entry, includeMatcher)) continue; | ||
@@ -103,3 +104,4 @@ let stats; | ||
module.exports.sync = (dir, opts) => { | ||
if (isExcluded(dir, opts)) return []; | ||
const {includeMatcher, excludeMatcher} = makeMatchers(opts); | ||
if (isExcluded(dir, excludeMatcher)) return []; | ||
opts = Object.assign({}, defaults, opts); | ||
@@ -110,3 +112,3 @@ const results = []; | ||
try { | ||
entries = fs.readdirSync(dir, {encoding: opts.encoding, withFileTypes: true}); | ||
entries = readdirSync(dir, {encoding: opts.encoding, withFileTypes: true}); | ||
} catch (err) { | ||
@@ -123,4 +125,4 @@ if (opts.strict) { | ||
const path = join(dir, entry.name); | ||
if (isExcluded(path, opts)) continue; | ||
if (!isIncluded(entry, opts)) continue; | ||
if (isExcluded(path, excludeMatcher)) continue; | ||
if (!isIncluded(entry, includeMatcher)) continue; | ||
@@ -130,3 +132,3 @@ let stats; | ||
try { | ||
stats = opts.followSymlinks ? fs.statSync(path) : fs.lstatSync(path); | ||
stats = opts.followSymlinks ? statSync(path) : lstatSync(path); | ||
} catch (err) { | ||
@@ -148,3 +150,4 @@ if (opts.strict) throw err; | ||
module.exports.stream = async function* (dir, opts) { | ||
if (isExcluded(dir, opts)) return; | ||
const {includeMatcher, excludeMatcher} = makeMatchers(opts); | ||
if (isExcluded(dir, excludeMatcher)) return; | ||
opts = Object.assign({}, defaults, opts); | ||
@@ -166,4 +169,4 @@ let entries = []; | ||
const path = join(dir, entry.name); | ||
if (isExcluded(path, opts)) continue; | ||
if (!isIncluded(entry, opts)) continue; | ||
if (isExcluded(path, excludeMatcher)) continue; | ||
if (!isIncluded(entry, includeMatcher)) continue; | ||
@@ -170,0 +173,0 @@ let stats; |
{ | ||
"name": "rrdir", | ||
"version": "3.2.1", | ||
"version": "4.0.0", | ||
"description": "Recursive directory reader with a delightful API", | ||
@@ -18,9 +18,9 @@ "author": "silverwind <me@silverwind.io>", | ||
"dependencies": { | ||
"minimatch": "^3.0.4" | ||
"picomatch": "^2.1.1" | ||
}, | ||
"devDependencies": { | ||
"eslint": "6.6.0", | ||
"eslint-config-silverwind": "5.0.0", | ||
"updates": "9.1.0", | ||
"versions": "7.0.2" | ||
"eslint": "6.7.2", | ||
"eslint-config-silverwind": "5.1.1", | ||
"updates": "9.3.3", | ||
"versions": "7.0.5" | ||
}, | ||
@@ -27,0 +27,0 @@ "keywords": [ |
@@ -6,3 +6,3 @@ # rrdir | ||
`rrdir` recursively reads a directory and returns entries within via an async iterator or array. Memory usage is `O(1)` for the iterator and `O(n)` for the array variants. It takes advantage of Node.js `withFileTypes` for increased performance and can typically iterate millions of files in a matter of seconds on a fast disk. | ||
`rrdir` recursively reads a directory and returns entries within via an async iterator or array. It has minimal dependencies and can typically iterate millions of files in a matter of seconds. Memory usage is `O(1)` for the iterator and `O(n)` for the array variants. | ||
@@ -46,3 +46,3 @@ ## Installation | ||
- `options.encoding` *string*: The encoding to use on `entry.path`. Default: `'utf8'`. | ||
- `options.minimatch` *Object*: [minimatch options](https://github.com/isaacs/minimatch#options). Default: `{dot: true}`. | ||
- `options.match` *Object*: [picomatch options](https://github.com/micromatch/picomatch#options). Default: `{dot: true}`. | ||
@@ -49,0 +49,0 @@ #### `entry` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9544
3
+ Addedpicomatch@^2.1.1
+ Addedpicomatch@2.3.1(transitive)
- Removedminimatch@^3.0.4
- Removedbalanced-match@1.0.2(transitive)
- Removedbrace-expansion@1.1.11(transitive)
- Removedconcat-map@0.0.1(transitive)
- Removedminimatch@3.1.2(transitive)