Skip to content

Instantly share code, notes, and snippets.

@piotr-placzek
Last active December 21, 2021 18:02
Show Gist options
  • Save piotr-placzek/5a2df3d73d749260c8bb261137d51abd to your computer and use it in GitHub Desktop.
Save piotr-placzek/5a2df3d73d749260c8bb261137d51abd to your computer and use it in GitHub Desktop.
js recursive list files
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