https://github.com/compulim/crawl-fs
Recursively crawl the content of a folder
https://github.com/compulim/crawl-fs
Last synced: about 1 year ago
JSON representation
Recursively crawl the content of a folder
- Host: GitHub
- URL: https://github.com/compulim/crawl-fs
- Owner: compulim
- License: mit
- Created: 2016-02-12T09:21:01.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2016-02-12T18:05:08.000Z (over 10 years ago)
- Last Synced: 2025-05-31T10:41:58.732Z (about 1 year ago)
- Language: JavaScript
- Size: 11.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# crawl-fs [](https://travis-ci.org/compulim/crawl-fs)
Recursively crawls a folder and returns a list of relative filenames. Iteration is flat and do not use recursive function.
## Usage
There are few ways to use `crawl-fs`. Following examples assume the folder structure looks like this:
```
./folder-to-crawl/abc.txt
./folder-to-crawl/def/
./folder-to-crawl/def/ghi.txt
./folder-to-crawl/xyz/
```
We only returns files, empty folders are not included in result. Filenames are always relative to the base path, which relative to `process.cwd()`, specified in the function call.
### Default returns an array
```javascript
require('crawl-fs')('folder-to-crawl/', (err, filenames) => {
assert.deepEqual(
filenames,
[
'abc.txt',
'def/ghi.txt'
]
);
});
```
### Returns a Promise with an iterator
If applicable, this approach is preferred as it is progressive and requires less memory.
```javascript
let numCalled = 0;
require('crawl-fs').withIterator('folder-to-crawl/', filename => {
// First iteration
numCalled === 0 && assert.equal(filename, 'abc.txt');
// Second iteration
numCalled === 1 && assert.equal(filename, 'def/ghi.txt');
numCalled++;
}).then(() => {
// Completion
assert.equal(numCalled, 2);
});
```
## Design considerations
If you think these design considerations are not valid, please [challenge](https://github.com/compulim/crawl-fs/issues/new/) us.
* We did not use Promise progression, instead, we prefer iterator pattern with Promise resolver as finisher
* [Bluebird](http://bluebirdjs.com/docs/api/progression-migration.html) also discourage progression
* We did not implement ES6 generator
* Our iterator is async, ES6 sync generator has no real benefits over array, in terms of time/space complexity
## Changelog
* 0.0.1 (2016-02-13) - Initial commit
## Wishlist
* [ ] When async generator is official in ES7, we should add new `.withAsyncGenerator()` function
## Contribution
Want to contribute? There are few easy ways:
* Like us? Please [star](https://github.com/compulim/crawl-fs/stargazers) us!
* Want to make it even awesome? [Send us your wish](https://github.com/compulim/crawl-fs/issues/new/).
* Hate how it is working? [File an issue](https://github.com/compulim/crawl-fs/issues/) to us.