@folder/readdir

中文文档
3.1.0发布于 2021/05/13MITCommonJS零依赖近 30 天下载 3871

Recursively read a directory, blazing fast.

原始使用文档
文档目录27 节
  1. Install
  2. Why use @folder/readdir and not some other lib?
  3. Usage
  4. Options
  5. absolute
  6. base
  7. basename
  8. depth
  9. dot
  10. filter
  11. follow
  12. isMatch
  13. nodir
  14. objects
  15. onDirectory
  16. onEach
  17. onFile
  18. onSymbolicLink
  19. realpath
  20. recursive
  21. symlinks
  22. unique
  23. Tips & Tricks
  24. Benchmarks
  25. About
  26. Author
  27. License

@folder/readdir Donate NPM version NPM monthly downloads NPM total downloads

Recursively read a directory, blazing fast.

Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your :heart: and support.

Install

Install with npm (requires Node.js >=10):

$ npm install --save @folder/readdir

Why use @folder/readdir and not some other lib?

Usage

const readdir = require('@folder/readdir');
const options = {};

// async usage
console.log(await readdir('somedir', options));
console.log(await readdir(['two', 'dirs'], options));

// sync usage
console.log(readdir.sync('somedir', options));
console.log(readdir.sync(['two' 'dirs'], options));

params

Both the async and sync functions take the same arguments:

readdir(dir, options);
  • dir (string|array) - one or more directories to read
  • options - see available options

Options

absolute

When true, absolute paths are returned. Otherwise, returned paths are relative to options.base if defined, or the given directory.

Type: boolean

Default: undefined

Example

console.log(await readdir('some/dir', { absolute: true }));

base

The base directory from which relative paths should be created.

Type: string

Default: Defaults to the directory passed as the first argument.

Example

const files = await readdir('some/dir', { base: 'dir' });
console.log(files);

basename

When true, only the basename of each file is returned.

Type: boolean

Default: undefined

Example

console.log(await readdir('some/dir', { basename: true }));

depth

The maximum folder depth to recursively read directories.

Type: number

Default: undefined

Example

const files = await readdir('some/dir', { depth: 2 });
console.log(files);

dot

Dotfiles are included in the result by default. Pass false to ignore all dotfiles. Use [onEach][], [onFile][], [onDirectory][], or [isMatch] if you need something more granular.

Type: boolean

Default: true

Example

const files = await readdir('.');
console.log(files);
//=> ['.DS_Store', '.git', 'LICENSE', 'README.md', 'package.json']

const files = await readdir('.', { dot: false });
console.log(files);
//=> ['LICENSE', 'README.md', 'package.json']

filter

Type: function|string|array|regexp

Default: undefined

Example

// only return file paths with "foo" somewhere in the path
console.log(await readdir('some/dir', { filter: /foo/ }));

// only return file paths without "foo" somewhere in the path
console.log(await readdir('some/dir', { filter: file => !/foo/.test(file.path) }));

follow

Follow symbolic links.

Type: boolean

Default: undefined

Example

console.log(await readdir('some/dir', { follow: true }));

isMatch

Type: function|string|regex|array<function|string|regex>

Default: undefined

Example

// only return file paths with "/.git/" somewhere in the path
console.log(await readdir('some/dir', { isMatch: /\/\.git\// }));

// only return file paths that are not inside "node_modules"
console.log(await readdir('some/dir', { isMatch: file => !file.relative.includes('node_modules') }));

// get all files that are not named .DS_Store
console.log(await readdir('some/dir', { isMatch: file => file.name !== '.DS_Store' }));

// use globs
const picomatch = require('picomatch');
const isMatch = picomatch('*/*.js');
console.log(await readdir('some/dir', { isMatch: file => isMatch(file.relative) }));

nodir

When true directories are excluded from the result.

Type: boolean

Default: undefined

objects

Return fs.Dirent objects instead of paths.

Type: boolean

Default: undefined

console.log(await readdir('some/dir', { objects: true }));

onDirectory

Function to be called on all directories.

Type: function

Default: undefined

Example

const onDirectory = file => {
  if (file.name === 'node_modules') {
    file.recurse = false;
  }
};
console.log(await readdir('some/dir', { onDirectory }));

onEach

Function to be called on all directories and files.

Type: function

Default: undefined

Example

const onEach = file => {
  if (file.name === 'node_modules') {
    file.recurse = false;
  }
  if (file.isFile() && file.name[0] === '.') {
    file.keep = true;
  }
};
console.log(await readdir('some/dir', { onEach }));

onFile

Function to be called on all files.

Type: function

Default: undefined

Example

const onFile = file => {
  if (file.isFile() && file.name[0] === '.') {
    file.keep = true;
  }
};
console.log(await readdir('some/dir', { onFile }));

Function to be called on all symbolic links.

Type: function

Default: undefined

Example

const onSymbolicLink = file => {
  // do stuff
};
console.log(await readdir('some/dir', { onSymbolicLink }));

realpath

When true, the realpath of the file is returned in the result. This can be used in combination with other options, like basename or relative.

Type: boolean

Default: undefined

Example

console.log(await readdir('some/dir', { realpath: true }));

recursive

Type: function

Type: string

Type: boolean

Default: undefined

Example

const files = await readdir('some/dir', { recursive: true });
console.log(files);

Returns the first directory level of symbolic links. Use options.follow to recursively follow symlinks.

Type: boolean

Default: undefined

Example

console.log(await readdir('some/dir', { symlinks: true }));

unique

Return only unique file paths. Only needed when options.realpath is true.

Type: boolean

Default: undefined

Example

console.log(await readdir('some/dir', { unique: true }));

Tips & Tricks

Use the onFile option to operate on files as they are read from the file system, before they are pushed onto the results array.

This allows you to pricisely control which files are returned.

(Note that even when you specify that files should be returned as paths rather than objects, all functions passed on the options will receive files as objects, so that you may manipulate the paths that are returned however you need to)

const readdir = require('@folder/readdir');
const isMatch = file => true;

module.exports = async (dir, options) => {
  const opts = { absolute: true, recursive: true, objects: true, ...options };
  const files = [];

  const onFile = file => {
    if (isMatch(file)) {
      files.push(file);
    }
  };

  await readdir(dir, { ...opts, onFile });
  return files;
};

Files and directories

The onFile option does not receive dir objects, only dirents (files). If you need both files and directories, you can do the following:

const readdir = require('@folder/readdir');
const isMatch = file => true;

module.exports = async (dir, options) => {
  const opts = { recursive: true, objects: true, ...options };
  const files = [];

  const onDirectory = file => {
    if (file.name === 'node_modules') {
      file.recurse = false;
    }
  };

  const onFile = file => {
    if (isMatch(file)) {
      files.push(file);
    }
  };

  await readdir(dir, { ...opts, onFile, onDirectory });
  return files;
};

Or you can use onEach (which gives you each file before it has been determined whether or not the file will be returned based on other criteria and options. this allows you to override default behavior in a granular way), or onPush (which gives you a file that is going to be returned in the results array).

Here, we only show onEach, since it's identical to onPush in terms of usage.

const readdir = require('@folder/readdir');

const ignore = ['node_modules', '.git'];
const isIgnored = file => ignore.includes(file.nane);

module.exports = async (dir, options) => {
  const opts = { recursive: true, objects: true, ...options };
  const files = [];

  const onEach = file => {
    if (file.isDirectory()) {
      file.recurse = !isIgnored(file);
    } else {
      files.push(file);
    }
  };

  await readdir(dir, { ...opts, onFile, onEach });
  return files;
};

Benchmarks

(Note that only the benchmarks against fdir are included here since that library claims to be the fastest)

To run the benchmarks yourself, you'll need to cd into the bench folder and run $ npm i. Run the recursive-large benchmarks last, and before you run them cd into bench/fixtures and do $ npm i.

Specs

  • CPU: Intel® Core™ i9-9980HK 2.4GHz
  • Cores: 16 (8 Physical)
  • RAM: 64GB
  • Disk: Apple APPLE SSD AP2048N 1864GB NVMe (PCIe x4)
  • OS: macOS macOS Big Sur (darwin)
  • Kernel: 20.3.0 x64
  • Node: v15.14.0
  • V8: 8.6.395.17-node.28
# single directory (~5-10 files)
  @folder/readdir x 24,938 ops/sec (124,693 runs sampled)
             fdir x 24,771 ops/sec (123,858 runs sampled)

# recursive ~220 files
  @folder/readdir x 1,915 ops/sec (9,576 runs sampled)
             fdir x 1,850 ops/sec (9,253 runs sampled)

# recursive ~2,700 files
  @folder/readdir x 155 ops/sec (780 runs sampled)
             fdir x 145 ops/sec (730 runs sampled)

# recursive ~57,200 files (just gatsby!)
  @folder/readdir x 11 ops/sec (57 runs sampled)
             fdir x 10 ops/sec (54 runs sampled)

About

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Running Tests

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

$ npm install && npm test
Building docs

(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)

To generate the readme, run the following command:

$ npm install -g verbose/verb#dev verb-generate-readme && verb

Author

Jon Schlinkert

License

Copyright © 2021, Jon Schlinkert. Released under the MIT License.


This file was generated by verb-generate-readme, v0.8.0, on April 19, 2021.

版本与发布记录

@folder/readdir 共发布 7 个版本,首次发布于 2019/04/22;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.1.0latest2021/05/135 年前
3.0.12021/04/205 年前
3.0.02021/04/195 年前
2.1.02019/08/187 年前
2.0.02019/07/027 年前
1.1.02019/04/307 年前

关于 @folder/readdir 的常见问题

@folder/readdir 是什么?

Recursively read a directory, blazing fast.

如何安装 @folder/readdir?

在项目目录运行 npm install @folder/readdir,或使用 pnpm add / yarn add / bun add。

@folder/readdir 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/folder__readdir 是否存在。

@folder/readdir 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@folder/readdir 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=10。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@folder/readdir 可以商用吗?

@folder/readdir 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@folder/readdir 的下载量有多少?

根据 npm 官方下载统计,@folder/readdir 在 2026/10/15 – 2026/11/13 这 30 天内被下载 3,871 次,日均约 129 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@folder/readdir 还在维护吗?

最新版本 3.1.0 发布于 2021/05/13(5 年前);至今共发布 7 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/04/22。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。