@aboviq/readdir-recursive 中文文档教程

发布于 3年前 浏览 24 项目主页 更新于 3年前

@aboviq/readdir-recursive

构建状态NPM 版本XO code style

可自定义的异步递归 fs.readdir,没有依赖项和合理的默认值

Why?

  • No dependencies / Small
  • Asynchronous, i.e. returns a promise (uses async/await under the hood)
  • Provides a syncronous version as well
  • Sane defaults, i.e. does not recurse into node_modules by default
  • Fully customizable, with options to decide in which folders to recurse into and what files and information to include in the results, with full access to each file's Stats information

Installation

安装 @aboviq/ readdir-recursive 使用 npm

npm install @aboviq/readdir-recursive

Usage

Module usage

const readdirRecursive = require('@aboviq/readdir-recursive');

const files = await readdirRecursive('a/path');
/*
[
  "/full-path/a/path/filename.ext",
  "/full-path/a/path/nested/folders/another.ext",
  ...
]
*/

const files = readdirRecursive.sync('a/path');
/*
[
  "/full-path/a/path/filename.ext",
  "/full-path/a/path/nested/folders/another.ext",
  ...
]
*/

API

readdirRecursive(dir, options)

NameTypeDescription
dirStringThe folder to read files recursively in, either relative to cwd or an absolute path
optionsObjectOptions for filtering, recursion and transformation

返回:Promise,所有找到的文件根据转换 <

readdirRecursive.sync(dir, options)

NameTypeDescription
dirStringThe folder to read files recursively in, either relative to cwd or an absolute path
optionsObjectOptions for filtering, recursion and transformation

返回:数组,找到所有根据transformer转换后未过滤掉的文件

Options

strong>注意:所有函数选项都可以使用readdirRecursive,但不是同步版本。

options.filter

类型:函数
签名:filter :: Object -> 布尔值
默认值:<代码>() => true

filter 选项用于决定文件是否应包含在生成的文件数组中。 如果过滤器函数返回真值,则包含一个文件。

传递给 filter 函数的 Object 具有以下属性:

NameTypeDescription
fileStringThe file name, e.g. "file.txt"
pathStringThe full path to the file, e.g. "/your/folder/sub-folder/file.txt"
statsStatsA stats object providing information about the file

options.transform

类型:Function
签名:transform :: Object -> 字符串
默认值:返回每个文件的完整路径的函数

transform 选项用于将文件信息转换为有用的信息。 每个通过过滤器函数的文件在被包含在结果数组中之前都会被转换。

传递给 transform 函数的 Object 具有以下属性:

NameTypeDescription
fileStringThe file name, e.g. "file.txt"
pathStringThe full path to the file, e.g. "/your/folder/sub-folder/file.txt"
statsStatsA stats object providing information about the file

options.recurse

类型:Function
签名:recurse :: Object -> 布尔值
默认值:不会递归的函数 node_modules

recurse 选项用于决定文件夹是否应该递归。 如果递归函数返回真值,则文件夹被递归。

传递给 recurse 函数的 Object 具有以下属性:

NameTypeDescription
dirStringThe folder name, e.g. "src"
pathStringThe full path to the folder, e.g. "/your/folder/sub-folder"
statsStatsA stats object providing information about the folder

Contributing

请参阅贡献指南 和我们的 行为准则

License

麻省理工学院 © Aboviq AB

@aboviq/readdir-recursive

Build statusNPM versionXO code style

Customizeable async recursive fs.readdir with no dependencies and sane defaults

Why?

  • No dependencies / Small
  • Asynchronous, i.e. returns a promise (uses async/await under the hood)
  • Provides a syncronous version as well
  • Sane defaults, i.e. does not recurse into node_modules by default
  • Fully customizable, with options to decide in which folders to recurse into and what files and information to include in the results, with full access to each file's Stats information

Installation

Install @aboviq/readdir-recursive using npm:

npm install @aboviq/readdir-recursive

Usage

Module usage

const readdirRecursive = require('@aboviq/readdir-recursive');

const files = await readdirRecursive('a/path');
/*
[
  "/full-path/a/path/filename.ext",
  "/full-path/a/path/nested/folders/another.ext",
  ...
]
*/

const files = readdirRecursive.sync('a/path');
/*
[
  "/full-path/a/path/filename.ext",
  "/full-path/a/path/nested/folders/another.ext",
  ...
]
*/

API

readdirRecursive(dir, options)

NameTypeDescription
dirStringThe folder to read files recursively in, either relative to cwd or an absolute path
optionsObjectOptions for filtering, recursion and transformation

Returns: Promise<Array>, all found files transformed according to the transformer and that has not been filtered out

readdirRecursive.sync(dir, options)

NameTypeDescription
dirStringThe folder to read files recursively in, either relative to cwd or an absolute path
optionsObjectOptions for filtering, recursion and transformation

Returns: Array, all found files transformed according to the transformer and that has not been filtered out

Options

Note: all function options can be asynchronous (return promises) when using the async version of readdirRecursive, but not with the sync version.

options.filter

Type: Function
Signature: filter :: Object -> Boolean
Default: () => true

The filter option is used to decide if a file should be included in the resulting array of files or not. A file is included if the filter function returns a truthy value.

The Object passed to the filter function has the following properties:

NameTypeDescription
fileStringThe file name, e.g. "file.txt"
pathStringThe full path to the file, e.g. "/your/folder/sub-folder/file.txt"
statsStatsA stats object providing information about the file

options.transform

Type: Function
Signature: transform :: Object -> String
Default: a function returing the full path of each file

The transform option is used to transform file information into something useful. Every file that passes the filter function will be transformed before being included in the resulting array.

The Object passed to the transform function has the following properties:

NameTypeDescription
fileStringThe file name, e.g. "file.txt"
pathStringThe full path to the file, e.g. "/your/folder/sub-folder/file.txt"
statsStatsA stats object providing information about the file

options.recurse

Type: Function
Signature: recurse :: Object -> Boolean
Default: a function which won't recurse node_modules

The recurse option is used to decide if a folder should be recursed into or not. A folder is recursed if the recurse function returns a truthy value.

The Object passed to the recurse function has the following properties:

NameTypeDescription
dirStringThe folder name, e.g. "src"
pathStringThe full path to the folder, e.g. "/your/folder/sub-folder"
statsStatsA stats object providing information about the folder

Contributing

See Contribution Guidelines and our Code Of Conduct.

License

MIT © Aboviq AB

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文