Skip to content
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,9 @@ recursively search for required files.
browserify-graph <file>

Outputs a graph of all required files, starting at `<file>`.

Optionally, you can specify:
--depth/-d <depth> #only go depth levels deep in the graph
--noExternal don't walk external dependencies (anything with node_modules in the path)

browserify-graph --depth <depth> --noExternal <file>
62 changes: 45 additions & 17 deletions browserify-graph
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,66 @@

var detective = require('detective'),
resolve = require('browser-resolve'),
fs = require('fs'),
fs = require('graceful-fs'),
path = require('path'),
async = require('async'),
archy = require('archy');
archy = require('archy'),
optimist = require('optimist');

if (process.argv.length < 3) {
console.error('Usage: browserify-graph <file>');
console.error('Prints out a graph of all modules a file depends on');
console.error('Uses the same dependency resolution as browserify');
process.exit(1);
}
var usageString = 'Usage: browserify-graph -d [depth] <file>'
+ '\nPrints out a graph of all modules a file depends on'
+ '\nUses the same dependency resolution as browserify';

var argv = optimist
.usage(usageString)
.alias('depth', 'd')
.boolean(['noExternal'])
.describe('noExternal', 'Don\'t walk external dependencies')
.demand(1)
.argv;

var IS_CORE = /^[^\\\/]+$/,
file = path.resolve(process.argv[2]),
basedir = path.dirname(file);
file = path.resolve(argv._[0]),
basedir = path.dirname(file),
maxDepth = argv.depth || -1,
cache = {};

get(file, done);
get(file, done, 0);

function get(file, callback) {
function get(file, callback, level) {
if (IS_CORE.test(file)) {
return callback(null, { label: file + ' - builtin', nodes: [] });
}

file = path.resolve(file);
fs.readFile(file, 'utf8', read);

if(argv['noExternal'] && file.indexOf('node_modules') >= 0) {
return callback(null, { label: file + ' - external', nodes: [] });
}

if(maxDepth > 0 && level >= maxDepth) {
return callback(null, { label: path.relative(basedir, file), nodes: [] });
}

if(typeof cache[file] === 'undefined' || !cache[file]) {
cache[file] = {};
fs.readFile(file, 'utf8', read);
} else {
read(null, null);
}

function read(err, contents) {
if (err) {
return callback(err);
}

var requires = detective(contents);

var requires = [];
if(typeof cache[file] === 'undefined' || !cache[file] || !cache[file].requires) {
requires = detective(contents);
cache[file].requires = requires;
} else {
requires = cache[file].requires;
}
async.map(requires, getDep, gotDeps);
}

Expand All @@ -45,7 +73,7 @@ function get(file, callback) {
return callback(err);
}

return get(p, callback);
return get(p, callback, level + 1);
}
}

Expand All @@ -61,4 +89,4 @@ function get(file, callback) {
function done(err, data) {
if (err) throw err;
console.log(archy(data));
}
}
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
{
"name": "browserify-graph",
"version": "0.0.0",
"version": "0.0.1",
"description": "Print a graph of all modules a file depends on",
"dependencies": {
"archy": "~0.0.2",
"async": "~0.2.9",
"browser-resolve": "~1.2.0",
"detective": "~2.2.0"
"detective": "~2.2.0",
"graceful-fs": "^3.0.7",
"optimist": "^0.6.1"
},
"devDependencies": {},
"bin": {
Expand Down