Created
November 25, 2019 11:44
-
-
Save mcasimir/fd3778e556068007f7b5b5fad7f330eb to your computer and use it in GitHub Desktop.
mongodb-deps.js
This file contains 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
const traverse = require('traverse'); | |
const getUniversalModuleTree = require('universal-module-tree'); | |
const { DepGraph } = require('dependency-graph'); | |
const path = require('path'); | |
const matcher = (name) => name.startsWith('@mongo') || | |
name.startsWith('mongo') || | |
name.startsWith('hadron'); | |
(async function main() { | |
const packageJsonPath = path.join(process.cwd(), 'package.json'); | |
const packageJson = require(packageJsonPath); | |
const universalModuleTree = await getUniversalModuleTree(process.cwd()); | |
const graph = buildDependencyGraph(packageJson, universalModuleTree); | |
const dotFormat = graphToDotFormat(graph); | |
console.log(dotFormat); | |
})().catch(console.error); | |
function buildDependencyGraph(packageJson, universalModuleTree) { | |
const graph = new DepGraph(); | |
const packageNodeName = concatNameAndVersion(packageJson); | |
graph.addNode(packageNodeName); | |
traverse(universalModuleTree).forEach( | |
addDependenciesFromModuleTree(graph, packageNodeName, matcher) | |
); | |
return graph; | |
} | |
function addDependenciesFromModuleTree(graph, packageNodeName, matcher) { | |
return function (node) { | |
if (node && node.name && matcher(node.name)) { | |
let prev = null; | |
extractParentsData(this.parents).forEach((data) => { | |
const nodeName = concatNameAndVersion(data); | |
graph.addNode(nodeName); | |
if (prev) { | |
graph.addDependency(prev, nodeName); | |
} | |
prev = nodeName; | |
}); | |
graph.addDependency(packageNodeName, prev); | |
} | |
}; | |
} | |
function extractParentsData(parents) { | |
return parents.filter((parent) => parent.node && parent.node.data) | |
.map((parent) => parent.node.data); | |
} | |
function concatNameAndVersion({ name, version }) { | |
return `${name}@${version}`; | |
} | |
function graphToDotFormat(graph) { | |
const edges = []; | |
for (const [source, destinations] of Object.entries(graph.outgoingEdges)) { | |
for (const destination of destinations) { | |
edges.push(`"${source}" -> "${destination}";`); | |
} | |
} | |
return `digraph G { | |
rankdir=LR; | |
${edges.join('\n')} | |
}`; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment