Created
December 22, 2019 05:32
-
-
Save MohiuddinAkib/ebd0170e4d9e55cad3704f8505a6a262 to your computer and use it in GitHub Desktop.
Function to get an array of file paths nested inside a directory
This file contains hidden or 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 fs = require("fs"); | |
const path = require("path"); | |
const controllersDir = path.join(__dirname, "target_dir"); | |
const files = fs.readdirSync(controllersDir); | |
const getFiles = files => | |
files.reduce((acc, file) => { | |
const filePath = path.join(controllersDir, file); | |
const dirent = fs.lstatSync(filePath); | |
let fileToReturn; | |
if (dirent.isFile()) { | |
fileToReturn = filePath; | |
} else { | |
const files = fs.readdirSync(filePath).map(f => `${file}/${f}`); | |
fileToReturn = getFiles(files); | |
} | |
return acc.concat(fileToReturn); | |
}, []); | |
const newFiles = getFiles(files); | |
console.log(newFiles); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment