Created
September 17, 2015 21:00
-
-
Save donabrams/366dbf4fecf48d046d7c to your computer and use it in GitHub Desktop.
List all dependencies for given entry points
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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