Showing with 51 additions and 27 deletions.
  1. +51 −27 lib/require-analyzer.js
@@ -120,7 +120,7 @@ analyzer.npmAnalyze = function (deps, options, callback) {
return callback();
}

analyzer.findModulesDir(options.target, function (err, root) {
analyzer.findNextDir(options.target, function (err, root) {
if (err) {
return callback(err);
}
@@ -130,13 +130,21 @@ analyzer.npmAnalyze = function (deps, options, callback) {
// Then see if it depends on any other dependencies that are in the
// list so those dependencies may be removed (only if `options.reduce` is set).
//
readInstalled(root, function (err, result) {
readInstalled(root, 1, function (err, result) {
if (err) {
return callback(err);
}
else if (!result || !result.dependencies || Object.keys(result.dependencies).length === 0) {
// When no dependencies were found, return what we got
return callback(null, deps);
if(Array.isArray(deps)){
return callback(null, deps.reduce(function(obj, prop){
obj[prop] = "*";
return obj;
}, {}));
}
else {
return callback(null, deps);
}
}

Object.keys(result.dependencies).forEach(function (pkg) {
@@ -217,9 +225,7 @@ analyzer.dir = function (options, callback) {
// If there is a package.json in the directory
// then analyze the require(s) based on `package.main`
//
if (files.indexOf('package.json') !== -1 ||
(options.target && files.indexOf(options.target) !== -1) // TODO undestand this
) {
if (files.indexOf('package.json') !== -1) {
return analyzer.package(options, callback);
}

@@ -463,14 +469,16 @@ function analyzeFile (options, callback) {
});
}

var findDepsPath = path.join(__dirname, '..', 'bin', 'find-dependencies');

function spawnWorker (options, callback) {
//
// Spawn the `find-dependencies` bin helper to ensure that we are able to
// bypass any modules which have already been required in the current process.
//
var packages = options.packages,
errs = options.errors,
deps = fork(path.join(__dirname, '..', 'bin', 'find-dependencies'), [options.target], {silent: true});
deps = fork(findDepsPath, [options.target], {silent: true});

deps.send(options.target);

@@ -503,7 +511,6 @@ function spawnWorker (options, callback) {
// Remove the timeout now that we have exited.
//
clearTimeout(timeoutId);

callback();
});
}
@@ -518,8 +525,9 @@ function spawnWorker (options, callback) {
//

analyzer.file = function(options, callback){
options.packages = options.packages || {};
options.errors = options.errors || [];
if(!options.packages) options.packages = {};
if(!options.errors) options.errors = [];

analyzeFile(options, function(err){
if(options.errors.length > 0){
callback(options.errors); //TODO call with real error object
@@ -554,33 +562,49 @@ analyzer.file = function(options, callback){
};

//
// ### function findModulesDir (target)
// #### @target {string} The directory (or file) to search up from
// Searches up from the specified `target` until it finds a directory which contains
// a folder called `node_modules`
// ### function findNextDir (target)
// #### @target {string} The path to search up from
// Searches up from the specified `target` until it finds a directory
//
analyzer.findModulesDir = function (target, callback) {
analyzer.findNextDir = function(target, callback) {
fs.stat(target, function (err, stats) {
if (err) {
callback(err);
}
else if (stats.isDirectory()) {
fs.readdir(target, function (err, files) {
if (err) {
callback(err);
}
//TODO behave differently when a node_modules dir is present
else if (files.indexOf('node_modules') !== -1 || files.indexOf('package.json') !== -1) {
callback(null, target);
}
else {
callback(null, target);
}
});
callback(null, target);
}
else if (stats.isFile()) {
analyzer.findModulesDir(path.dirname(target), callback);
}
else {
callback(new Error(target + ' is not a file or a directory.'));
}
});
};

//
// ### function findModulesDir (target)
// #### @target {string} The directory (or file) to search up from
// Searches up from the specified `target` until it finds a directory which contains
// a folder called `node_modules`
//
analyzer.findModulesDir = function (target, callback) {
analyzer.findNextDir(target, function(err, dir){
fs.readdir(target, function (err, files) {
if (err) {
callback(err);
}
else if (files.indexOf('node_modules') !== -1 || files.indexOf('package.json') !== -1) {
callback(null, target);
}
else if (target === (target = path.dirname(target))){
callback(new Error('Couldn\'t find a node_modules directory.'));
}
else {
analyzer.findModulesDir(target, callback);
}
});
});
};