Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
0fa910f
improved docs, relative shortName option, improved performance for sy…
AckerApple Dec 12, 2016
6fce528
version patch bump
AckerApple Dec 12, 2016
3b01fe5
more windows compat
AckerApple Dec 12, 2016
42ed810
fixed issue when shortName=relative and type=combine
AckerApple Feb 8, 2017
1a21c09
Merge branch 'master' into master
AckerApple Feb 18, 2017
0984364
readme
AckerApple Jun 15, 2017
5a74154
tests
AckerApple Jun 15, 2017
c5e9d93
backwards compat old node
AckerApple Jun 15, 2017
cf54939
skip test for non utf8 files
AckerApple Jun 15, 2017
352413b
test remove arrow function
AckerApple Jun 15, 2017
3d4bc4b
backwards compat, removed Object.assign
AckerApple Jun 15, 2017
d0e9d78
removed support for node .1
AckerApple Jun 15, 2017
14e65c2
windows compat tests
AckerApple Jun 15, 2017
f507444
refine windows tests
AckerApple Jun 15, 2017
d0f447c
fix win testing
AckerApple Jun 15, 2017
501cc20
Create CODE_OF_CONDUCT.md
AckerApple Jun 15, 2017
f22e1e8
Delete CODE_OF_CONDUCT.md
AckerApple Jun 15, 2017
2f1ad78
Create CODE_OF_CONDUCT.md
AckerApple Jun 15, 2017
d162325
Merge pull request #1 from AckerApple/add-code-of-conduct-1
AckerApple Jun 15, 2017
3b83eda
Delete CODE_OF_CONDUCT.md
AckerApple Jun 15, 2017
3d5eaeb
Create CODE_OF_CONDUCT.md
AckerApple Jun 15, 2017
bfde55e
Merge pull request #2 from AckerApple/add-code-of-conduct-2
AckerApple Jun 15, 2017
44a6c68
Delete CODE_OF_CONDUCT.md
AckerApple Jun 15, 2017
408847b
fixed strange problem with webpack.
Aug 9, 2017
89938ae
Merge pull request #3 from btbxbob/master
AckerApple Aug 9, 2017
72a4d2c
ready for npm publish
AckerApple Aug 9, 2017
d915358
Merge branch 'master' of https://github.com/AckerApple/node-dir
AckerApple Aug 9, 2017
d372735
ready for npm publish
AckerApple Aug 9, 2017
e87f233
full version
AckerApple Aug 9, 2017
7cebf51
x
AckerApple Aug 9, 2017
a0a62b0
fix badge
AckerApple Aug 9, 2017
3eba375
x
AckerApple Aug 9, 2017
09ca72d
badges
AckerApple Aug 10, 2017
1927d64
v
AckerApple Aug 10, 2017
ec0a3da
fix empty folders
AckerApple Oct 14, 2017
ab7cbc6
fix test
AckerApple Oct 14, 2017
c9a93a2
fix test2
AckerApple Oct 14, 2017
6ad7dec
fixes
AckerApple Oct 16, 2017
1b41552
path-reader
AckerApple Feb 14, 2018
66d2c7a
compiled from typescript + fsStat options
Jun 17, 2022
a225bad
update
Jun 17, 2022
6a7f6db
update
Jun 17, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 98 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@ A lightweight Node.js module with methods for some common directory and file ope

