使用节点js中的promises进行递归目录列表

问题描述 投票:-4回答:1

我想递归列出目录并使用node.js中的promises读取其中的文件。有人可以帮我解决这个问题吗?

node.js es6-promise readdir
1个回答
1
投票

鉴于此目录结构:

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

你有几种方法可以做到这一点。让我们考虑使用async / await最容易布局,注释在代码中:

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);

这将生成一个嵌套路径的嵌套文件数组:

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

然后你可以通过从这个问题中提取来扩充这个以得到一个扁平的文件列表: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);

哪个产生:

[
    "dirtest/bar.txt",
    "dirtest/foo/foo.txt",
    "index.js"
]
© www.soinside.com 2019 - 2024. All rights reserved.