Permalink
Browse files

simplified mergeDependencies & .package

• removed scanning scripts as they weren't implemented properly (they
just looped a couple of times without doing anything)
• simplified signature of mergeDependencies
• return errors in .package
• also updated some comments
  • Loading branch information...
1 parent 873a641 commit e169c9ebe6a3eda1f1bef5badc86aed3a9ce73cb @fb55 fb55 committed Sep 8, 2012
Showing with 26 additions and 54 deletions.
  1. +26 −54 lib/require-analyzer.js
View
@@ -108,7 +108,7 @@ analyzer.path = function(options, callback){
// #### @deps {Array} List of dependencies to analyze.
// #### @options {Object} Set of options to analyze with.
// #### @callback {function} Continuation to respond to when complete.
-// Analyzes the list of dependencies using `npm`, consumes the options:
+// Analyzes the list of dependencies using `read-installed`, consumes the options:
//
// options.reduce: Will remove deps consumed by sibling deps
//
@@ -284,10 +284,6 @@ analyzer.dir = function (options, callback) {
// the require statements in the script located at `package.main`
//
analyzer.package = function (options, callback) {
- var deps = {},
- pkgDeps = {},
- devDeps = {},
- bdlDeps = {};
//
// Attempt to read the package.json in the current directory
//
@@ -298,9 +294,6 @@ analyzer.package = function (options, callback) {
// Attempt to read the package.json data.
//
pkg = JSON.parse(pkg.toString());
- pkgDeps = pkg.dependencies;
- devDeps = pkg.devDependencies;
- bdlDeps = pkg.bundleDependencies;
}
catch (e) {
return callback(e);
@@ -314,7 +307,6 @@ analyzer.package = function (options, callback) {
//
// Analyze the require(s) based on:
// - the `main` property of the package.json
- // - the scripts in the options that relate to package.json
// - the default file if no package.json exists
//
var todo = 0,
@@ -323,7 +315,8 @@ analyzer.package = function (options, callback) {
function dequeue(err) {
todo--;
if (todo === 0) {
- mergeDependencies(err, _deps, pkgDeps, devDeps, bdlDeps, callback);
+ if(err) callback(err);
+ else mergeDependencies(_deps, pkg, callback);
}
}
@@ -332,39 +325,13 @@ analyzer.package = function (options, callback) {
analyzer.file(options, function (err, deps) {
_deps = _deps.concat(deps.filter(function (d) {
- return _deps.indexOf(d) === -1 && d !== pkg.name;
+ return d !== pkg.name && _deps.indexOf(d) === -1;
}));
dequeue(err);
});
}
- var scripts = 'scripts' in options ? options.scripts : ['test','prestart'];
-
- scripts = scripts.map(function (item) {
- return pkg.scripts && pkg.scripts[item];
- }).filter(Boolean);
-
- if (scripts) {
- scripts.forEach(function analyzeScript(script) {
- if (!script) {
- return;
- }
-
- var newoptions = analyzer.clone(options);
- try {
- newoptions.target = require.resolve(path.join(newoptions.target, path.normalize(pkg.main || '/')));
- }
- catch (e) {
- todo = 1;
- deps = null;
- dequeue(e);
- }
-
- processOptions(newoptions);
- });
- }
-
var newoptions = analyzer.clone(options);
function setMain(files, pkg, newoptions, callback) {
@@ -395,21 +362,20 @@ analyzer.package = function (options, callback) {
}
function setTarget(pkg, newoptions) {
- try {
- newoptions.target = require.resolve(path.join(newoptions.target, path.normalize(pkg.main || '/')));
- }
- catch (e) {
+ var newPath = path.join(newoptions.target, pkg.main ? path.normalize(pkg.main) : '/'),
+ newTarget = analyzer.resolve(newPath);
+
+ if (newTarget === false) {
todo = 1;
deps = null;
- dequeue(e);
+ dequeue(new Error('Couldn\'t resolve path ' + newPath));
}
return processOptions(newoptions);
}
// add logic to default to app.js or server.js for main if main is not present.
if ( !('main' in pkg) || pkg.main === '') {
- var files = ['app.js', 'server.js', 'index.js'];
- setMain(files, pkg, newoptions, setTarget);
+ setMain(['app.js', 'server.js', 'index.js'], pkg, newoptions, setTarget);
}
else {
setTarget(pkg, newoptions);
@@ -596,6 +562,7 @@ analyzer.findModulesDir = function (target, callback) {
callback(err);
}
else if (files.indexOf('node_modules') !== -1 || files.indexOf('package.json') !== -1) {
+ //TODO ensure it's actually a directory/file
callback(null, target);
}
else if (target === (target = path.dirname(target))){
@@ -768,27 +735,32 @@ analyzer.resolve = function(file, base){
}
};
-function mergeDependencies(err, deps, pkgDeps, devDeps, bndlDeps, callback) {
+function mergeDependencies(deps, pkg, callback) {
+ var pkgDeps = pkg.dependencies;
+
function removeDevDeps(deps) {
var obj = analyzer.clone(deps), dep;
- for (dep in devDeps) {
- if (dep in obj) {
- delete obj[dep];
+
+ if('devDependencies' in pkg){
+ for (dep in pkg.devDependencies) {
+ if (dep in obj) {
+ delete obj[dep];
+ }
}
}
- for (dep in bndlDeps) {
- if (dep in obj) {
- delete obj[dep];
+
+ if('bundleDependencies' in pkg){
+ for (dep in pkg.bundleDependencies) {
+ if (dep in obj) {
+ delete obj[dep];
+ }
}
}
return obj;
}
var merged = {};
- if (err) {
- return callback(err);
- }
if (!Array.isArray(deps)) {
if (typeof deps === 'undefined' ||
Object.keys(deps).length === 0) {

0 comments on commit e169c9e

Please sign in to comment.