Skip to content

Commit 3ac54e0

Browse files
committed
Recursively read a directory
0 parents  commit 3ac54e0

File tree

9 files changed

+228
-0
lines changed

9 files changed

+228
-0
lines changed

.gitignore

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Compiled source #
2+
###################
3+
*.com
4+
*.class
5+
*.dll
6+
*.exe
7+
*.o
8+
*.so
9+
10+
# Packages #
11+
############
12+
# it's better to unpack these files and commit the raw source
13+
# git has its own built in compression methods
14+
*.7z
15+
*.dmg
16+
*.gz
17+
*.iso
18+
*.jar
19+
*.rar
20+
*.tar
21+
*.zip
22+
23+
# Logs and databases #
24+
######################
25+
*.log
26+
*.sql
27+
*.sqlite
28+
29+
# OS generated files #
30+
######################
31+
.DS_Store*
32+
ehthumbs.db
33+
Icon?
34+
Thumbs.db
35+
36+
# Node.js #
37+
###########
38+
lib-cov
39+
*.seed
40+
*.log
41+
*.csv
42+
*.dat
43+
*.out
44+
*.pid
45+
*.gz
46+
47+
pids
48+
logs
49+
results
50+
51+
node_modules
52+
npm-debug.log
53+
54+
# Components #
55+
##############
56+
57+
/build
58+
/components
59+
60+
# ImageMagick #
61+
###############
62+
63+
*.cache
64+
*.mpc
65+
66+
# Other #
67+
#########
68+
test/*.2
69+
test/*/*.2
70+
test/*.mp4
71+
test/images/originalSideways.jpg.2

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test/

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
language: node_js
2+
node_js:
3+
- "0.10"

Makefile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
BIN = ./node_modules/.bin/
2+
3+
test:
4+
@${BIN}mocha \
5+
--reporter spec \
6+
--require should \
7+
--bail
8+
9+
clean:
10+
@rm -rf node_modules
11+
12+
.PHONY: test clean

README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# fs.readdirSyncRecursive [![Build Status](https://travis-ci.org/jonathanong/fs-readdir-recursive.png)](https://travis-ci.org/jonathanong/fs-readdir-recursive)
2+
3+
Read a directory recursively.
4+
5+
## Example
6+
7+
```js
8+
var read = require('fs-readdir-recursive')
9+
read(__dirname) === [
10+
'test/test.js',
11+
'index.js',
12+
'Makefile',
13+
'package.json',
14+
'README.md'
15+
]
16+
```
17+
18+
## API
19+
20+
### read(root [, filter])
21+
22+
`root` is the directory you wish to scan. `filter` is an optional filter for the files. By default, filter is:
23+
24+
```js
25+
function (x) {
26+
return x[0] !== '.'
27+
}
28+
```
29+
30+
Which basically just ignores `.` files.
31+
32+
## License
33+
34+
The MIT License (MIT)
35+
36+
Copyright (c) 2013 Jonathan Ong [email protected]
37+
38+
Permission is hereby granted, free of charge, to any person obtaining a copy
39+
of this software and associated documentation files (the "Software"), to deal
40+
in the Software without restriction, including without limitation the rights
41+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
42+
copies of the Software, and to permit persons to whom the Software is
43+
furnished to do so, subject to the following conditions:
44+
45+
The above copyright notice and this permission notice shall be included in
46+
all copies or substantial portions of the Software.
47+
48+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
49+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
50+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
51+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
52+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
53+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
54+
THE SOFTWARE.

index.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var fs = require('fs')
2+
var path = require('path')
3+
4+
module.exports = read
5+
6+
function read(root, filter, files, prefix) {
7+
prefix = prefix || ''
8+
files = files || []
9+
filter = filter || noDotFiles
10+
11+
var dir = path.join(root, prefix)
12+
if (fs.lstatSync(dir).isDirectory())
13+
fs.readdirSync(dir)
14+
.filter(filter)
15+
.forEach(function (name) {
16+
read(root, filter, files, path.join(prefix, name))
17+
})
18+
else
19+
files.push(prefix)
20+
21+
return files
22+
}
23+
24+
function noDotFiles(x) {
25+
return x[0] !== '.'
26+
}

package.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "fs-readdir-recursive",
3+
"description": "Recursively read a directory",
4+
"version": "0.0.1",
5+
"scripts": {
6+
"test": "make test"
7+
},
8+
"devDependencies": {
9+
"mocha": "*",
10+
"should": "*"
11+
},
12+
"author": {
13+
"name": "Jonathan Ong",
14+
"email": "[email protected]",
15+
"url": "http://jongleberry.com",
16+
"twitter": "https://twitter.com/jongleberry"
17+
},
18+
"repository": {
19+
"type": "git",
20+
"url": "https://github.com/jonathanong/fs-readdir-recursive.git"
21+
},
22+
"bugs": {
23+
"url": "https://github.com/jonathanong/fs-readdir-recursive/issues",
24+
"email": "[email protected]"
25+
},
26+
"license": "MIT",
27+
"engines": {
28+
"node": ">= 0.10"
29+
}
30+
}

test/.ignore

Whitespace-only changes.

test/test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
var path = require('path')
2+
3+
var read = require('../')
4+
5+
describe('fs.readdirSyncRecursive()', function () {
6+
it('should work in the folder', function (done) {
7+
var files = read(__dirname)
8+
9+
files.length.should.equal(1)
10+
files[0].should.equal('test.js')
11+
12+
done()
13+
})
14+
15+
it('should work at the root with a filter', function (done) {
16+
var files = read(path.join(__dirname, '..'), function (name) {
17+
return name[0] !== '.' && name !== 'node_modules'
18+
})
19+
20+
files.length.should.equal(5)
21+
files.sort().should.eql([
22+
'test/test.js',
23+
'index.js',
24+
'Makefile',
25+
'package.json',
26+
'README.md'
27+
].sort())
28+
29+
done()
30+
})
31+
})

0 commit comments

Comments
 (0)