(blocks: BlockContentDepsWithOrder[])
| 11 | * We are still creating `modulesEdges` which are visualized as dashed lines in the DAG graph. |
| 12 | */ |
| 13 | export function buildDagFromBlocks(blocks: BlockContentDepsWithOrder[]): BlockDependencyDag { |
| 14 | const edges: DagEdge[] = [] |
| 15 | const modulesEdges: DagEdge[] = [] |
| 16 | |
| 17 | const moduleDefinitionsLookup = blocks |
| 18 | .flatMap(block => block.importedModules ?? []) |
| 19 | .reduce<Record<string, true>>((acc, m) => { |
| 20 | acc[m] = true |
| 21 | return acc |
| 22 | }, {}) |
| 23 | |
| 24 | const blocksWithUsedImportedModules = blocks.map(block => { |
| 25 | const usedImportedModules = block.usedVariables.filter( |
| 26 | (variable: string) => moduleDefinitionsLookup[variable] === true |
| 27 | ) |
| 28 | const usedVariables = block.usedVariables.filter((variable: string) => moduleDefinitionsLookup[variable] !== true) |
| 29 | |
| 30 | return { |
| 31 | ...block, |
| 32 | usedVariables, |
| 33 | usedImportedModules, |
| 34 | } |
| 35 | }) |
| 36 | |
| 37 | const nodes = blocksWithUsedImportedModules.reduce<Record<string, DagNode>>((acc, block) => { |
| 38 | acc[block.id] = { |
| 39 | id: block.id, |
| 40 | inputVariables: [], |
| 41 | order: block.order, |
| 42 | importedModules: block.importedModules ?? [], |
| 43 | importedPackages: block.importedPackages ?? [], |
| 44 | outputVariables: [...block.definedVariables], |
| 45 | usedImportedModules: block.usedImportedModules ?? [], |
| 46 | error: block.error ?? null, |
| 47 | } |
| 48 | return acc |
| 49 | }, {}) |
| 50 | |
| 51 | const createEdge = (fromBlockId: string, toBlockId: string, variableName: string) => { |
| 52 | const toNode = nodes[toBlockId] |
| 53 | if (!toNode) { |
| 54 | return |
| 55 | } |
| 56 | |
| 57 | if (!toNode.inputVariables.includes(variableName)) { |
| 58 | toNode.inputVariables.push(variableName) |
| 59 | } |
| 60 | |
| 61 | const fromNode = nodes[fromBlockId] |
| 62 | if (!fromNode) { |
| 63 | return |
| 64 | } |
| 65 | |
| 66 | if (!fromNode.outputVariables.includes(variableName)) { |
| 67 | fromNode.outputVariables.push(variableName) |
| 68 | } |
| 69 | |
| 70 | edges.push({ |
no test coverage detected