Comparing version 5.0.0 to 6.0.0
75
index.js
@@ -9,3 +9,2 @@ "use strict"; | ||
const defaults = { | ||
encoding: "utf8", | ||
strict: false, | ||
@@ -21,2 +20,6 @@ stats: false, | ||
const readDirOpts = { | ||
withFileTypes: true, | ||
}; | ||
function isExcluded(path, matcher) { | ||
@@ -32,8 +35,2 @@ if (!matcher) return false; | ||
// when a include pattern is specified, stop yielding directories | ||
function canInclude(entry, opts) { | ||
if (!opts.include || !opts.include.length) return true; | ||
return !entry.isDirectory(); | ||
} | ||
function build(dirent, path, stats) { | ||
@@ -45,21 +42,15 @@ const entry = {path, directory: dirent.isDirectory(), symlink: dirent.isSymbolicLink()}; | ||
function makeMatchers(opts) { | ||
let includeMatcher = null; | ||
let excludeMatcher = null; | ||
function makeMatchers({include, exclude, match}) { | ||
return { | ||
includeMatcher: (include && include.length) ? picomatch(include, match) : null, | ||
excludeMatcher: (exclude && exclude.length) ? picomatch(exclude, match) : null, | ||
}; | ||
} | ||
if (opts && opts.include && opts.include.length) { | ||
includeMatcher = picomatch(opts.include, opts.match); | ||
const rrdir = module.exports = async (dir, opts = {}, {includeMatcher, excludeMatcher} = {}) => { | ||
if (includeMatcher === undefined) { | ||
opts = Object.assign({}, defaults, opts); | ||
({includeMatcher, excludeMatcher} = makeMatchers(opts)); | ||
} | ||
if (opts && opts.exclude && opts.exclude.length) { | ||
excludeMatcher = picomatch(opts.exclude, opts.match); | ||
} | ||
return {includeMatcher, excludeMatcher}; | ||
} | ||
const rrdir = module.exports = async (dir, opts) => { | ||
const {includeMatcher, excludeMatcher} = makeMatchers(opts); | ||
if (isExcluded(dir, excludeMatcher)) return []; | ||
opts = Object.assign({}, defaults, opts); | ||
const results = []; | ||
@@ -69,3 +60,3 @@ let entries = []; | ||
try { | ||
entries = await readdir(dir, {encoding: opts.encoding, withFileTypes: true}); | ||
entries = await readdir(dir, readDirOpts); | ||
} catch (err) { | ||
@@ -93,7 +84,5 @@ if (opts.strict) { | ||
} | ||
if (stats && canInclude(entry, opts)) results.push(build(entry, path, stats)); | ||
} else { | ||
if (canInclude(entry, opts)) results.push(build(entry, path)); | ||
} | ||
results.push(build(entry, path, stats)); | ||
if (entry.isDirectory()) results.push(...await rrdir(path, opts)); | ||
@@ -105,6 +94,8 @@ } | ||
module.exports.sync = (dir, opts) => { | ||
const {includeMatcher, excludeMatcher} = makeMatchers(opts); | ||
if (isExcluded(dir, excludeMatcher)) return []; | ||
opts = Object.assign({}, defaults, opts); | ||
rrdir.sync = module.exports.sync = (dir, opts = {}, {includeMatcher, excludeMatcher} = {}) => { | ||
if (includeMatcher === undefined) { | ||
opts = Object.assign({}, defaults, opts); | ||
({includeMatcher, excludeMatcher} = makeMatchers(opts)); | ||
} | ||
const results = []; | ||
@@ -114,3 +105,3 @@ let entries = []; | ||
try { | ||
entries = readdirSync(dir, {encoding: opts.encoding, withFileTypes: true}); | ||
entries = readdirSync(dir, readDirOpts); | ||
} catch (err) { | ||
@@ -138,7 +129,5 @@ if (opts.strict) { | ||
} | ||
if (stats && canInclude(entry, opts)) results.push(build(entry, path, stats)); | ||
} else { | ||
if (canInclude(entry, opts)) results.push(build(entry, path)); | ||
} | ||
results.push(build(entry, path, stats)); | ||
if (entry.isDirectory()) results.push(...rrdir.sync(path, opts)); | ||
@@ -150,10 +139,12 @@ } | ||
module.exports.stream = async function* (dir, opts) { | ||
const {includeMatcher, excludeMatcher} = makeMatchers(opts); | ||
if (isExcluded(dir, excludeMatcher)) return; | ||
opts = Object.assign({}, defaults, opts); | ||
rrdir.stream = module.exports.stream = async function* (dir, opts = {}, {includeMatcher, excludeMatcher} = {}) { | ||
if (includeMatcher === undefined) { | ||
opts = Object.assign({}, defaults, opts); | ||
({includeMatcher, excludeMatcher} = makeMatchers(opts)); | ||
} | ||
let entries = []; | ||
try { | ||
entries = await readdir(dir, {encoding: opts.encoding, withFileTypes: true}); | ||
entries = await readdir(dir, readDirOpts); | ||
} catch (err) { | ||
@@ -181,9 +172,7 @@ if (opts.strict) { | ||
} | ||
if (stats && canInclude(entry, opts)) yield build(entry, path, stats); | ||
} else { | ||
if (canInclude(entry, opts)) yield build(entry, path); | ||
} | ||
yield build(entry, path, stats); | ||
if (entry.isDirectory()) yield* await rrdir.stream(path, opts); | ||
} | ||
}; |
{ | ||
"name": "rrdir", | ||
"version": "5.0.0", | ||
"version": "6.0.0", | ||
"description": "Recursive directory reader with a delightful API", | ||
@@ -8,2 +8,3 @@ "author": "silverwind <me@silverwind.io>", | ||
"license": "BSD-2-Clause", | ||
"main": "index.js", | ||
"scripts": { | ||
@@ -23,5 +24,7 @@ "test": "make test" | ||
"eslint": "6.8.0", | ||
"eslint-config-silverwind": "7.1.0", | ||
"updates": "9.3.3", | ||
"versions": "7.0.5" | ||
"eslint-config-silverwind": "10.0.1", | ||
"jest": "25.1.0", | ||
"tempy": "0.5.0", | ||
"updates": "10.2.3", | ||
"versions": "8.2.5" | ||
}, | ||
@@ -38,3 +41,7 @@ "keywords": [ | ||
"scandir" | ||
] | ||
], | ||
"jest": { | ||
"verbose": false, | ||
"testTimeout": 10000 | ||
} | ||
} |
# rrdir | ||
[![](https://img.shields.io/npm/v/rrdir.svg?style=flat)](https://www.npmjs.org/package/rrdir) [![](https://img.shields.io/npm/dm/rrdir.svg)](https://www.npmjs.org/package/rrdir) [![](https://api.travis-ci.org/silverwind/rrdir.svg?style=flat)](https://travis-ci.org/silverwind/rrdir) | ||
[![](https://img.shields.io/npm/v/rrdir.svg?style=flat)](https://www.npmjs.org/package/rrdir) [![](https://img.shields.io/npm/dm/rrdir.svg)](https://www.npmjs.org/package/rrdir) | ||
@@ -41,10 +41,7 @@ > Recursive directory reader with a delightful API | ||
- `options.followSymlinks` *boolean*: Whether to follow symlinks when `options.stats` is enabled. Default: `true`. | ||
- `options.exclude` *Array*: File and directory globs to exclude. Default: `[]`. | ||
- `options.include` *Array*: File globs to include. When specified, will stop yielding directories. Default: `[]`. | ||
- `options.exclude` *Array*: Full path globs to exclude. Default: `[]`. | ||
- `options.include` *Array*: Full path globs to include. Default: `[]`. | ||
- `options.strict` *boolean*: Whether to throw immediately when reading an entry fails. Default: `false`. | ||
- `options.encoding` *string*: The encoding to use on `entry.path`. Default: `'utf8'`. | ||
- `options.match` *Object*: [picomatch options](https://github.com/micromatch/picomatch#options). Default: `{dot: true}`. | ||
`include` and `exclude` globs match against the full directory, to match individial files by name, use `**/filename` or `**/*.js`. | ||
#### `entry` | ||
@@ -51,0 +48,0 @@ |
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
8815
6
140
55