- [installation](#installation)
- [usage](#usage)
- [methods](#methods)
- [readFiles( dir, options, fileCallback, finishedCallback)](#readfiles-dir-options-filecallback-finishedcallback)
- [readFilesStream( dir, options, streamCallback, finishedCallback)](#readfilesstream-dir-options-streamcallback-finishedcallback)
- [readFilesStream examples](#readfilesstream-examples)
- [files( dir, callback )](#files-dir-callback)
- [files( dir, {sync:true} )](#files-dir-synctrue)
- [promiseFiles( dir, callback )](#promisefiles-dir-callback)
- [subdirs( dir, callback )](#subdirs-dir-callback)
- [paths(dir, [combine], callback )](#pathsdir-combine-callback)
- [methods](#methods)
- [readFiles](#readfiles)
- [readFilesStream](#readfilesstream)
- [readFilesStream examples](#readfilesstream-examples)
- [files async](#files-async)
- [files sync](#files-sync)
- [promiseFiles](#promisefiles)
- [subdirs](#subdirs)
- [paths](#paths)
- [API Docs](#api-docs)
- [files(dir, type, callback, options)](#filesdir-type-callback-options)
- [files](#files-api)
- [promiseFiles](#promisefiles-api)
- [License](#license)

#### installation
Expand All @@ -33,10 +34,19 @@ For the sake of brevity, assume that the following line of code precedes all of
var dir = require('node-dir');
```

#### readFiles( dir, [options], fileCallback, [finishedCallback] )
#### readFilesStream( dir, [options], streamCallback, [finishedCallback] )
#### readFiles
A variation on the method readFilesStream. See usage for [readFilesStream](#readFilesStream)
```
readFiles( dir, [options], fileCallback, [finishedCallback] )
```

#### readFilesStream
Sequentially read the content of each file in a directory, passing the contents to a callback, optionally calling a finished callback when complete. The options and finishedCallback arguments are not required.

```
readFilesStream( dir, [options], streamCallback, [finishedCallback] )
```

Valid options are:
- encoding: file encoding (defaults to 'utf8')
- exclude: a regex pattern or array to specify filenames to ignore
Expand All @@ -53,8 +63,8 @@ A reverse sort can also be achieved by setting the sort option to 'reverse', 'de

#### readFilesStream examples

```javascript
// display contents of files in this script's directory
Display contents of files in this script's directory
```
dir.readFiles(__dirname,
function(err, content, next) {
if (err) throw err;
Expand All @@ -64,9 +74,12 @@ dir.readFiles(__dirname,
function(err, files){
if (err) throw err;
console.log('finished reading files:', files);
});
}
);
```

// display contents of huge files in this script's directory
Display contents of huge files in this script's directory
```
dir.readFilesStream(__dirname,
function(err, stream, next) {
if (err) throw err;
Expand All @@ -82,9 +95,12 @@ dir.readFilesStream(__dirname,
function(err, files){
if (err) throw err;
console.log('finished reading files:', files);
});
}
);
```

// match only filenames with a .txt extension and that don't start with a `.´
Match only filenames with a .txt extension and that don't start with a `.´
```
dir.readFiles(__dirname, {
match: /.txt$/,
exclude: /^\./
Expand All @@ -96,9 +112,12 @@ dir.readFiles(__dirname, {
function(err, files){
if (err) throw err;
console.log('finished reading files:',files);
});
}
);
```

// exclude an array of subdirectory names
Exclude an array of subdirectory names
```
dir.readFiles(__dirname, {
exclude: ['node_modules', 'test']
Copy link
Copy Markdown

@mlcdf mlcdf Jun 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a typo in the current README: the option to exclude directories is excludeDir, not exclude.

}, function(err, content, next) {
Expand All @@ -109,19 +128,20 @@ dir.readFiles(__dirname, {
function(err, files){
if (err) throw err;
console.log('finished reading files:',files);
});

}
);
```

// the callback for each file can optionally have a filename argument as its 3rd parameter
// and the finishedCallback argument is optional, e.g.
The callback for each file can optionally have a filename argument as its 3rd parameter and the finishedCallback argument is optional, e.g.
```
dir.readFiles(__dirname, function(err, content, filename, next) {
console.log('processing content of file', filename);
next();
});
console.log('processing content of file', filename);
next();
});
```


#### files( dir, callback )
#### files async
Asynchronously iterate the files of a directory and its subdirectories and pass an array of file paths to a callback.

```javascript
Expand All @@ -131,17 +151,23 @@ dir.files(__dirname, function(err, files) {
});
```

#### files( dir, {sync:true} )
#### files sync
Synchronously iterate the files of a directory and its subdirectories and pass an array of file paths to a callback.


In this example, a console log of items by relative path will be made
```javascript
var files = dir.files(__dirname, {sync:true});
var files = dir.files(__dirname, {sync:true, shortName:'relative'});
console.log(files);
```

#### promiseFiles( dir, callback )
#### promiseFiles
Asynchronously iterate the files of a directory and its subdirectories and pass an array of file paths to a callback.


```
promiseFiles(path, readType||options, options)
```

promiseFiles example
```javascript
dir.promiseFiles(__dirname)
.then((files)=>{
Expand All @@ -155,14 +181,18 @@ Note that for the files and subdirs the object returned is an array, and thus al
```javascript
dir.files(__dirname, function(err, files) {
if (err) throw err;

// sort ascending
files.sort();

// sort descending
files.reverse();

// include only certain filenames
files = files.filter(function (file) {
return ['allowed', 'file', 'names'].indexOf(file) > -1;
});

// exclude some filenames
files = files.filter(function (file) {
return ['exclude', 'these', 'files'].indexOf(file) === -1;
Expand All @@ -172,22 +202,30 @@ dir.files(__dirname, function(err, files) {

Also note that if you need to work with the contents of the files asynchronously, please use the readFiles method. The files and subdirs methods are for getting a list of the files or subdirs in a directory as an array.

#### subdirs( dir, callback )
#### subdirs
Asynchronously iterate the subdirectories of a directory and its subdirectories and pass an array of directory paths to a callback.

```
subdirs( dir, callback )
```

Example
```javascript
dir.subdirs(__dirname, function(err, subdirs) {
if (err) throw err;
console.log(subdirs);
});
```

#### paths(dir, [combine], callback )
#### paths
Asynchronously iterate the subdirectories of a directory and its subdirectories and pass an array of both file and directory paths to a callback.

Separated into two distinct arrays (paths.files and paths.dirs)
```
paths(dir, [combine], callback )
```

```javascript
Example: Separated into two distinct arrays (paths.files and paths.dirs)
```
dir.paths(__dirname, function(err, paths) {
if (err) throw err;
console.log('files:\n',paths.files);
Expand All @@ -207,7 +245,11 @@ dir.paths(__dirname, true, function(err, paths) {

## API Docs

### files(dir, type, callback, options)
### files API

```
files(dir, type, callback, options)
```

- **dir** - directory path to read
- **type**='file'
Expand All @@ -218,7 +260,24 @@ dir.paths(__dirname, true, function(err, paths) {
- **callback** -
- **options**
- **sync**=false - results are returned inline and no callbacks are used
- **shortName**=false - instead of fullpath file names, just get the names
- **shortName**=false||'relative' - instead of fullpath file names, just get the names or relative item names
- **recursive**=true - traverse through all children of given path

### promiseFiles API

```
promiseFiles(dir, type||options, options)
```

- **dir** - directory path to read
- **type**='file'
- 'file' returns only file listings
- 'dir' returns only directory listings
- 'all' returns {dirs:[], files:[]}
- 'combine' returns []
- **options**
- **sync**=false - DO NOT USE for promiseFiles, will cause unexpected behavior
- **shortName**=false||'relative' - instead of fullpath file names, just get the names or relative item names
- **recursive**=true - traverse through all children of given path

## License
Expand Down
Loading