-1

I want to recursively list the directories and read the files inside it using promises in node.js. Can someone please help me to get over this ?

1 Answers1

4

Given this directory structure:

.
├── dirtest
│   ├── bar.txt
│   └── foo
│       └── foo.txt
└── index.js

You have several ways to do this. Let's consider the most easy to layout using async/await, comments are in the code:

const fs = require("fs");
const path = require("path");
const util = require("util");

// Promisify the fs functions we will use (or use require("fs").promises)
const astat = util.promisify(fs.stat);
const areaddir = util.promisify(fs.readdir);

/**
 * Get a list of all files in a directory
 * @param {String} dir The directory to inventory
 * @returns {Array} Array of files
 */
async function getFiles(dir) {
  // Get this directory's contents
  const files = await areaddir(dir);
  // Wait on all the files of the directory
  return Promise.all(files
    // Prepend the directory this file belongs to
    .map(f => path.join(dir, f))
    // Iterate the files and see if we need to recurse by type
    .map(async f => {
      // See what type of file this is
      const stats = await astat(f);
      // Recurse if it is a directory, otherwise return the filepath
      return stats.isDirectory() ? getFiles(f) : f;
    }));
}

getFiles(".")
  .then(files => JSON.stringify(files, null, 4))
  .then(console.log)
  .catch(console.error);

This will produce a nested array of files with paths prepended:

[
    [
        "dirtest/bar.txt",
        [
            "dirtest/foo/foo.txt"
        ]
    ],
    "index.js"
]

You could then augment this to get a flattened list of files by drawing from this question: Merge/flatten an array of arrays in JavaScript?:

/**
 * Flatten an arbitrarrily deep Array of Arrays to a single Array
 * @param {Array} arr Array of Arrays to flatten
 * @returns {Array} The flattened Array
 */
function flatten(arr) {
  return arr.reduce((flat, toFlatten) => flat.concat(Array.isArray(toFlatten) ? flatten(toFlatten) : toFlatten), []);
}

/*
 * Same code from above snippet
 */

getFiles(".")
  .then(flatten)
  .then(files => JSON.stringify(files, null, 4))
  .then(console.log)
  .catch(console.error);

Which produces:

[
    "dirtest/bar.txt",
    "dirtest/foo/foo.txt",
    "index.js"
]
zero298
  • 25,467
  • 10
  • 75
  • 100