Skip to content

Commit

Permalink
reorganized the code and fixed unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tallesl committed Jun 20, 2016
1 parent 01aae59 commit 9547c0e
Show file tree
Hide file tree
Showing 16 changed files with 164 additions and 144 deletions.
5 changes: 2 additions & 3 deletions .gitignore
@@ -1,5 +1,4 @@
node_modules/
node_modules
npm-debug.log
.*.swp
.DS_Store
npm-debug.log

2 changes: 0 additions & 2 deletions .jshintignore

This file was deleted.

6 changes: 0 additions & 6 deletions .jshintrc

This file was deleted.

2 changes: 1 addition & 1 deletion .travis.yml
@@ -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

4 changes: 2 additions & 2 deletions LICENSE
@@ -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.

51 changes: 24 additions & 27 deletions README.md
@@ -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.
57 changes: 57 additions & 0 deletions index.js
@@ -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))
}
}
})
})
}
})
}
63 changes: 0 additions & 63 deletions lib/recursive-files.js

This file was deleted.

17 changes: 8 additions & 9 deletions package.json
@@ -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"
}
70 changes: 70 additions & 0 deletions test.js
@@ -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)
})
31 changes: 0 additions & 31 deletions test/recursive-files.js

This file was deleted.

0 comments on commit 9547c0e

Please sign in to comment.