Skip to content

Instantly share code, notes, and snippets.

@ronanyeah
Last active November 2, 2016 02:24
Show Gist options
  • Save ronanyeah/ee81bf11799c944b8234dbf5c0f26618 to your computer and use it in GitHub Desktop.
Save ronanyeah/ee81bf11799c944b8234dbf5c0f26618 to your computer and use it in GitHub Desktop.
'use strict'
const fs = require('fs')
// Flattens a nested array recursively.
const flatten = arr =>
arr.filter( Array.isArray ).length
? flatten( Array.prototype.concat( ...arr ) )
: arr
// Walks through directories and files, nesting arrays to represent
// folder nesting.
const walk = root =>
fs.readdirSync(root)
.map(
file =>
fs.statSync(`${root}/${file}`).isDirectory()
? walk(`${root}/${file}`)
: `${root}/${file}`
)
// Takes a folder path and returns the paths
// to any files underneath that folder.
module.exports = path =>
flatten( walk(path) )
.map( str => str.substring(path.length) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment