Skip to content

Instantly share code, notes, and snippets.

@konstantin-hatvan
Created February 16, 2020 09:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save konstantin-hatvan/219d920c457ec1e3796f2cbb2db8dd10 to your computer and use it in GitHub Desktop.
Save konstantin-hatvan/219d920c457ec1e3796f2cbb2db8dd10 to your computer and use it in GitHub Desktop.
Recursive fs.readdir with relative file paths
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