Comparing version 0.1.2 to 0.2.0
77
index.js
/*! | ||
* dirs <https://github.com/jonschlinkert/dirs> | ||
* | ||
* Copyright (c) 2014 Jon Schlinkert, contributors. | ||
* Licensed under the MIT License | ||
* Copyright (c) 2014-2015, Jon Schlinkert. | ||
* Licensed under the MIT License. | ||
*/ | ||
@@ -11,45 +11,56 @@ | ||
var fs = require('fs'); | ||
var isDir = require('is-directory'); | ||
var path = require('path'); | ||
var async = require('async'); | ||
var dirs = module.exports = function dirs(dir, cb) { | ||
if (typeof cb !== 'function') { | ||
return dirs.sync(dir); | ||
} | ||
module.exports = dirs; | ||
if (!dir.length) { | ||
return cb(null, []); | ||
function dirs(cwd, cb) { | ||
if (arguments.length === 1) { | ||
throw new Error('dirs async expects a callback function.'); | ||
} | ||
fs.readdir(dir, function (err, files) { | ||
if (err) { | ||
return cb(err); | ||
} | ||
fs.readdir(cwd, function(err, files) { | ||
if (err) return cb(err); | ||
var res = []; | ||
var arr = []; | ||
async.map(files, function(fp, next) { | ||
fp = path.join(cwd, fp); | ||
files.forEach(function (filepath) { | ||
filepath = [dir, filepath].join('/'); | ||
if (isDir(filepath)) { | ||
arr = arr.concat(dirs(filepath)); | ||
} | ||
arr.push(filepath); | ||
fs.stat(fp, function(err, stats) { | ||
if (err) return handle(err, next); | ||
if (stats.isDirectory()) { | ||
dirs(fp, function (err, files) { | ||
if (err) return cb(err); | ||
next(null, res.push.apply(res, files)); | ||
}); | ||
res.push(fp); | ||
} else { | ||
next(null, res.push(fp)); | ||
} | ||
}); | ||
}, function(err) { | ||
cb(err, res); | ||
}); | ||
return cb(null, arr); | ||
}); | ||
}; | ||
} | ||
function handle(err, next) { | ||
return (err.code !== 'ENOENT') ? next(err) : next(); | ||
} | ||
dirs.sync = function dirsSync(dir) { | ||
return fs.readdirSync(dir) | ||
.reduce(function (acc, filepath) { | ||
filepath = [dir, filepath].join('/'); | ||
var files = fs.readdirSync(dir); | ||
var len = files.length, i = 0; | ||
var res = []; | ||
if (isDir(filepath)) { | ||
acc = acc.concat(dirs(filepath)); | ||
} | ||
acc.push(filepath); | ||
return acc; | ||
}, []); | ||
while (len--) { | ||
var fp = path.join(dir, files[i++]); | ||
if (fs.statSync(fp).isDirectory()) { | ||
res.push.apply(res, dirsSync(fp)) | ||
} | ||
res.push(fp); | ||
} | ||
return res; | ||
}; |
{ | ||
"name": "dirs", | ||
"description": "List directories or files, async or sync.", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"homepage": "https://github.com/jonschlinkert/dirs", | ||
@@ -17,7 +17,8 @@ "author": { | ||
}, | ||
"licenses": [ | ||
{ | ||
"type": "MIT", | ||
"url": "https://github.com/jonschlinkert/dirs/blob/master/LICENSE-MIT" | ||
} | ||
"license": { | ||
"type": "MIT", | ||
"url": "https://github.com/jonschlinkert/dirs/blob/master/LICENSE" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
@@ -29,11 +30,9 @@ "main": "index.js", | ||
"scripts": { | ||
"test": "mocha -R spec" | ||
"test": "mocha" | ||
}, | ||
"dependencies": { | ||
"is-directory": "^0.2.1" | ||
"async": "^0.9.0" | ||
}, | ||
"devDependencies": { | ||
"mocha": "*", | ||
"should": "^4.0.4", | ||
"verb": ">= 0.2.6" | ||
"mocha": "*" | ||
}, | ||
@@ -40,0 +39,0 @@ "keywords": [ |
@@ -1,57 +0,40 @@ | ||
# dirs [![NPM version](https://badge.fury.io/js/dirs.svg)](http://badge.fury.io/js/dirs) | ||
# dirs [![NPM version](https://badge.fury.io/js/dirs.svg)](http://badge.fury.io/js/dirs) [![Build Status](https://travis-ci.org/jonschlinkert/dirs.svg)](https://travis-ci.org/jonschlinkert/dirs) | ||
> List directories or files, async or sync. | ||
## Install | ||
#### Install with [npm](npmjs.org): | ||
## Install with [npm](npmjs.org) | ||
```bash | ||
npm i dirs --save-dev | ||
npm i dirs --save | ||
``` | ||
## Run tests | ||
## Usage | ||
```bash | ||
npm test | ||
```js | ||
var list = require('dirs'); | ||
``` | ||
## Usage | ||
Pass the starting directory: | ||
```js | ||
var list = require('dirs'); | ||
// async | ||
list('fixtures', function (err, files) { | ||
list('.', function (err, files) { | ||
if (err) console.log(err); | ||
console.log(files) | ||
//=> ['.gitattributes', '.gitignore', '.jshintrc', ...] | ||
}); | ||
// sync | ||
var files = list('fixtures'); | ||
console.log(files); | ||
list('.'); | ||
//=> ['.gitattributes', '.gitignore', '.jshintrc', ...] | ||
``` | ||
### Filtering | ||
## Running tests | ||
Install dev dependencies. | ||
Use whatever filtering function you need on the returned array: | ||
**Example:** | ||
```js | ||
var isDir = require('is-directory'); | ||
// async | ||
list('fixtures', function (err, files) { | ||
if (err) console.log(err); | ||
files = files.filter(isDir); | ||
console.log(files) | ||
//=> [ 'fixtures/one', 'fixtures/three', 'fixtures/two' ] | ||
}); | ||
// Sync | ||
console.log(list('fixtures').filter(isDir)); | ||
```bash | ||
npm i -d && npm test | ||
``` | ||
## Contributing | ||
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/dirs/issues) | ||
@@ -66,3 +49,3 @@ ## Author | ||
## License | ||
Copyright (c) 2014 Jon Schlinkert, contributors. | ||
Copyright (c) 2015 Jon Schlinkert | ||
Released under the MIT license | ||
@@ -72,2 +55,2 @@ | ||
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on September 05, 2014._ | ||
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on March 22, 2015._ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1
4592
4
53
54
+ Addedasync@^0.9.0
+ Addedasync@0.9.2(transitive)
- Removedis-directory@^0.2.1
- Removedis-directory@0.2.3(transitive)