Skip to content

Instantly share code, notes, and snippets.

@JayWood
Created June 29, 2021 13:19
Show Gist options
  • Save JayWood/50e265e72c0f4129077b0d7cbfc6d834 to your computer and use it in GitHub Desktop.
Save JayWood/50e265e72c0f4129077b0d7cbfc6d834 to your computer and use it in GitHub Desktop.
A function to recursively get all files in an array.
/**
* Gets all files as the function says.
*
* @param {string} dirPath
* @param {string[]} arrayOfFiles
* @returns {*|string[]}
*/
const getAllFiles = function(dirPath, arrayOfFiles) {
const files = fs.readdirSync(dirPath)
arrayOfFiles = arrayOfFiles || []
files.forEach(function(file) {
if (fs.statSync(dirPath + "/" + file).isDirectory()) {
arrayOfFiles = getAllFiles(dirPath + "/" + file, arrayOfFiles)
} else {
// Get only PHP and CSS, minus the .min files
if ( ! file.includes( '.min' ) && extensions.includes( path.extname( file ) ) ) {
arrayOfFiles.push(path.join(__dirname, dirPath, "/", file))
}
}
})
return arrayOfFiles
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment