Last active
November 2, 2016 02:24
-
-
Save ronanyeah/ee81bf11799c944b8234dbf5c0f26618 to your computer and use it in GitHub Desktop.
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
'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