Comparing version 3.0.5 to 3.1.0
35
index.js
@@ -5,3 +5,3 @@ "use strict"; | ||
const {promisify} = require("util"); | ||
const {join} = require("path"); | ||
const {join, basename} = require("path"); | ||
const multimatch = require("multimatch"); | ||
@@ -19,2 +19,3 @@ | ||
exclude: [], | ||
include: [], | ||
minimatch: { | ||
@@ -27,7 +28,18 @@ matchBase: true, | ||
function isExcluded(dir, opts) { | ||
if (!dir || !opts || !opts.exclude || !opts.exclude.length) return false; | ||
return Boolean(multimatch(dir, opts.exclude, opts.minimatch).length); | ||
function isExcluded(path, opts) { | ||
if (!opts || !opts.exclude || !opts.exclude.length) return false; | ||
return Boolean(multimatch(basename(path), opts.exclude, opts.minimatch).length); | ||
} | ||
function isIncluded(entry, opts) { | ||
if (!opts || !opts.include || !opts.include.length || entry.isDirectory()) return true; | ||
return Boolean(multimatch(entry.name, opts.include, opts.minimatch).length); | ||
} | ||
// 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) { | ||
@@ -59,2 +71,3 @@ const entry = {path, directory: dirent.isDirectory(), symlink: dirent.isSymbolicLink()}; | ||
if (isExcluded(path, opts)) continue; | ||
if (!isIncluded(entry, opts)) continue; | ||
@@ -69,5 +82,5 @@ let stats; | ||
} | ||
if (stats) results.push(build(entry, path, stats)); | ||
if (stats && canInclude(entry, opts)) results.push(build(entry, path, stats)); | ||
} else { | ||
results.push(build(entry, path)); | ||
if (canInclude(entry, opts)) results.push(build(entry, path)); | ||
} | ||
@@ -101,2 +114,3 @@ | ||
if (isExcluded(path, opts)) continue; | ||
if (!isIncluded(entry, opts)) continue; | ||
@@ -111,5 +125,5 @@ let stats; | ||
} | ||
if (stats) results.push(build(entry, path, stats)); | ||
if (stats && canInclude(entry, opts)) results.push(build(entry, path, stats)); | ||
} else { | ||
results.push(build(entry, path)); | ||
if (canInclude(entry, opts)) results.push(build(entry, path)); | ||
} | ||
@@ -142,2 +156,3 @@ | ||
if (isExcluded(path, opts)) continue; | ||
if (!isIncluded(entry, opts)) continue; | ||
@@ -152,5 +167,5 @@ let stats; | ||
} | ||
if (stats) yield build(entry, path, stats); | ||
if (stats && canInclude(entry, opts)) yield build(entry, path, stats); | ||
} else { | ||
yield build(entry, path); | ||
if (canInclude(entry, opts)) yield build(entry, path); | ||
} | ||
@@ -157,0 +172,0 @@ |
{ | ||
"name": "rrdir", | ||
"version": "3.0.5", | ||
"description": "Recursive directory crawler with a delightful API", | ||
"version": "3.1.0", | ||
"description": "Recursive directory reader with a delightful API", | ||
"author": "silverwind <me@silverwind.io>", | ||
@@ -6,0 +6,0 @@ "repository": "silverwind/rrdir", |
# 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) | ||
> Recursive directory crawler with a delightful API | ||
> Recursive directory reader with a delightful API | ||
`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. | ||
## Installation | ||
@@ -15,11 +17,11 @@ ```console | ||
const entries = await rrdir("../dir"); | ||
// => [{path: '../dir/file1', directory: false, symlink: true}] | ||
for await (const entry of rrdir.stream("dir")) { | ||
// => {path: 'dir/file', directory: false, symlink: true} | ||
} | ||
const entries = rrdir.sync("../dir"); | ||
// => [{path: '../dir/file1', directory: false, symlink: true}] | ||
const entries = await rrdir("dir"); | ||
// => [{path: 'dir/file', directory: false, symlink: true}] | ||
for await (const entry of rrdir.stream("../dir")) { | ||
// => {path: '../dir/file1', directory: false, symlink: true} | ||
} | ||
const entries = rrdir.sync("dir"); | ||
// => [{path: 'dir/file', directory: false, symlink: true}] | ||
@@ -31,6 +33,6 @@ ``` | ||
### `rrdir(dir, [options])` | ||
### `rrdir.stream(dir, [options])` | ||
### `rrdir.sync(dir, [options])` | ||
### `rrdir.stream(dir, [options])` | ||
Recursively crawls a directory for entries contained within. `rrdir` and `rrdir.sync` return an array of `entry`, `rrdir.stream` is a async iterator which yields `entry`. By default, errors while reading files will be ignored and put in `entry.err`. | ||
Recursively reader a directory for entries contained within. `rrdir` and `rrdir.sync` return an array of `entry`, `rrdir.stream` is a async iterator which yields `entry`. By default, errors while reading files will be ignored and put in `entry.err`. | ||
@@ -41,3 +43,4 @@ #### `options` | ||
- `options.followSymlinks` *boolean*: Whether to follow symlinks when `options.stats` is enabled. Default: `true`. | ||
- `options.exclude` *Array*: Path globs to exclude from the result. Default: `[]`. | ||
- `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.strict` *boolean*: Whether to throw immediately when reading an entry fails. Default: `false`. | ||
@@ -44,0 +47,0 @@ - `options.encoding` *string*: The encoding to use on `entry.path`. Default: `'utf8'`. |
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
9275
142
56