Last active
December 21, 2021 18:02
-
-
Save piotr-placzek/5a2df3d73d749260c8bb261137d51abd to your computer and use it in GitHub Desktop.
js recursive list files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const path = require('path'); | |
const fs = require('fs-extra'); | |
/** | |
* @param {String} rootDirectory | |
* @param {String[]} exclude | |
* @returns {String[]} | |
*/ | |
function tree(rootDirectory, exclude) { | |
const getAllFiles = function (dirPath, exclude, arrayOfFiles) { | |
files = fs.readdirSync(dirPath); | |
arrayOfFiles = arrayOfFiles || []; | |
files.forEach(function (file) { | |
if (!exclude.includes(file)) { | |
if (fs.statSync(dirPath + '/' + file).isDirectory()) { | |
arrayOfFiles = getAllFiles(dirPath + '/' + file, exclude, arrayOfFiles); | |
} else if (!file.endsWith('.json')) { | |
arrayOfFiles.push(path.join(__dirname, dirPath, '/', file)); | |
} | |
} | |
}); | |
return arrayOfFiles; | |
}; | |
const paths = getAllFiles(rootDirectory, exclude); | |
return paths; | |
} | |
module.exports = tree; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment