-1

I'm trying to make function which takes as an argument directory string (for example: 'public/css') parse that directory and subdirectories and returns an array of file paths relative to the project directory.

bashkovpd
  • 454
  • 1
  • 7
  • 20

2 Answers2

1

You can use this concept of getting directories from Get all directories within directory nodejs

const fs = require('fs')
const path = require('path')

function getDirectories (srcpath) {
  return fs.readdirSync(srcpath)
    .filter((file) => {
        fs.lstatSync(path.join(srcpath, file)).isDirectory())
}

as you can see you have for each folder the option to read the content of it with the readdirSync function.

I hope it gives you a better idea of file system in node and helps you to move forward

Community
  • 1
  • 1
ShukiB
  • 86
  • 1
  • 7
-1

OK, relying on the code provided by ShukiB my function looks like this:

const flatten = arr => arr.reduce((acc, val) =>
    acc.concat(Array.isArray(val) ? flatten(val) : val), []);

Array.prototype.flatten = function() {return flatten(this)};

const walkDir = dir => fs.readdirSync(dir)
    .map(file => fs.statSync(path.join(dir, file)).isDirectory()
        ? walkDir(path.join(dir, file))
        : path.join(dir, file).replace(/\\/g, '/')).flatten();
bashkovpd
  • 454
  • 1
  • 7
  • 20