Comparing version 0.1.5 to 0.1.7
80
index.js
var through = require('through') | ||
var through2 = require('through2') | ||
, glob = require('glob') | ||
, fs = require('graceful-fs') | ||
, stat = fs.stat | ||
, path = require('path') | ||
module.exports = function(dirname){ | ||
var walker = through(write, end) | ||
var stream = through2(write, flush) | ||
, options = { cwd: dirname | ||
@@ -14,7 +15,6 @@ , strict: true | ||
, queue = [] | ||
, globin = true | ||
fs.exists(dirname, function(exists){ | ||
if (! exists) { | ||
return walker.emit('error', new Error(dirname + ' does not exist')) | ||
return stream.emit('error', new Error(dirname + ' does not exist')) | ||
} | ||
@@ -26,10 +26,14 @@ | ||
glob('**', options) | ||
.on('error', function(err){ walker.emit('error', err) }) | ||
.on('match', stat) | ||
.on('end', function(){ globin = false }) | ||
.on('error', function(err){ stream.emit('error', err) }) | ||
.on('match', onmatch) | ||
}) | ||
return walker | ||
// For non pipe/stream consumption, end will not emit without a consumer | ||
stream.on('data', function(chunk) { | ||
stream.emit('file', chunk.toString()) | ||
}) | ||
function stat(match){ | ||
return stream | ||
function onmatch(match) { | ||
// don't stat empty strings | ||
@@ -40,51 +44,53 @@ if (match.length === 0) return | ||
stream.write(pathname) | ||
} | ||
function write(chunk, enc, callback) { | ||
var pathname = chunk.toString() | ||
queue.push(pathname) | ||
fs.stat(pathname, function(err, stats){ | ||
if (err) return walker.emit('error', err) | ||
stat(pathname, function(err, stats) { | ||
dequeue(pathname) | ||
if (stats.isFile()) { | ||
var file = { filename: pathname | ||
, stats: stats | ||
} | ||
if (err) return callback(err) | ||
if (! stats.isFile()) return callback() | ||
walker.queue(pathname) // for the through pipe | ||
walker.emit('file', pathname) | ||
var file = { | ||
filename: pathname, | ||
stats: stats | ||
} | ||
if (wants('stat')) walker.emit('stat', file) | ||
if (wants('stat')) stream.emit('stat', file) | ||
if (wants('read')) read(file) | ||
else finish(pathname) | ||
if (wants('read')) read(file, callback) | ||
else callback(null, pathname) | ||
} else finish(pathname) | ||
if (queue.length === 0) stream.end() | ||
}) | ||
} | ||
function finish(pathname){ | ||
queue.splice(queue.indexOf(pathname), 1) | ||
if (queue.length === 0 && !globin) { | ||
walker.emit('end') | ||
} | ||
function flush(callback) { | ||
callback() | ||
} | ||
function wants(event){ | ||
return walker.listeners(event).length > 0 | ||
return stream.listeners(event).length > 0 | ||
} | ||
function read(file){ | ||
function dequeue(pathname) { | ||
queue.splice(queue.indexOf(pathname), 1) | ||
} | ||
function read(file, callback){ | ||
fs.readFile(file.filename, 'utf8', function(err, data){ | ||
if (err) return walker.emit('error', err) | ||
if (err) return callback(err) | ||
file.data = data | ||
walker.emit('read', file) | ||
finish(file.filename) | ||
stream.emit('read', file) | ||
callback(null, file.filename) | ||
}) | ||
} | ||
} | ||
// noop for now | ||
function write(data){} | ||
function end(){} |
{ | ||
"name": "powerwalk", | ||
"version": "0.1.5", | ||
"version": "0.1.7", | ||
"description": "Recursively walks a directory and emits filenames. Supports additional stat and read events (if you want them).", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha test/test*.js" | ||
"test": "make test" | ||
}, | ||
@@ -23,11 +23,9 @@ "repository": { | ||
"license": "MIT", | ||
"readmeFilename": "README.md", | ||
"gitHead": "2bce49cc8d60801864b60e94eb58a130caa263af", | ||
"dependencies": { | ||
"graceful-fs": "~2.0.0", | ||
"through": "~2.3.4", | ||
"glob": "~3.2.1" | ||
"glob": "^4.0.5", | ||
"graceful-fs": "^3.0.2", | ||
"through2": "^0.6.1" | ||
}, | ||
"devDependencies": { | ||
"mocha": "~1.12.0" | ||
"prova": "^1.14.0" | ||
}, | ||
@@ -39,3 +37,4 @@ "bugs": { | ||
"test": "test" | ||
} | ||
}, | ||
"homepage": "https://github.com/jxson/powerwalk" | ||
} |
@@ -5,7 +5,8 @@ # powerwalk | ||
> Recursively walks a directory and emits filenames. Supports additional stat and read events (if you want them). | ||
> Recursively walks a directory and streams filenames. | ||
[![Build Status](https://travis-ci.org/jxson/powerwalk.png?branch=master)](https://travis-ci.org/jxson/powerwalk) [![Dependency Status](https://david-dm.org/jxson/powerwalk.png)](https://david-dm.org/jxson/powerwalk) | ||
[![Build Status](https://travis-ci.org/jxson/powerwalk.png?branch=master)](https://travis-ci.org/jxson/powerwalk) | ||
[![Dependency Status](https://david-dm.org/jxson/powerwalk.png)](https://david-dm.org/jxson/powerwalk) | ||
I keep writing and re-writing this code in one form or another for most of my projects. I thought it might be useful to some of you. There are a few similar packages on npm already but none seem to have either the narrow focus I wanted or they use straight `fs` calls which can be harsh when EMFILE happens. | ||
I keep writing and re-writing this code in one form or another for most of my projects. I thought it might be useful to some of you. There are a few similar packages on npm already but none seem to have either a narrow focus, support streams, or use straight `fs` calls which can be harsh when EMFILE happens. | ||
@@ -17,4 +18,4 @@ # EXAMPLES | ||
powerwalk('./content') | ||
.on('file', function(filename){ | ||
console.log(filename) | ||
.on('data', function(filename){ | ||
console.log(filename.toString()) | ||
}) | ||
@@ -25,46 +26,6 @@ | ||
var through = require('through') | ||
, stream = through(write, end) | ||
var stream = through2(write, flush) | ||
powerwalk('./content').pipe(stream) | ||
# powerwalk(dir) | ||
Performs an async walk, returns an event emitter that will execute file calls and emit events appropriately. | ||
var walker = powerwalk('my-directory') | ||
## Event: 'error' | ||
`function(error){ }` | ||
Emitted when an error happens | ||
## Event: 'file' | ||
`function(filename){ }` | ||
Everytime a file is found this is emitted with the **absolute path to the file** as filename. | ||
## Event: 'stat' | ||
`function(file){ }` | ||
If there is a listener for the `stat` event an `fs.stat` call will be made and emit this event with a file object | ||
## Event: 'read' | ||
`function(file){ }` | ||
If there is a listener for the `read` event an `fs.readFile` call will be made and emit this event with a file object. | ||
## Event: 'end' | ||
`function(end){ }` | ||
Emitted when the walk is over. | ||
# File Object | ||
Objects emitted from `stat` and and `read` events will have 3 properties: | ||
* `filename` - the absolute pathname for the file | ||
* `stats` - the stats result for the file | ||
* `data` - the contents of the file | ||
# LICENSE (MIT) | ||
@@ -71,0 +32,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
1
6165
11
89
37
+ Addedthrough2@^0.6.1
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbrace-expansion@1.1.11(transitive)
+ Addedconcat-map@0.0.1(transitive)
+ Addedcore-util-is@1.0.3(transitive)
+ Addedglob@4.5.3(transitive)
+ Addedgraceful-fs@3.0.12(transitive)
+ Addedinflight@1.0.6(transitive)
+ Addedisarray@0.0.1(transitive)
+ Addedminimatch@2.0.10(transitive)
+ Addednatives@1.1.6(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedreadable-stream@1.0.34(transitive)
+ Addedstring_decoder@0.10.31(transitive)
+ Addedthrough2@0.6.5(transitive)
+ Addedwrappy@1.0.2(transitive)
+ Addedxtend@4.0.2(transitive)
- Removedthrough@~2.3.4
- Removedglob@3.2.11(transitive)
- Removedgraceful-fs@2.0.3(transitive)
- Removedlru-cache@2.7.3(transitive)
- Removedminimatch@0.3.0(transitive)
- Removedsigmund@1.0.1(transitive)
- Removedthrough@2.3.8(transitive)
Updatedglob@^4.0.5
Updatedgraceful-fs@^3.0.2