node.js - 有没有更好的方法在 typescript 中编写这个递归方法

标签 node.js typescript recursion promise

我正在尝试编写一种方法来查找文件夹中的所有文件,包括子文件夹。使用 fs.readdirSync 编写非常简单,但我正在尝试编写一个不会阻塞的版本。 (即使用fs.readdir)。

我有一个可以运行的版本,但不太漂亮。对 Node 有更多经验的人可以看看是否有更好的方法来编写这个?我可以在我的代码库中看到其他一些地方可以应用此模式,因此如果有一个更干净的版本就好了!

  private static findFilesFromFolder(folder: string): Promise<string[]> {
    let lstat = util.promisify(fs.lstat)
    let readdir = util.promisify(fs.readdir)

    // Read the initial folder
    let files = readdir(folder)

      // Join the folder name to the file name to make it absolute
      .then(files => files.map(file => path.join(folder, file)))

      // Get the stats for each file (also as a promise)
      .then(files =>
        Promise.all(files.map(file =>
          lstat(file).then(stats => { return { file: file, stats: stats } })
        ))
      )

      // If the file is a folder, recurse. Otherwise just return the file itself.
      .then(info =>
        Promise.all(info.map(info => {
          if (info.stats.isDirectory()) {
            return this.findFilesFromFolder(info.file)
          } else {
            return Promise.resolve([info.file])
          }
        }
      )))

      // Do sume munging of the types - convert Promise<string[][]> to Promise<string[]>
    .then(nested => Array.prototype.concat.apply([], nested) as string[])

    return files
  }

最佳答案

我会做一些事情来使其更干净:

  • 将递归基本情况从循环内移动到顶层
  • 使用async/await语法
  • 使用const代替let

还将 promisify 调用放入 importfs

const lstat = util.promisify(fs.lstat)
const readdir = util.promisify(fs.readdir)

…
private static async findFilesFromPath(folder: string): Promise<string[]> {
  const stats = await lstat(folder);
  // If the file is a folder, recurse. Otherwise just return the file itself.
  if (stats.isDirectory()) {
    // Read the initial folder
    const files = await readdir(folder);
    // Join the folder name to the file name to make it absolute
    const paths = files.map(file => path.join(folder, file))
    const nested = await Promise.all(paths.map(p => this.findFilesFromPath(p)))
    // Do sume munging of the types - convert string[][] string[]
    return Array.prototype.concat.apply([], nested) as string[];
  } else {
    return [folder];
  }
}

关于node.js - 有没有更好的方法在 typescript 中编写这个递归方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59267003/

相关文章:

node.js - 在nodejs中回调内部回调?

javascript - 我们可以将 Angular FormArray 与 angular2-multiselect 下拉菜单一起使用吗?

javascript - 为什么运行时类型检查在 ts 中如此重要?

node.js - 如何在 OpenShift 上将 MongoDB 从 3.2.10 升级到 3.6?

node.js - 是否可以跳过 v8 GC 收集的对象?

mysql - 更可靠的循环来更新 Mongodb 中的 ObjectID?

java - 从网页中获取所有超链接并在java中递归地执行此操作

angular - *ng用于显示数据但抛出 'undefined'错误

recursion - LISP:如何在递归函数上不使用(Print)或(Format t)函数的情况下输出到控制台

python - 无法弄清楚递归函数