Skip to content

Commit

Permalink
basic component graph with dependencies labeled (#2311)
Browse files Browse the repository at this point in the history
* basic component graph with dependencies labeled

* change the graph to have the components as the content of the nodes, also change the parameter to pass BitId[] instead of Component[]
  • Loading branch information
davidfirst committed Feb 9, 2020
1 parent f3fc0b4 commit d6227d8
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions src/scope/graph/components-graph.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import graphLib, { Graph } from 'graphlib';
import R from 'ramda';
import Component from '../../consumer/component/consumer-component';
import Dependencies from '../../consumer/component/dependencies/dependencies';
import Dependencies, { DEPENDENCIES_TYPES } from '../../consumer/component/dependencies/dependencies';
import loadFlattenedDependenciesForCapsule from '../../consumer/component-ops/load-flattened-dependencies';
import ComponentWithDependencies from '../component-dependencies';
import GeneralError from '../../error/general-error';
import { ComponentsAndVersions } from '../scope';
import { BitId } from '../../bit-id';
import { BitId, BitIds } from '../../bit-id';
import { Consumer } from '../../consumer';
import { Dependency } from '../../consumer/component/dependencies';

export type AllDependenciesGraphs = {
graphDeps: Graph;
Expand Down Expand Up @@ -61,6 +64,40 @@ export function buildOneGraphForComponentsAndMultipleVersions(components: Compon
return graph;
}

/**
* returns one graph that includes all dependencies types. each edge has a label of the dependency
* type. the nodes content is the Component object.
*/
export async function buildOneGraphForComponents(ids: BitId[], consumer: Consumer): Promise<Graph> {
const graph = new Graph();
const { components } = await consumer.loadComponents(BitIds.fromArray(ids));
const componentsWithDeps = await Promise.all(
components.map(component => loadFlattenedDependenciesForCapsule(consumer, component))
);
const allComponents: Component[] = R.flatten(componentsWithDeps.map(c => [c.component, ...c.allDependencies]));

// set vertices
allComponents.forEach(component => {
const idStr = component.id.toStringWithoutVersion();
if (!graph.hasNode(idStr)) graph.setNode(idStr, component);
});

// set edges
allComponents.forEach((component: Component) => {
DEPENDENCIES_TYPES.forEach(depType => {
component[depType].get().forEach((dependency: Dependency) => {
const depIdStr = dependency.id.toStringWithoutVersion();
graph.setEdge(component.id.toStringWithoutVersion(), depIdStr, depType);
});
});
});

// uncomment to print the graph content
// console.log('graph', graphLib.json.write(graph))

return graph;
}

function _setGraphEdges(bitId: BitId, dependencies: Dependencies, graph: Graph) {
const id = bitId.toString();
dependencies.get().forEach(dependency => {
Expand Down

0 comments on commit d6227d8

Please sign in to comment.