Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add the actual cycle to the build output #293

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 39 additions & 3 deletions src/default-rules/circular-dependencies/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,44 @@ export const validateRegistries = (maybeRegistries: string | string[] | undefine
export const createNodeName = (node: string, commonPrefix: string): string =>
node.substring(commonPrefix.length, node.length) || commonPrefix;

const createUserOutput = (commonPrefix: string, cycleGraph: graphlib.Graph): string =>
`cyclic root folder: ${commonPrefix}${EOL}${dot.write(cycleGraph)}`;
const createUserOutput = (
commonPrefix: string,
cycleGraph: graphlib.Graph,
cycle: string[],
): string =>
`cyclic root folder: ${commonPrefix}${EOL}${dot.write(cycleGraph)}${EOL}Cycle:${cycle.join(
' -> ',
)}`;

function findShortestCycle(graph: graphlib.Graph): string[] | null {
let shortestCycle: string[] | null = null;
function dfs(node: string, visited: string[], allNodes: string[]): void {
visited.push(node);
allNodes.push(node);

const successors = graph.successors(node);
if (successors) {
for (const successor of successors) {
if (!visited.includes(successor)) {
dfs(successor, visited, allNodes);
} else if (allNodes[0] === successor) {
if (!shortestCycle || allNodes.length < shortestCycle.length) {
shortestCycle = [...allNodes];
}
}
}
}

visited.pop();
allNodes.pop();
}

for (const node of graph.nodes()) {
dfs(node, [], []);
}

return shortestCycle;
}

const createMappedCycleMessage = (cycle: string[], mapping: Mapping, graph: graphlib.Graph) => {
const cycleGraph = new graphlib.Graph();
Expand Down Expand Up @@ -77,7 +113,7 @@ const createMappedCycleMessage = (cycle: string[], mapping: Mapping, graph: grap
);
});

return createUserOutput(commonPrefix, cycleGraph);
return createUserOutput(commonPrefix, cycleGraph, findShortestCycle(cycleGraph) || []);
};

const createCycleMessage = (cycle: string[], graph: graphlib.Graph) =>
Expand Down