Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add verbose option #123

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ var list = dependencyTree.toList({
* `nonExistent`: array used for storing the list of partial paths that do not exist
* `filter`: a function used to determine if a module (and its subtree) should be included in the dependency tree
- The first argument given to the filter is an absolute filepath to the dependency and the second is the filepath to the currently traversed file. Should return a `Boolean`. If it returns `true`, the module is included in the resulting tree.
* `verbose`: wether to return the tree as a nested list with additional data for each dependency
* `detective`: object with configuration specific to detectives used to find dependencies of a file
- for example `detective.amd.skipLazyLoaded: true` tells the AMD detective to omit inner requires
- See [precinct's usage docs](https://github.com/dependents/node-precinct#usage) for the list of module types you can pass options to.
Expand Down Expand Up @@ -77,6 +78,40 @@ Example:
This structure was chosen to serve as a visual representation of the dependency tree
for use in the [Dependents](https://github.com/mrjoelkemp/sublime-dependents) plugin.

Example using `verbose: true`:

```js
[{
resolved: '/Users/mrjoelkemp/Documents/node-dependency-tree/test/example/extended/a.js',
partial: './test/example/extended/a.js',
dependencies: [{
resolved: '/Users/mrjoelkemp/Documents/node-dependency-tree/test/example/extended/b.js',
partial: './test/example/extended/b.js',
dependencies: [{
resolved: '/Users/mrjoelkemp/Documents/node-dependency-tree/test/example/extended/d.js',
partial: 'test/example/extended/d.js',
dependencies: []
}, {
resolved: '/Users/mrjoelkemp/Documents/node-dependency-tree/test/example/extended/e.js',
partial: 'test/example/extended/e.js',
dependencies: []
}]
}, {
resolved: '/Users/mrjoelkemp/Documents/node-dependency-tree/test/example/extended/c.js',
partial: './test/example/extended/c.js',
dependencies: [{
resolved: '/Users/mrjoelkemp/Documents/node-dependency-tree/test/example/extended/f.js',
partial: 'test/example/extended/f.js',
dependencies: []
}, {
resolved: '/Users/mrjoelkemp/Documents/node-dependency-tree/test/example/extended/g.js',
partial: 'test/example/extended/g.js',
dependencies: []
}]
}
}]
```

##### CLI version

* Assumes a global install: `npm install -g dependency-tree`
Expand Down
38 changes: 32 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const Config = require('./lib/Config');
* Format is a filename -> tree as list lookup table
* @param {Array} [options.nonExistent] - List of partials that do not exist
* @param {Boolean} [options.isListForm=false]
* @param {Boolean} [options.verbose=false] Return a verbose tree in list form
* @param {String|Object} [options.tsConfig] Path to a typescript config (or a preloaded one).
* @return {Object}
*/
Expand All @@ -29,7 +30,7 @@ module.exports = function(options) {

if (!fs.existsSync(config.filename)) {
debug('file ' + config.filename + ' does not exist');
return config.isListForm ? [] : {};
return config.isListForm || config.verbose ? [] : {};
}

const results = traverse(config);
Expand All @@ -43,6 +44,14 @@ module.exports = function(options) {
debug('list form of results requested');

tree = Array.from(results);
} else if (config.verbose) {
debug('verbose form of results requested');

tree = [{
resolved: config.filename,
partial: null,
dependencies: results,
}];
} else {
debug('object form of results requested');

Expand Down Expand Up @@ -126,7 +135,11 @@ module.exports._getDependencies = function(config) {
continue;
}

resolvedDependencies.push(result);
if (config.verbose) {
resolvedDependencies.push({resolved: result, partial: dep});
} else {
resolvedDependencies.push(result);
}
}

return resolvedDependencies;
Expand All @@ -137,7 +150,14 @@ module.exports._getDependencies = function(config) {
* @return {Object|Set}
*/
function traverse(config) {
let subTree = config.isListForm ? new Set() : {};
let subTree;
if (config.isListForm) {
subTree = new Set();
} else if (config.verbose) {
subTree = [];
} else {
subTree = {};
}

debug('traversing ' + config.filename);

Expand All @@ -151,7 +171,7 @@ function traverse(config) {
debug('cabinet-resolved all dependencies: ', dependencies);
// Prevents cycles by eagerly marking the current file as read
// so that any dependent dependencies exit
config.visited[config.filename] = config.isListForm ? [] : {};
config.visited[config.filename] = config.isListForm || config.verbose ? [] : {};

if (config.filter) {
debug('using filter function to filter out dependencies');
Expand All @@ -164,21 +184,27 @@ function traverse(config) {

for (let i = 0, l = dependencies.length; i < l; i++) {
const d = dependencies[i];
const resolved = config.verbose ? d.resolved : d;
const localConfig = config.clone();
localConfig.filename = d;
localConfig.filename = resolved;

if (localConfig.isListForm) {
for (let item of traverse(localConfig)) {
subTree.add(item);
}
} else if (localConfig.verbose) {
d.dependencies = traverse(localConfig);
subTree.push(d);
} else {
subTree[d] = traverse(localConfig);
subTree[resolved] = traverse(localConfig);
}
}

if (config.isListForm) {
subTree.add(config.filename);
config.visited[config.filename].push(...subTree);
} else if (config.verbose) {
config.visited[config.filename] = subTree;
} else {
config.visited[config.filename] = subTree;
}
Expand Down
1 change: 1 addition & 0 deletions lib/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Config {
this.directory = options.directory || options.root;
this.visited = options.visited || {};
this.nonExistent = options.nonExistent || [];
this.verbose = options.verbose;
this.isListForm = options.isListForm;
this.requireConfig = options.config || options.requireConfig;
this.webpackConfig = options.webpackConfig;
Expand Down