Skip to content

Instantly share code, notes, and snippets.

@donabrams
Created September 17, 2015 21:00
Show Gist options
  • Select an option

  • Save donabrams/366dbf4fecf48d046d7c to your computer and use it in GitHub Desktop.

Select an option

Save donabrams/366dbf4fecf48d046d7c to your computer and use it in GitHub Desktop.
List all dependencies for given entry points
var detective = require('detective');
var fs = require('fs');
var _ = require('lodash');
var path = require('path');
var resolve = require('resolve');
var toProcess = [{name: './index.js', basedir: __dirname}];
var entries = {};
while (toProcess.length) {
var next = toProcess.pop();
var requireName = next.name;
var basedir = next.basedir;
var filePath = resolve.sync(requireName, {basedir: basedir, extensions: [ '.js', '.jsx', '.json' ]});
if (!resolve.isCore(filePath) &&
!entries[filePath])
{
entries[filePath] = true;
var isJs = ['.js', '.jsx']
.reduce(
function(acc, ext) {
return acc || endsWith(filePath, ext);
},
false
);
if (isJs) {
var src = fs.readFileSync(filePath);
var requires = detective(src);
requires.forEach(function(requireName) {
toProcess.push({name: requireName, basedir: path.dirname(filePath)});
});
}
}
}
function endsWith(str, end) {
return str.substr(str.length-end.length) === end;
}
console.dir(Object.keys(entries));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment