Skip to content

Instantly share code, notes, and snippets.

@pyrsmk
Last active July 8, 2022 08:43
Show Gist options
  • Save pyrsmk/5a97f0cea014e6db8cf294840f3f818f to your computer and use it in GitHub Desktop.
Save pyrsmk/5a97f0cea014e6db8cf294840f3f818f to your computer and use it in GitHub Desktop.
Read a directory recursively
import fs from 'fs'
import path from 'path'
const recursiveReaddirSync = (currentPath : string) => {
let results : Array<string> = []
fs.readdirSync(currentPath).forEach(filename => {
const filePath = path.resolve(currentPath, filename)
const stat = fs.statSync(filePath)
if (stat.isDirectory()) {
results = results.concat(
recursiveReaddirSync(filePath)
)
return
}
if (stat.isFile()) {
results.push(filePath)
return
}
})
return results
}
export default recursiveReaddirSync
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment