Skip to content

Commit

Permalink
use .path inside .dir (replaces findit)
Browse files Browse the repository at this point in the history
  • Loading branch information
fb55 committed Sep 7, 2012
1 parent 5d32a73 commit e432496
Showing 1 changed file with 47 additions and 47 deletions.
94 changes: 47 additions & 47 deletions lib/require-analyzer.js
Expand Up @@ -14,8 +14,7 @@ var util = require('util'),
readInstalled = require('read-installed'),
detective = require('detective'),
resolve = require('resolve'),
semver = require('semver'),
findit = require('findit');
semver = require('semver');

var analyzer = exports;

Expand Down Expand Up @@ -192,6 +191,14 @@ analyzer.npmAnalyze = function (deps, options, callback) {
return emitter;
};

function filterFiles(file){
//
// If the file is not `.js` or `.coffee` do no analyze it
//
var ext = path.extname(file);
return ext === '.js' || ext === '.coffee';
}

//
// ### function package (dir, callback)
// #### @dir {string} Parent directory to analyze
Expand All @@ -200,11 +207,13 @@ analyzer.npmAnalyze = function (deps, options, callback) {
// running `analyzer.package()` if it exists. Otherwise attempts to run
// `analyzer.file()` on all files in the source tree.
//
analyzer.dir = function (options, callback) {
analyzer.dir = function (options, callback) {
var target = path.resolve(__dirname, options.target);

//
// Read the target directory
//
fs.readdir(options.target, function (err, files) {
fs.readdir(target, function (err, files) {
if (err) {
return callback(err);
}
Expand All @@ -213,72 +222,63 @@ analyzer.dir = function (options, callback) {
// If there is a package.json in the directory
// then analyze the require(s) based on `package.main`
//
if ((options.target && files.indexOf(options.target) !== -1)
|| files.indexOf('package.json') !== -1) {
if (files.indexOf('package.json') !== -1 ||
(options.target && files.indexOf(options.target) !== -1) // TODO undestand this
) {
return analyzer.package(options, callback);
}

var remaining = files.length,
packages = {};

//
// Otherwise find all files in the directory tree
// and attempt to run `analyzer.file()` on each of them
// in parallel.
//
var files = [],
done = [],
packages = {},
traversed = false,
target = path.resolve(__dirname, options.target),
finder = findit.find(target);

function onRequired() {
//
// Respond to the `callback` if all files have been traversed
// and all files have been executed via `analyzer.file()`
//
if (traversed && files.length === done.length) {
callback(null, Object.keys(packages));
}
}

finder.on('file', function (file) {
files.forEach(function(file){
//
// skip all files from "node_modules" directories
// because the checked direcotry might already be
//
// as the checked direcotry might already be
// in a node_modules directory, only the relative path
// is checked
//
var relativePath = path.relative(target, file);
if (relativePath.indexOf("node_modules") >= 0) {
return;
}

//
// If the file is not `.js` or `.coffee` do no analyze it
// call analyzer.path and currate all dependencies
//
var ext = path.extname(file),
clone = analyzer.merge({}, options);

if (ext !== '.js' && ext !== '.coffee') {
return;
}

files.push(file);

clone.target = file;
analyzer.file(clone, function (err, deps) {
deps.forEach(function (dep) {
analyzer.path({
__proto__: options,
target: file,
fileFilter: filterFiles
}, function(err, deps){
if(err && err.code !== "UNSUPPORTED_TYPE"){
//
// skip symlinks & friends
// but forward real errors
//
remaining = -1; //ensures that callback won't be called again
callback(err);
return;
}

deps.forEach(function(dep){
packages[dep] = true;
});

done.push(file);
onRequired();

//
// when all files are analyzed, call the callback
//
if(!--remaining){
callback(null, Object.keys(packages));
}
});
});

finder.on('end', function () {
traversed = true;
onRequired();
});
});
};

Expand Down

0 comments on commit e432496

Please sign in to comment.