mirror of
https://github.com/LukeHagar/vercel.git
synced 2025-12-08 04:22:09 +00:00
[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:
@@ -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,
|
||||
};
|
||||
Reference in New Issue
Block a user