[scripts] Create internal-dependency-trace script package (#9783)

Adds a new package under `scripts/internal-dependency-trace` that can be used to generate a rendering of any internal file dependency graph.

Default configured to generate the directed dependency graph of `packages/cli/src/index.ts`
This commit is contained in:
Ethan Arrowood
2023-04-10 15:01:17 -06:00
committed by GitHub
parent 8634f9cd7e
commit 136077ab6f
10 changed files with 784 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
const { readFileSync } = require('fs');
const { createHash } = require('crypto');
const { stripRootDirectory } = require('./root-directory');
const hash = p => {
const h = createHash('sha1');
h.update(p);
return h.digest('hex');
};
const line = (p1, p2) =>
` ${hash(p1)}(${stripRootDirectory(p1)}) --> ${hash(p2)}(${stripRootDirectory(
p2
)})\n`;
function generateMermaidOutput(filePath, traceDataPath) {
const traceData = JSON.parse(readFileSync(traceDataPath, 'utf-8'));
const visited = new Set();
let output = 'graph LR\n';
function _generateMermaidOutput(_filePath) {
if (!traceData[_filePath]) return;
if (visited.has(hash(_filePath))) return;
else visited.add(hash(_filePath));
for (const dependency of traceData[_filePath].dependsOn) {
output += line(_filePath, dependency);
_generateMermaidOutput(dependency);
}
}
_generateMermaidOutput(filePath);
return output;
}
module.exports = {
generateMermaidOutput,
};