dependency-tree
Advanced tools
Comparing version 0.0.0 to 1.0.0
110
index.js
@@ -1,9 +0,115 @@ | ||
var detective = require('detective-amd'); | ||
var detective = require('detective-amd'), | ||
q = require('q'), | ||
path = require('path'), | ||
fs = require('fs'); | ||
/** | ||
* Recursively find all dependencies (avoiding circular) until travering the entire dependency tree | ||
* and return a flat list of all nodes | ||
* | ||
* @todo Should work for CommonJS | ||
* | ||
* @param {String} filename - The path of the module whose tree to traverse | ||
* @param {String} root - The directory containing all JS files | ||
* | ||
* @returns {Promise} (String[]) => null - Resolves with all unique, visited dependencies | ||
*/ | ||
module.exports.getTreeList = function(filename) { | ||
module.exports.getTreeAsList = function traverse(filename, root) { | ||
var dependencies; | ||
if (typeof traverse.results === 'undefined') { | ||
filename = path.resolve(process.cwd(), filename); | ||
traverse.results = [filename]; | ||
} | ||
if (typeof traverse.visited === 'undefined') { | ||
traverse.visited = {}; | ||
traverse.visited[filename] = true; | ||
} | ||
try { | ||
dependencies = detective(fs.readFileSync(filename)); | ||
} catch(e) { | ||
return done(); | ||
} | ||
if (! dependencies.length) { | ||
return done(); | ||
} | ||
dependencies = avoidLoaders(dependencies); | ||
dependencies = resolveFilepaths(dependencies, filename, root); | ||
dependencies = avoidDuplicates(dependencies, traverse.visited); | ||
traverse.results = traverse.results.concat(dependencies); | ||
return q.all(dependencies.map(function(dep) { | ||
return traverse(dep, root); | ||
})) | ||
.then(function() { | ||
return traverse.results; | ||
}); | ||
}; | ||
function done() { | ||
return q().then(function() { return true; }); | ||
} | ||
/** | ||
* @param {String[]} dependencies - dependencies of the given filename | ||
* @param {String} filename | ||
* @param {String} root | ||
* @return {String[]} | ||
*/ | ||
function resolveFilepaths(dependencies, filename, root) { | ||
return dependencies.map(function(dep) { | ||
var depDir = path.dirname(filename); | ||
// Relative paths are about current file, non-relative are about the root | ||
if (dep.indexOf('..') === 0 || dep.indexOf('.') === 0) { | ||
depDir = path.resolve(root, depDir); | ||
dep = path.resolve(depDir, dep); | ||
} else { | ||
dep = path.resolve(root, dep); | ||
} | ||
if (dep.indexOf('.js') === -1) { | ||
dep = dep += '.js'; | ||
} | ||
return dep; | ||
}); | ||
} | ||
/** | ||
* Note: mutates the cache to note dependencies that were not visited but will be | ||
* @param {String[]} dependencies | ||
* @param {Object} cache - A lookup table of visited nodes | ||
* @return {String[]} | ||
*/ | ||
function avoidDuplicates(dependencies, cache) { | ||
return dependencies.filter(function(dep) { | ||
var wasVisited = !!cache[dep]; | ||
if (!wasVisited) cache[dep] = true; | ||
return !wasVisited; | ||
}); | ||
} | ||
/** | ||
* Returns a list of dependencies that do not include requirejs loaders (like hogan, text, and css) | ||
* @param {String[]} dependencies | ||
* @return {String[]} | ||
*/ | ||
function avoidLoaders(dependencies) { | ||
var avoided = [ | ||
'hgn!', | ||
'css!', | ||
'txt!' | ||
], | ||
pattern = new RegExp(avoided.join('|')); | ||
return dependencies.filter(function(dep) { | ||
return !pattern.test(dep); | ||
}); | ||
} |
{ | ||
"name": "dependency-tree", | ||
"version": "0.0.0", | ||
"version": "1.0.0", | ||
"description": "Utilities for interacting with the dependency tree of a module", | ||
@@ -9,2 +9,5 @@ "main": "index.js", | ||
}, | ||
"bin": { | ||
"tree-as-list": "bin/treeAsList.js" | ||
}, | ||
"repository": { | ||
@@ -26,3 +29,7 @@ "type": "git", | ||
}, | ||
"homepage": "https://github.com/mrjoelkemp/node-dependency-tree" | ||
"homepage": "https://github.com/mrjoelkemp/node-dependency-tree", | ||
"dependencies": { | ||
"detective-amd": "^2.1.4", | ||
"q": "^1.0.1" | ||
} | ||
} |
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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
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
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
4864
108
1
53
0
2
1
+ Addeddetective-amd@^2.1.4
+ Addedq@^1.0.1
+ Addedast-module-types@2.7.1(transitive)
+ Addedbabylon@6.18.0(transitive)
+ Addeddeep-is@0.1.4(transitive)
+ Addeddetective-amd@2.4.0(transitive)
+ Addedescodegen@1.14.3(transitive)
+ Addedesprima@4.0.1(transitive)
+ Addedestraverse@4.3.0(transitive)
+ Addedesutils@2.0.3(transitive)
+ Addedfast-levenshtein@2.0.6(transitive)
+ Addedget-amd-module-type@2.0.5(transitive)
+ Addedlevn@0.3.0(transitive)
+ Addednode-source-walk@3.3.0(transitive)
+ Addedoptionator@0.8.3(transitive)
+ Addedprelude-ls@1.1.2(transitive)
+ Addedq@1.5.1(transitive)
+ Addedsource-map@0.6.1(transitive)
+ Addedtype-check@0.3.2(transitive)
+ Addedword-wrap@1.2.5(transitive)