Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Recursively read a directory, blazing fast. Use with picomatch or micromatch to match globs.

License

NotificationsYou must be signed in to change notification settings

folder/readdir

Repository files navigation

Recursively read a directory, blazing fast.

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

Install

Install withnpm (requiresNode.js >=10):

$ npm install --save @folder/readdir

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

Usage

constreaddir=require('@folder/readdir');constoptions={};// async usageconsole.log(awaitreaddir('somedir',options));console.log(awaitreaddir(['two','dirs'],options));// sync usageconsole.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 availableoptions

Options

absolute

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

Type:boolean

Default:undefined

Example

console.log(awaitreaddir('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

constfiles=awaitreaddir('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(awaitreaddir('some/dir',{basename:true}));

depth

The maximum folder depth to recursively read directories.

Type:number

Default:undefined

Example

constfiles=awaitreaddir('some/dir',{depth:2});console.log(files);

dot

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

Type:boolean

Default:true

Example

constfiles=awaitreaddir('.');console.log(files);//=> ['.DS_Store', '.git', 'LICENSE', 'README.md', 'package.json']constfiles=awaitreaddir('.',{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 pathconsole.log(awaitreaddir('some/dir',{filter:/foo/}));// only return file paths without "foo" somewhere in the pathconsole.log(awaitreaddir('some/dir',{filter:file=>!/foo/.test(file.path)}));

follow

Follow symbolic links.

Type:boolean

Default:undefined

Example

console.log(awaitreaddir('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 pathconsole.log(awaitreaddir('some/dir',{isMatch:/\/\.git\//}));// only return file paths that are not inside "node_modules"console.log(awaitreaddir('some/dir',{isMatch:file=>!file.relative.includes('node_modules')}));// get all files that are not named .DS_Storeconsole.log(awaitreaddir('some/dir',{isMatch:file=>file.name!=='.DS_Store'}));// use globsconstpicomatch=require('picomatch');constisMatch=picomatch('*/*.js');console.log(awaitreaddir('some/dir',{isMatch:file=>isMatch(file.relative)}));

nodir

Whentrue directories are excluded from the result.

Type:boolean

Default:undefined

objects

Returnfs.Dirent objects instead of paths.

Type:boolean

Default:undefined

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

onDirectory

Function to be called on all directories.

Type:function

Default:undefined

Example

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

onEach

Function to be called on all directories and files.

Type:function

Default:undefined

Example

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

onFile

Function to be called on all files.

Type:function

Default:undefined

Example

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

onSymbolicLink

Function to be called on all symbolic links.

Type:function

Default:undefined

Example

constonSymbolicLink=file=>{// do stuff};console.log(awaitreaddir('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, likebasename orrelative.

Type:boolean

Default:undefined

Example

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

recursive

Type:function

Type:string

Type:boolean

Default:undefined

Example

constfiles=awaitreaddir('some/dir',{recursive:true});console.log(files);

symlinks

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

Type:boolean

Default:undefined

Example

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

unique

Return only unique file paths. Only needed whenoptions.realpath istrue.

Type:boolean

Default:undefined

Example

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

Tips & Tricks

Use theonFile 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 aspaths 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)

constreaddir=require('@folder/readdir');constisMatch=file=>true;module.exports=async(dir,options)=>{constopts={absolute:true,recursive:true,objects:true, ...options};constfiles=[];constonFile=file=>{if(isMatch(file)){files.push(file);}};awaitreaddir(dir,{ ...opts, onFile});returnfiles;};

Files and directories

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

constreaddir=require('@folder/readdir');constisMatch=file=>true;module.exports=async(dir,options)=>{constopts={recursive:true,objects:true, ...options};constfiles=[];constonDirectory=file=>{if(file.name==='node_modules'){file.recurse=false;}};constonFile=file=>{if(isMatch(file)){files.push(file);}};awaitreaddir(dir,{ ...opts, onFile, onDirectory});returnfiles;};

Or you can useonEach (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), oronPush (which gives you a file that is going to be returned in the results array).

Here, we only showonEach, since it's identical toonPush in terms of usage.

constreaddir=require('@folder/readdir');constignore=['node_modules','.git'];constisIgnored=file=>ignore.includes(file.nane);module.exports=async(dir,options)=>{constopts={recursive:true,objects:true, ...options};constfiles=[];constonEach=file=>{if(file.isDirectory()){file.recurse=!isIgnored(file);}else{files.push(file);}};awaitreaddir(dir,{ ...opts, onFile, onEach});returnfiles;};

Benchmarks

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

To run the benchmarks yourself, you'll need to cd into thebench folder and run$ npm i. Run therecursive-large benchmarks last, and before you run them cd intobench/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&& npmtest
Building docs

(This project's readme.md is generated byverb, 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 theMIT License.


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


[8]ページ先頭

©2009-2025 Movatter.jp