recursive-dir-reader
Advanced tools
Comparing version 0.0.1 to 1.0.0
54
index.js
@@ -11,24 +11,28 @@ 'use strict'; | ||
*/ | ||
function async(dir, callback) { | ||
function async(dir, callback = undefined) { | ||
const paths = []; | ||
fs.readdir(dir, (err, items) => { | ||
if (err) throw console.error(err); | ||
startReading(dir, callback); | ||
each(items, itemName => { | ||
const path = `${dir}/${itemName}`; | ||
function startReading(dir, callback) { | ||
fs.readdir(dir, (err, items) => { | ||
if (err) throw console.error(err); | ||
fs.stat(path, (err, stat) => { | ||
if (err) throw console.error(err); | ||
each(items, itemName => { | ||
const path = `${dir}/${itemName}`; | ||
if (stat.isDirectory()) async(path, callback); | ||
fs.stat(path, (err, stat) => { | ||
if (err) throw console.error(err); | ||
else { | ||
if (callback) callback(path); | ||
if (stat.isDirectory()) startReading(path, callback); | ||
paths.push(path); | ||
} | ||
else { | ||
if (callback) callback(path); | ||
paths.push(path); | ||
} | ||
}); | ||
}); | ||
}); | ||
}); | ||
} | ||
@@ -38,5 +42,19 @@ return paths; | ||
function sync(dir, callback) { | ||
/** | ||
* Synchronous Get All Files In Directory & Subdirectories | ||
* @param {string} dir - A path to a directory | ||
* @param {function|undefined} callback - Calls the function one time for each item in the folder. | ||
*/ | ||
function sync(dir, callback = undefined) { | ||
const paths = []; | ||
try { | ||
const paths = []; | ||
startReading(dir, callback); | ||
} catch (err) { | ||
throw console.error(err); | ||
} | ||
return paths; | ||
function startReading(dir, callback) { | ||
const items = fs.readdirSync(dir); | ||
@@ -48,3 +66,3 @@ | ||
if (isDirectory) sync(path, callback); | ||
if (isDirectory) startReading(path, callback); | ||
@@ -57,6 +75,2 @@ else { | ||
}); | ||
return paths; | ||
} catch (err) { | ||
throw console.error(err); | ||
} | ||
@@ -63,0 +77,0 @@ } |
{ | ||
"name": "recursive-dir-reader", | ||
"version": "0.0.1", | ||
"version": "1.0.0", | ||
"description": "reading directory and subdirectories", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Sorry, the diff of this file is not supported yet
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
16853
9
56
0
1
65