Created
February 16, 2020 09:23
-
-
Save konstantin-hatvan/219d920c457ec1e3796f2cbb2db8dd10 to your computer and use it in GitHub Desktop.
Recursive fs.readdir with relative file paths
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'); | |
/* Prepend the given path segment */ | |
const prependPathSegment = pathSegment => location => path.join(pathSegment, location); | |
/* fs.readdir but with relative paths */ | |
const readdirPreserveRelativePath = location => fs.readdirSync(location).map(prependPathSegment(location)); | |
/* Recursive fs.readdir but with relative paths */ | |
const readdirRecursive = location => readdirPreserveRelativePath(location) | |
.reduce((result, currentValue) => fs.statSync(currentValue).isDirectory() | |
? result.concat(readdirRecursive(currentValue)) | |
: result.concat(currentValue), []); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment