recursive-readdir-async
Advanced tools
Comparing version 1.2.0 to 1.2.1
@@ -91,2 +91,3 @@ /** | ||
* @property error - The error object. The structure is variable | ||
* @property custom - Custom key to add custom properties | ||
*/ | ||
@@ -110,2 +111,4 @@ export interface IBase { | ||
error?: IError | any; | ||
/** Custom key to add custom properties */ | ||
custom?: any; | ||
} | ||
@@ -127,3 +130,3 @@ /** | ||
/** | ||
* Definition for the Item object that contains information of files used but this module | ||
* Definition for the Item object that contains information of files used in this module | ||
* @typedef IFile | ||
@@ -130,0 +133,0 @@ * @type {object} |
@@ -39,3 +39,2 @@ "use strict"; | ||
exports.list = exports.readFile = exports.stat = exports.PATH = exports.FS = exports.TREE = exports.LIST = void 0; | ||
// constants | ||
/** @readonly constant for mode LIST to be used in Options */ | ||
@@ -61,5 +60,2 @@ exports.LIST = 1; | ||
exports.PATH = _path; | ||
/* | ||
* Variables | ||
*/ | ||
let pathSimbol = '/'; | ||
@@ -120,3 +116,3 @@ /** | ||
for (const value of settings.exclude) { | ||
if (path.indexOf(value) > -1) { | ||
if (path.includes(value)) { | ||
return false; | ||
@@ -129,5 +125,20 @@ } | ||
/** | ||
* Adds optional keys to item | ||
* @param obj the item object | ||
* @param file the filename | ||
* @param settings the options configuration to use | ||
* @param deep The deep level | ||
* @returns void | ||
* @private | ||
*/ | ||
function addOptionalKeys(obj, file, settings, deep) { | ||
if (settings.extensions) { | ||
obj.extension = (exports.PATH.extname(file)).toLowerCase(); | ||
} | ||
if (settings.deep) { | ||
obj.deep = deep; | ||
} | ||
} | ||
/** | ||
* Reads content and creates a valid IBase collection | ||
* @param error Error object (if any) | ||
* @param files Files array to read in | ||
* @param rpath Path relative to | ||
@@ -141,43 +152,30 @@ * @param data Model | ||
*/ | ||
function read(error, files, rpath, data, settings, deep, resolve, reject) { | ||
/** | ||
* Adds optional keys to item | ||
* @param obj the item object | ||
* @param file the filename | ||
* @returns void | ||
* @private | ||
*/ | ||
function addOptionalKeys(obj, file) { | ||
if (settings.extensions) { | ||
obj.extension = (exports.PATH.extname(file)).toLowerCase(); | ||
function read(rpath, data, settings, deep, resolve, reject) { | ||
exports.FS.readdir(rpath, function (error, files) { | ||
// If error reject them | ||
if (error) { | ||
reject(error); | ||
} | ||
if (settings.deep) { | ||
obj.deep = deep; | ||
} | ||
} | ||
// If error reject them | ||
if (error) { | ||
reject(error); | ||
} | ||
else { | ||
const removeExt = (file) => { | ||
const extSize = exports.PATH.extname(file).length; | ||
return file.substring(0, file.length - (extSize > 0 ? extSize : 0)); | ||
}; | ||
// Iterate through elements (files and folders) | ||
for (let i = 0, tam = files.length; i < tam; i++) { | ||
const obj = { | ||
name: files[i], | ||
title: removeExt(files[i]), | ||
path: rpath, | ||
fullname: rpath + (rpath.endsWith(pathSimbol) ? '' : pathSimbol) + files[i], | ||
else { | ||
const removeExt = (file) => { | ||
const extSize = exports.PATH.extname(file).length; | ||
return file.substring(0, file.length - (extSize > 0 ? extSize : 0)); | ||
}; | ||
if (checkItem(obj.fullname, settings)) { | ||
addOptionalKeys(obj, files[i]); | ||
data.push(obj); | ||
// Iterate through elements (files and folders) | ||
for (const file of files) { | ||
const obj = { | ||
name: file, | ||
title: removeExt(file), | ||
path: rpath, | ||
fullname: rpath + (rpath.endsWith(pathSimbol) ? '' : pathSimbol) + file, | ||
}; | ||
if (checkItem(obj.fullname, settings)) { | ||
addOptionalKeys(obj, file, settings, deep); | ||
data.push(obj); | ||
} | ||
} | ||
// Finish, returning content | ||
resolve(data); | ||
} | ||
// Finish, returning content | ||
resolve(data); | ||
} | ||
}); | ||
} | ||
@@ -207,5 +205,3 @@ /** | ||
// Reading contents of path | ||
exports.FS.readdir(rpath, function (error, files) { | ||
read(error, files, rpath, data, settings, deep, resolve, reject); | ||
}); | ||
read(rpath, data, settings, deep, resolve, reject); | ||
}); | ||
@@ -230,2 +226,36 @@ } | ||
/** | ||
* Search if the fullname exist in the include array | ||
* @param fullname - The fullname of the item to search for | ||
* @param settings the options to be used | ||
* @returns true if exists | ||
*/ | ||
function exists(fullname, settings) { | ||
if (settings.include) { | ||
for (const value of settings.include) { | ||
if (fullname.includes(value)) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
/** | ||
* Removes paths that not match the include array | ||
* @param settings the options to be used | ||
* @param content items list | ||
* @returns void | ||
*/ | ||
function onlyInclude(settings, content) { | ||
if (settings.include && settings.include.length > 0) { | ||
for (let i = content.length - 1; i > -1; i--) { | ||
const item = content[i]; | ||
if (settings.mode === exports.TREE && item.isDirectory && item.content) | ||
continue; | ||
if (!exists(item.fullname, settings)) { | ||
content.splice(i, 1); | ||
} | ||
} | ||
} | ||
} | ||
/** | ||
* Returns an array of items in path | ||
@@ -252,35 +282,4 @@ * @param path path | ||
} | ||
onlyInclude(); | ||
onlyInclude(settings, content); | ||
return content; | ||
/** | ||
* Removes paths that not match the include array | ||
* @returns void | ||
*/ | ||
function onlyInclude() { | ||
/** | ||
* Search if the fullname exist in the include array | ||
* @param fullname - The fullname of the item to search for | ||
* @returns true if exists | ||
*/ | ||
function exists(fullname) { | ||
if (settings.include) { | ||
for (const value of settings.include) { | ||
if (fullname.indexOf(value) > -1) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
if (settings.include && settings.include.length > 0) { | ||
for (let i = content.length - 1; i > -1; i--) { | ||
const item = content[i]; | ||
if (settings.mode === exports.TREE && item.isDirectory && item.content) | ||
continue; | ||
if (!exists(item.fullname)) { | ||
content.splice(i, 1); | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
@@ -290,3 +289,3 @@ } | ||
* Returns an object with all items with selected options | ||
* @param list items list | ||
* @param collection items list | ||
* @param settings the options to use | ||
@@ -298,21 +297,21 @@ * @param progress callback progress | ||
*/ | ||
function statDir(list, settings, progress, deep) { | ||
function statDir(collection, settings, progress, deep) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let isOk = true; | ||
for (let i = list.length - 1; i > -1; i--) { | ||
for (let i = collection.length - 1; i > -1; i--) { | ||
try { | ||
list = yield statDirItem(list, i, settings, progress, deep); | ||
collection = yield statDirItem(collection, i, settings, progress, deep); | ||
if (progress !== undefined) { | ||
isOk = !progress(list[i], list.length - i, list.length); | ||
isOk = !progress(collection[i], collection.length - i, collection.length); | ||
} | ||
} | ||
catch (err) { | ||
list[i].error = err; | ||
collection[i].error = err; | ||
} | ||
if ((list[i].isDirectory && settings.ignoreFolders && | ||
!(list[i].content) && list[i].error === undefined) || !isOk) { | ||
list.splice(i, 1); | ||
if ((collection[i].isDirectory && settings.ignoreFolders && | ||
!(collection[i].content) && collection[i].error === undefined) || !isOk) { | ||
collection.splice(i, 1); | ||
} | ||
} | ||
return list; | ||
return collection; | ||
}); | ||
@@ -322,3 +321,3 @@ } | ||
* Returns an object with updated item information | ||
* @param list items list | ||
* @param collection items list | ||
* @param i index of item | ||
@@ -331,18 +330,18 @@ * @param settings the options to use | ||
*/ | ||
function statDirItem(list, i, settings, progress, deep) { | ||
function statDirItem(collection, i, settings, progress, deep) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const stats = yield stat(list[i].fullname); | ||
list[i].isDirectory = stats.isDirectory(); | ||
const stats = yield stat(collection[i].fullname); | ||
collection[i].isDirectory = stats.isDirectory(); | ||
if (settings.stats) { | ||
list[i].stats = stats; | ||
collection[i].stats = stats; | ||
} | ||
if (settings.readContent && !list[i].isDirectory) { | ||
list[i].data = yield readFile(list[i].fullname, settings.encoding); | ||
if (settings.readContent && !collection[i].isDirectory) { | ||
collection[i].data = yield readFile(collection[i].fullname, settings.encoding); | ||
} | ||
if (list[i].isDirectory && settings.recursive) { | ||
const item = list[i]; | ||
if (collection[i].isDirectory && settings.recursive) { | ||
const item = collection[i]; | ||
if (settings.mode === exports.LIST) { | ||
const result = yield listDir(item.fullname, settings, progress, deep + 1); | ||
if (result.length) { | ||
list = list.concat(result); | ||
collection = collection.concat(result); | ||
} | ||
@@ -357,3 +356,3 @@ } | ||
} | ||
return list; | ||
return collection; | ||
}); | ||
@@ -360,0 +359,0 @@ } |
@@ -91,2 +91,3 @@ /** | ||
* @property error - The error object. The structure is variable | ||
* @property custom - Custom key to add custom properties | ||
*/ | ||
@@ -110,2 +111,4 @@ export interface IBase { | ||
error?: IError | any; | ||
/** Custom key to add custom properties */ | ||
custom?: any; | ||
} | ||
@@ -127,3 +130,3 @@ /** | ||
/** | ||
* Definition for the Item object that contains information of files used but this module | ||
* Definition for the Item object that contains information of files used in this module | ||
* @typedef IFile | ||
@@ -130,0 +133,0 @@ * @type {object} |
@@ -17,3 +17,2 @@ /** | ||
}; | ||
// constants | ||
/** @readonly constant for mode LIST to be used in Options */ | ||
@@ -39,5 +38,2 @@ export const LIST = 1; | ||
export const PATH = _path; | ||
/* | ||
* Variables | ||
*/ | ||
let pathSimbol = '/'; | ||
@@ -96,3 +92,3 @@ /** | ||
for (const value of settings.exclude) { | ||
if (path.indexOf(value) > -1) { | ||
if (path.includes(value)) { | ||
return false; | ||
@@ -105,5 +101,20 @@ } | ||
/** | ||
* Adds optional keys to item | ||
* @param obj the item object | ||
* @param file the filename | ||
* @param settings the options configuration to use | ||
* @param deep The deep level | ||
* @returns void | ||
* @private | ||
*/ | ||
function addOptionalKeys(obj, file, settings, deep) { | ||
if (settings.extensions) { | ||
obj.extension = (PATH.extname(file)).toLowerCase(); | ||
} | ||
if (settings.deep) { | ||
obj.deep = deep; | ||
} | ||
} | ||
/** | ||
* Reads content and creates a valid IBase collection | ||
* @param error Error object (if any) | ||
* @param files Files array to read in | ||
* @param rpath Path relative to | ||
@@ -117,43 +128,30 @@ * @param data Model | ||
*/ | ||
function read(error, files, rpath, data, settings, deep, resolve, reject) { | ||
/** | ||
* Adds optional keys to item | ||
* @param obj the item object | ||
* @param file the filename | ||
* @returns void | ||
* @private | ||
*/ | ||
function addOptionalKeys(obj, file) { | ||
if (settings.extensions) { | ||
obj.extension = (PATH.extname(file)).toLowerCase(); | ||
function read(rpath, data, settings, deep, resolve, reject) { | ||
FS.readdir(rpath, function (error, files) { | ||
// If error reject them | ||
if (error) { | ||
reject(error); | ||
} | ||
if (settings.deep) { | ||
obj.deep = deep; | ||
} | ||
} | ||
// If error reject them | ||
if (error) { | ||
reject(error); | ||
} | ||
else { | ||
const removeExt = (file) => { | ||
const extSize = PATH.extname(file).length; | ||
return file.substring(0, file.length - (extSize > 0 ? extSize : 0)); | ||
}; | ||
// Iterate through elements (files and folders) | ||
for (let i = 0, tam = files.length; i < tam; i++) { | ||
const obj = { | ||
name: files[i], | ||
title: removeExt(files[i]), | ||
path: rpath, | ||
fullname: rpath + (rpath.endsWith(pathSimbol) ? '' : pathSimbol) + files[i], | ||
else { | ||
const removeExt = (file) => { | ||
const extSize = PATH.extname(file).length; | ||
return file.substring(0, file.length - (extSize > 0 ? extSize : 0)); | ||
}; | ||
if (checkItem(obj.fullname, settings)) { | ||
addOptionalKeys(obj, files[i]); | ||
data.push(obj); | ||
// Iterate through elements (files and folders) | ||
for (const file of files) { | ||
const obj = { | ||
name: file, | ||
title: removeExt(file), | ||
path: rpath, | ||
fullname: rpath + (rpath.endsWith(pathSimbol) ? '' : pathSimbol) + file, | ||
}; | ||
if (checkItem(obj.fullname, settings)) { | ||
addOptionalKeys(obj, file, settings, deep); | ||
data.push(obj); | ||
} | ||
} | ||
// Finish, returning content | ||
resolve(data); | ||
} | ||
// Finish, returning content | ||
resolve(data); | ||
} | ||
}); | ||
} | ||
@@ -183,5 +181,3 @@ /** | ||
// Reading contents of path | ||
FS.readdir(rpath, function (error, files) { | ||
read(error, files, rpath, data, settings, deep, resolve, reject); | ||
}); | ||
read(rpath, data, settings, deep, resolve, reject); | ||
}); | ||
@@ -206,2 +202,36 @@ } | ||
/** | ||
* Search if the fullname exist in the include array | ||
* @param fullname - The fullname of the item to search for | ||
* @param settings the options to be used | ||
* @returns true if exists | ||
*/ | ||
function exists(fullname, settings) { | ||
if (settings.include) { | ||
for (const value of settings.include) { | ||
if (fullname.includes(value)) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
/** | ||
* Removes paths that not match the include array | ||
* @param settings the options to be used | ||
* @param content items list | ||
* @returns void | ||
*/ | ||
function onlyInclude(settings, content) { | ||
if (settings.include && settings.include.length > 0) { | ||
for (let i = content.length - 1; i > -1; i--) { | ||
const item = content[i]; | ||
if (settings.mode === TREE && item.isDirectory && item.content) | ||
continue; | ||
if (!exists(item.fullname, settings)) { | ||
content.splice(i, 1); | ||
} | ||
} | ||
} | ||
} | ||
/** | ||
* Returns an array of items in path | ||
@@ -228,35 +258,4 @@ * @param path path | ||
} | ||
onlyInclude(); | ||
onlyInclude(settings, content); | ||
return content; | ||
/** | ||
* Removes paths that not match the include array | ||
* @returns void | ||
*/ | ||
function onlyInclude() { | ||
/** | ||
* Search if the fullname exist in the include array | ||
* @param fullname - The fullname of the item to search for | ||
* @returns true if exists | ||
*/ | ||
function exists(fullname) { | ||
if (settings.include) { | ||
for (const value of settings.include) { | ||
if (fullname.indexOf(value) > -1) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
if (settings.include && settings.include.length > 0) { | ||
for (let i = content.length - 1; i > -1; i--) { | ||
const item = content[i]; | ||
if (settings.mode === TREE && item.isDirectory && item.content) | ||
continue; | ||
if (!exists(item.fullname)) { | ||
content.splice(i, 1); | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
@@ -266,3 +265,3 @@ } | ||
* Returns an object with all items with selected options | ||
* @param list items list | ||
* @param collection items list | ||
* @param settings the options to use | ||
@@ -274,21 +273,21 @@ * @param progress callback progress | ||
*/ | ||
function statDir(list, settings, progress, deep) { | ||
function statDir(collection, settings, progress, deep) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let isOk = true; | ||
for (let i = list.length - 1; i > -1; i--) { | ||
for (let i = collection.length - 1; i > -1; i--) { | ||
try { | ||
list = yield statDirItem(list, i, settings, progress, deep); | ||
collection = yield statDirItem(collection, i, settings, progress, deep); | ||
if (progress !== undefined) { | ||
isOk = !progress(list[i], list.length - i, list.length); | ||
isOk = !progress(collection[i], collection.length - i, collection.length); | ||
} | ||
} | ||
catch (err) { | ||
list[i].error = err; | ||
collection[i].error = err; | ||
} | ||
if ((list[i].isDirectory && settings.ignoreFolders && | ||
!(list[i].content) && list[i].error === undefined) || !isOk) { | ||
list.splice(i, 1); | ||
if ((collection[i].isDirectory && settings.ignoreFolders && | ||
!(collection[i].content) && collection[i].error === undefined) || !isOk) { | ||
collection.splice(i, 1); | ||
} | ||
} | ||
return list; | ||
return collection; | ||
}); | ||
@@ -298,3 +297,3 @@ } | ||
* Returns an object with updated item information | ||
* @param list items list | ||
* @param collection items list | ||
* @param i index of item | ||
@@ -307,18 +306,18 @@ * @param settings the options to use | ||
*/ | ||
function statDirItem(list, i, settings, progress, deep) { | ||
function statDirItem(collection, i, settings, progress, deep) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const stats = yield stat(list[i].fullname); | ||
list[i].isDirectory = stats.isDirectory(); | ||
const stats = yield stat(collection[i].fullname); | ||
collection[i].isDirectory = stats.isDirectory(); | ||
if (settings.stats) { | ||
list[i].stats = stats; | ||
collection[i].stats = stats; | ||
} | ||
if (settings.readContent && !list[i].isDirectory) { | ||
list[i].data = yield readFile(list[i].fullname, settings.encoding); | ||
if (settings.readContent && !collection[i].isDirectory) { | ||
collection[i].data = yield readFile(collection[i].fullname, settings.encoding); | ||
} | ||
if (list[i].isDirectory && settings.recursive) { | ||
const item = list[i]; | ||
if (collection[i].isDirectory && settings.recursive) { | ||
const item = collection[i]; | ||
if (settings.mode === LIST) { | ||
const result = yield listDir(item.fullname, settings, progress, deep + 1); | ||
if (result.length) { | ||
list = list.concat(result); | ||
collection = collection.concat(result); | ||
} | ||
@@ -333,3 +332,3 @@ } | ||
} | ||
return list; | ||
return collection; | ||
}); | ||
@@ -336,0 +335,0 @@ } |
{ | ||
"name": "recursive-readdir-async", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"description": "Module to recursive read directory async (non blocking). Must be used with Promises. Configurable, extended filtering. etc.", | ||
@@ -5,0 +5,0 @@ "main": "./lib/cjs/index.js", |
@@ -15,2 +15,3 @@ [![CI Build](https://github.com/m0rtadelo/recursive-readdir-async/actions/workflows/ci.pages.yaml/badge.svg)](https://github.com/m0rtadelo/recursive-readdir-async/actions/workflows/ci.pages.yaml) | ||
>Compatible with Javascript and Typescript projects (with types) | ||
## Installation | ||
@@ -64,2 +65,3 @@ For normal usage into a project, you must install as a NPM dependency. The next command will do all the work: | ||
console.log(`${index} of ${total} ${obj.path}`) | ||
obj.custom = { foo: 'bar' };// use custom key to inject custom properties | ||
if(obj.name=="folder2") | ||
@@ -196,2 +198,4 @@ return true;// return true to delete item from the result array | ||
>`data` only exists if `options.readContent` is `true` | ||
>`custom` only exists if includes custom properties | ||
## Errors handling | ||
@@ -223,3 +227,3 @@ All errors will be added to the returned object. If an error occurs on the main call, the error will be returned like this: | ||
} | ||
} | ||
}, | ||
{ | ||
@@ -226,0 +230,0 @@ "name":"file.txt", |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
63823
1203
236
0