Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
reorganized the code and fixed unit tests
- Loading branch information
Showing
16 changed files
with
164 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
node_modules/ | ||
node_modules | ||
npm-debug.log | ||
.*.swp | ||
.DS_Store | ||
npm-debug.log | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
language: node_js | ||
node_js: 5 | ||
after_script: NODE_ENV=test istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
© 2014 Talles L <talleslasmar@gmail.com> | ||
|
||
Usage of the works is permitted provided that this instrument is retained with the works, so that any entity that uses the works is notified of this instrument. | ||
Usage of the works is permitted provided that this instrument is retained with the works, | ||
so that any entity that uses the works is notified of this instrument. | ||
|
||
DISCLAIMER: THE WORKS ARE WITHOUT WARRANTY. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,37 @@ | ||
# Recursive Files | ||
# recursive-files | ||
|
||
[![][build-img]][build] | ||
[![][coverage-img]][coverage] | ||
[![][dependencies-img]][dependencies] | ||
[![][devdependencies-img]][devdependencies] | ||
[![][module-img]][module] | ||
|
||
[![][npm-img]][npm] | ||
|
||
[build]: https://travis-ci.org/tallesl/recursive-files | ||
[build-img]: https://travis-ci.org/tallesl/recursive-files.png | ||
|
||
[coverage]: https://coveralls.io/r/tallesl/recursive-files?branch=master | ||
[coverage-img]: https://coveralls.io/repos/tallesl/recursive-files/badge.png?branch=master | ||
|
||
[dependencies]: https://david-dm.org/tallesl/recursive-files | ||
[dependencies-img]: https://david-dm.org/tallesl/recursive-files.png | ||
Asynchronous recursive [readdir]. | ||
Note that the given callback it's called multiple times, one for each found file. | ||
|
||
[devdependencies]: https://david-dm.org/tallesl/recursive-files#info=devDependencies | ||
[devdependencies-img]: https://david-dm.org/tallesl/recursive-files/dev-status.png | ||
[build]: https://travis-ci.org/tallesl/node-recursive-files | ||
[build-img]: https://travis-ci.org/tallesl/node-recursive-files.svg | ||
[coverage]: https://coveralls.io/r/tallesl/node-recursive-files?branch=master | ||
[coverage-img]: https://coveralls.io/repos/tallesl/node-recursive-files/badge.svg?branch=master | ||
[dependencies]: https://david-dm.org/tallesl/node-recursive-files | ||
[dependencies-img]: https://david-dm.org/tallesl/node-recursive-files.svg | ||
[devdependencies]: https://david-dm.org/tallesl/node-recursive-files#info=devDependencies | ||
[devdependencies-img]: https://david-dm.org/tallesl/node-recursive-files/dev-status.svg | ||
[npm]: https://npmjs.com/package/recursive-files | ||
[npm-img]: https://badge.fury.io/js/recursive-files.svg | ||
[readdir]: https://nodejs.org/api/all.html#fs_fs_readdir_path_options_callback | ||
|
||
[module]: http://badge.fury.io/js/recursive-files | ||
[module-img]: https://badge.fury.io/js/recursive-files.png | ||
## Usage | ||
|
||
[npm]: https://nodei.co/npm/recursive-files/ | ||
[npm-img]: https://nodei.co/npm/recursive-files.png?mini=true | ||
|
||
## recursiveFiles (dir, [options], callback) | ||
```js | ||
recursiveFiles(dir, [options], callback) | ||
``` | ||
|
||
* **dir** directory path; | ||
* **options** | ||
* **hidden** list hidden files (started with `.`); | ||
* **ext** list only files with the provided extension; | ||
* **nonregular** list *non regular* files such as devices or sockets; | ||
* **callback** `function (err, file)`, `file` will be a relative path of the found file to the given directory path; | ||
|
||
## Remarks | ||
* *Hidden* and *non regular* files are not listed by default; | ||
* The *callback* is called for *each* found file (realize that it's called multiple times). | ||
* **hidden** list hidden files (started with `.`), defaults to `false` | ||
* **ext** list only files with the provided extension | ||
* **nonregular** list *non regular* files such as devices or sockets, defaults to `false` | ||
* **callback** `function (err, file)` | ||
* `err` eventual error | ||
* `file` a relative path of the found file to the given directory path; |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
'use strict' | ||
|
||
const fs = require('fs') | ||
const path = require('path') | ||
|
||
module.exports = function read (dir, options, callback) { | ||
if (!callback && options) { | ||
callback = options | ||
options = { } | ||
} | ||
|
||
// forcing the ext, if any, to start with dot | ||
if (options && options.ext && options.ext[0] !== '.') options.ext = '.' + options.ext | ||
|
||
// list the files on the directory | ||
fs.readdir(dir, (err, files) => { | ||
if (err) { | ||
callback(err) | ||
} else { | ||
// for each file | ||
files.forEach((file) => { | ||
// getting full path of the file | ||
file = path.join(dir, file) | ||
|
||
// getting the stats of the file | ||
fs.stat(file, (err, stats) => { | ||
if (err) { | ||
process.nextTick(() => callback(err)) | ||
} else if (stats.isDirectory()) { | ||
// if it's a directory we do the whole thing for it | ||
read(file, options, callback) | ||
} else { | ||
// separating some info | ||
const ext = path.extname(file) | ||
const hidden = path.basename(file)[0] === '.' | ||
const nonregular = !stats.isFile() | ||
|
||
// if it's not a directory | ||
if ( | ||
// if we should display hidden files OR the file isn't a hidden one | ||
(options.hidden || !hidden) && | ||
|
||
// if we shouldn't filter by extension OR the file is the desired extension | ||
(!options.ext || options.ext === ext) && | ||
|
||
// if we shouldn't filter non regular files OR the file is a regular one | ||
(options.nonregular || !nonregular) | ||
) { | ||
// here, take it :) | ||
process.nextTick(() => callback(null, file)) | ||
} | ||
} | ||
}) | ||
}) | ||
} | ||
}) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,28 @@ | ||
{ | ||
"author": "Talles L <talleslasmar@gmail.com>", | ||
"bugs": { | ||
"url": "https://github.com/tallesl/recursive-files/issues" | ||
"url": "https://github.com/tallesl/node-recursive-files/issues" | ||
}, | ||
"description": "asynchronous recursive readdir", | ||
"description": "Asynchronous recursive readdir.", | ||
"devDependencies": { | ||
"coveralls": "^2.11.2", | ||
"istanbul": "^0.3.2", | ||
"mocha": "^2.0.1" | ||
"coveralls": "^2.11.9", | ||
"istanbul": "^0.4.3", | ||
"mocha": "^2.5.3", | ||
"standard": "^7.1.2" | ||
}, | ||
"homepage": "https://github.com/tallesl/recursive-files", | ||
"keywords": [ | ||
"async", | ||
"recursive", | ||
"readdir" | ||
], | ||
"license": "Fair", | ||
"main": "./lib/recursive-files.js", | ||
"name": "recursive-files", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/tallesl/recursive-files" | ||
"url": "https://github.com/tallesl/node-recursive-files.git" | ||
}, | ||
"scripts": { | ||
"test": "mocha" | ||
"test": "standard && mocha" | ||
}, | ||
"version": "1.0.1" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
'use strict' | ||
|
||
const assert = require('assert') | ||
const recursiveFiles = require('.') | ||
|
||
/* global it */ | ||
|
||
it('should list everything except hidden', (done) => { | ||
const files = [ ] | ||
|
||
recursiveFiles('dir', (err, file) => { | ||
assert.ifError(err) | ||
files.push(file) | ||
}) | ||
|
||
setTimeout(() => { | ||
assert.deepEqual( | ||
files.sort(), | ||
[ | ||
'dir/some-data.json', | ||
'dir/some-text-file.txt', | ||
'dir/subdir/another-text-file.txt' | ||
] | ||
) | ||
done() | ||
}, 200) | ||
}) | ||
|
||
it('should list everything including hidden', (done) => { | ||
const files = [ ] | ||
|
||
recursiveFiles('dir', { hidden: true }, (err, file) => { | ||
assert.ifError(err) | ||
files.push(file) | ||
}) | ||
|
||
setTimeout(() => { | ||
assert.deepEqual( | ||
files.sort(), | ||
[ | ||
'dir/.hidden', | ||
'dir/some-data.json', | ||
'dir/some-text-file.txt', | ||
'dir/subdir/.hidden', | ||
'dir/subdir/another-text-file.txt' | ||
] | ||
) | ||
done() | ||
}, 200) | ||
}) | ||
|
||
it('should list only .txt', (done) => { | ||
const files = [ ] | ||
|
||
recursiveFiles('dir', { ext: '.txt' }, (err, file) => { | ||
assert.ifError(err) | ||
files.push(file) | ||
}) | ||
|
||
setTimeout(() => { | ||
assert.deepEqual( | ||
files.sort(), | ||
[ | ||
'dir/some-text-file.txt', | ||
'dir/subdir/another-text-file.txt' | ||
] | ||
) | ||
done() | ||
}, 200) | ||
}) |
This file was deleted.
Oops, something went wrong.