Comparing version 8.0.3 to 8.1.0
53
index.js
@@ -8,2 +8,4 @@ "use strict"; | ||
const sepBuffer = Buffer.from(sep); | ||
const defaults = { | ||
@@ -20,8 +22,8 @@ strict: false, | ||
const readDirOpts = { | ||
withFileTypes: true, | ||
}; | ||
function makePath(entry, dir) { | ||
return dir === "." ? entry.name : `${dir}${sep}${entry.name}`; | ||
function makePath(entry, dir, encoding) { | ||
if (encoding === "buffer") { | ||
return dir === "." ? entry.name : Buffer.from([...dir, ...sepBuffer, ...entry.name]); | ||
} else { | ||
return dir === "." ? entry.name : `${dir}${sep}${entry.name}`; | ||
} | ||
} | ||
@@ -45,3 +47,3 @@ | ||
const rrdir = module.exports = async function* (dir, opts = {}, {includeMatcher, excludeMatcher} = {}) { | ||
const rrdir = module.exports = async function* (dir, opts = {}, {includeMatcher, excludeMatcher, encoding} = {}) { | ||
if (includeMatcher === undefined) { | ||
@@ -51,2 +53,3 @@ opts = Object.assign({}, defaults, opts); | ||
if (/[/\\]$/.test(dir)) dir = dir.substring(0, dir.length - 1); | ||
encoding = Buffer.isBuffer(dir) ? "buffer" : undefined; | ||
} | ||
@@ -57,3 +60,3 @@ | ||
try { | ||
dirents = await readdir(dir, readDirOpts); | ||
dirents = await readdir(dir, {encoding, withFileTypes: true}); | ||
} catch (err) { | ||
@@ -69,4 +72,4 @@ if (opts.strict) { | ||
for (const dirent of dirents) { | ||
const path = makePath(dirent, dir); | ||
if (excludeMatcher && excludeMatcher(path)) continue; | ||
const path = makePath(dirent, dir, encoding); | ||
if (excludeMatcher && excludeMatcher(encoding === "buffer" ? String(path) : path)) continue; | ||
@@ -91,8 +94,8 @@ let stats; | ||
if (!includeMatcher || includeMatcher(path)) yield build(dirent, path, stats, opts); | ||
if (recurse) yield* await rrdir(path, opts, {includeMatcher, excludeMatcher}); | ||
if (!includeMatcher || includeMatcher(encoding === "buffer" ? String(path) : path)) yield build(dirent, path, stats, opts); | ||
if (recurse) yield* await rrdir(path, opts, {includeMatcher, excludeMatcher, encoding}); | ||
} | ||
}; | ||
module.exports.async = async (dir, opts = {}, {includeMatcher, excludeMatcher} = {}) => { | ||
module.exports.async = async (dir, opts = {}, {includeMatcher, excludeMatcher, encoding} = {}) => { | ||
if (includeMatcher === undefined) { | ||
@@ -102,2 +105,3 @@ opts = Object.assign({}, defaults, opts); | ||
if (/[/\\]$/.test(dir)) dir = dir.substring(0, dir.length - 1); | ||
encoding = Buffer.isBuffer(dir) ? "buffer" : undefined; | ||
} | ||
@@ -109,3 +113,3 @@ | ||
try { | ||
dirents = await readdir(dir, readDirOpts); | ||
dirents = await readdir(dir, {encoding, withFileTypes: true}); | ||
} catch (err) { | ||
@@ -121,4 +125,4 @@ if (opts.strict) { | ||
await Promise.all(dirents.map(async dirent => { | ||
const path = makePath(dirent, dir); | ||
if (excludeMatcher && excludeMatcher(path)) return; | ||
const path = makePath(dirent, dir, encoding); | ||
if (excludeMatcher && excludeMatcher(encoding === "buffer" ? String(path) : path)) return; | ||
@@ -143,4 +147,4 @@ let stats; | ||
if (!includeMatcher || includeMatcher(path)) results.push(build(dirent, path, stats, opts)); | ||
if (recurse) results.push(...await module.exports.async(path, opts, {includeMatcher, excludeMatcher})); | ||
if (!includeMatcher || includeMatcher(encoding === "buffer" ? String(path) : path)) results.push(build(dirent, path, stats, opts)); | ||
if (recurse) results.push(...await module.exports.async(path, opts, {includeMatcher, excludeMatcher, encoding})); | ||
})); | ||
@@ -151,3 +155,3 @@ | ||
module.exports.sync = (dir, opts = {}, {includeMatcher, excludeMatcher} = {}) => { | ||
module.exports.sync = (dir, opts = {}, {includeMatcher, excludeMatcher, encoding} = {}) => { | ||
if (includeMatcher === undefined) { | ||
@@ -157,2 +161,3 @@ opts = Object.assign({}, defaults, opts); | ||
if (/[/\\]$/.test(dir)) dir = dir.substring(0, dir.length - 1); | ||
encoding = Buffer.isBuffer(dir) ? "buffer" : undefined; | ||
} | ||
@@ -164,3 +169,3 @@ | ||
try { | ||
dirents = readdirSync(dir, readDirOpts); | ||
dirents = readdirSync(dir, {encoding, withFileTypes: true}); | ||
} catch (err) { | ||
@@ -176,4 +181,4 @@ if (opts.strict) { | ||
for (const dirent of dirents) { | ||
const path = makePath(dirent, dir); | ||
if (excludeMatcher && excludeMatcher(path)) continue; | ||
const path = makePath(dirent, dir, encoding); | ||
if (excludeMatcher && excludeMatcher(encoding === "buffer" ? String(path) : path)) continue; | ||
@@ -198,4 +203,4 @@ let stats; | ||
if (!includeMatcher || includeMatcher(path)) results.push(build(dirent, path, stats, opts)); | ||
if (recurse) results.push(...module.exports.sync(path, opts, {includeMatcher, excludeMatcher})); | ||
if (!includeMatcher || includeMatcher(encoding === "buffer" ? String(path) : path)) results.push(build(dirent, path, stats, opts)); | ||
if (recurse) results.push(...module.exports.sync(path, opts, {includeMatcher, excludeMatcher, encoding})); | ||
} | ||
@@ -202,0 +207,0 @@ |
{ | ||
"name": "rrdir", | ||
"version": "8.0.3", | ||
"version": "8.1.0", | ||
"description": "Recursive directory reader with a delightful API", | ||
@@ -26,2 +26,3 @@ "author": "silverwind <me@silverwind.io>", | ||
"jest": "26.0.1", | ||
"semver": "7.3.2", | ||
"tempy": "0.5.0", | ||
@@ -28,0 +29,0 @@ "updates": "10.2.11", |
@@ -37,2 +37,6 @@ # rrdir | ||
#### `dir` *String* | *Buffer* | ||
The directory to read. If you pass a `Buffer` here, `entry.path` will be a Buffer as well. | ||
#### `options` *Object* | ||
@@ -39,0 +43,0 @@ |
10544
164
59
8