Comparing version 0.1.1 to 0.1.2
106
index.js
var fs = require('fs'); | ||
var path = require('path'); | ||
var EventEmitter = require('events').EventEmitter; | ||
var Seq = require('seq'); | ||
function createInodeChecker() { | ||
var inodes = {}; | ||
return function inodeSeen(inode) { | ||
if (inodes[inode]) { | ||
return true; | ||
} else { | ||
inodes[inode] = true; | ||
return false; | ||
} | ||
} | ||
} | ||
exports = module.exports = find; | ||
exports.find = find; | ||
function find (base, cb) { | ||
function find (base, options, cb) { | ||
cb = arguments[arguments.length - 1]; | ||
if (typeof(cb) !== 'function') { | ||
cb = undefined; | ||
} | ||
var em = new EventEmitter; | ||
var inodes = {}; | ||
var inodeSeen = createInodeChecker(); | ||
@@ -17,3 +34,3 @@ function finder (dir, f) { | ||
var p = dir + '/' + file; | ||
fs.stat(p, this.into(p)); | ||
fs.lstat(p, this.into(p)); | ||
}) | ||
@@ -28,3 +45,3 @@ .seq(function () { | ||
if (inodes[stat.ino]) { | ||
if (inodeSeen(stat.ino)) { | ||
// already seen this inode, probably a recursive symlink | ||
@@ -36,3 +53,21 @@ this(null); | ||
if (stat.isDirectory()) { | ||
if (stat.isSymbolicLink()) { | ||
em.emit('link', file, stat); | ||
if (options && options.follow_symlinks) { | ||
path.exists(file, function(exists) { | ||
if (exists) { | ||
fs.readlink(file, function(err, resolvedPath) { | ||
if (err) { | ||
em.emit('error', err); | ||
} else { | ||
finder(path.resolve(path.dir(file), resolvedPath)); | ||
} | ||
}); | ||
} | ||
}); | ||
} else { | ||
this(null); | ||
} | ||
} | ||
else if (stat.isDirectory()) { | ||
em.emit('directory', file, stat); | ||
@@ -45,4 +80,2 @@ finder(file, this); | ||
} | ||
inodes[stat.ino] = true; | ||
} | ||
@@ -55,11 +88,17 @@ }) | ||
fs.stat(base, function (err, s) { | ||
fs.lstat(base, function (err, s) { | ||
if (err) { | ||
em.emit('error', err); | ||
} | ||
else if (s.isDirectory()) { | ||
if (s.isDirectory()) { | ||
finder(base, em.emit.bind(em, 'end')); | ||
} | ||
else if (s.isSymbolicLink()) { | ||
if (cb) cb(base, s); | ||
em.emit('link', base, s); | ||
em.emit('end'); | ||
} | ||
else { | ||
em.emit('file', base); | ||
if (cb) cb(base, s); | ||
em.emit('file', base, s); | ||
em.emit('end'); | ||
@@ -72,23 +111,38 @@ } | ||
exports.findSync = function findSync (dir, cb) { | ||
var rootStat = fs.statSync(dir); | ||
if (!rootStat.isDirectory()) { | ||
if (cb) cb(dir, rootStat); | ||
return [dir]; | ||
exports.findSync = function findSync(dir, options, callback) { | ||
cb = arguments[arguments.length - 1]; | ||
if (typeof(cb) !== 'function') { | ||
cb = undefined; | ||
} | ||
return fs.readdirSync(dir).reduce(function (files, file) { | ||
var p = dir + '/' + file; | ||
var stat = fs.statSync(p); | ||
if (cb) cb(p, stat); | ||
files.push(p); | ||
var inodeSeen = createInodeChecker(); | ||
var files = []; | ||
var fileQueue = []; | ||
var processFile = function processFile(file) { | ||
var stat = fs.lstatSync(file); | ||
if (inodeSeen(stat.ino)) { | ||
return; | ||
} | ||
files.push(file); | ||
cb && cb(file, stat) | ||
if (stat.isDirectory()) { | ||
files.push.apply(files, findSync(p, cb)); | ||
fs.readdirSync(file).forEach(function(f) { fileQueue.push(path.join(file, f)); }); | ||
} else if (stat.isSymbolicLink()) { | ||
if (options && options.follow_symlinks && path.existsSync(file)) { | ||
fileQueue.push(fs.realpathSync(file)); | ||
} | ||
} | ||
return files; | ||
}, []); | ||
}; | ||
/* we don't include the starting directory unless it is a file */ | ||
var stat = fs.lstatSync(dir); | ||
if (stat.isDirectory()) { | ||
fs.readdirSync(dir).forEach(function(f) { fileQueue.push(path.join(dir, f)); }); | ||
} else { | ||
fileQueue.push(dir); | ||
} | ||
while (fileQueue.length > 0) { | ||
processFile(fileQueue.shift()); | ||
} | ||
return files; | ||
}; | ||
exports.find.sync = exports.findSync; |
{ | ||
"name" : "findit", | ||
"version" : "0.1.1", | ||
"version" : "0.1.2", | ||
"description" : "Walk a directory tree.", | ||
@@ -13,2 +13,5 @@ "main" : "./index.js", | ||
}, | ||
"scripts" : { | ||
"test" : "expresso" | ||
}, | ||
"repository" : { | ||
@@ -15,0 +18,0 @@ "type" : "git", |
@@ -5,2 +5,9 @@ var assert = require('assert'); | ||
path.relative = path.relative || function (base, dir) { | ||
return dir.indexOf(base + '/') === 0 | ||
? dir.slice(base.length + 1) | ||
: dir | ||
; | ||
}; | ||
exports.cbSync = function () { | ||
@@ -7,0 +14,0 @@ var files = []; |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
13461
17
322
102
0
1