diff --git a/packages/js/src/generators/release-version/utils/sort-projects-topologically.spec.ts b/packages/js/src/generators/release-version/utils/sort-projects-topologically.spec.ts index 7dbcf27cfb36d..da9b8d82988a0 100644 --- a/packages/js/src/generators/release-version/utils/sort-projects-topologically.spec.ts +++ b/packages/js/src/generators/release-version/utils/sort-projects-topologically.spec.ts @@ -29,7 +29,7 @@ describe('sortProjectsTopologically', () => { expect(result).toEqual([projectGraph.nodes.project1]); }); - it('should return projects in the correct order', () => { + it('should return [2,1] if 1 depends on 2', () => { const projectGraph = { dependencies: { project1: [ @@ -111,4 +111,146 @@ describe('sortProjectsTopologically', () => { const result = sortProjectsTopologically(projectGraph, projectNodes); expect(result).toEqual(projectNodes); }); + + it('should return [3,2,1] if 1 depends on 2 and 2 depends on 3', () => { + const projectGraph = { + dependencies: { + project1: [ + { + source: 'project1', + target: 'project2', + type: 'static', + }, + ], + project2: [ + { + source: 'project2', + target: 'project3', + type: 'static', + }, + ], + }, + nodes: { + project1: { + name: 'project1', + data: { + root: '', + }, + type: 'app' as const, + }, + project2: { + name: 'project2', + data: { + root: '', + }, + type: 'app' as const, + }, + project3: { + name: 'project3', + data: { + root: '', + }, + type: 'app' as const, + }, + }, + }; + const projectNodes = [ + projectGraph.nodes.project1, + projectGraph.nodes.project2, + projectGraph.nodes.project3, + ]; + const result = sortProjectsTopologically(projectGraph, projectNodes); + expect(result).toEqual([ + projectGraph.nodes.project3, + projectGraph.nodes.project2, + projectGraph.nodes.project1, + ]); + }); + + it('should return [1,2,3,4] if 1 has zero dependencies, 2 has one, 3 has two, and 4 has three', () => { + const projectGraph = { + dependencies: { + project1: [], + project2: [ + { + source: 'project2', + target: 'project1', + type: 'static', + }, + ], + project3: [ + { + source: 'project3', + target: 'project1', + type: 'static', + }, + { + source: 'project3', + target: 'project2', + type: 'static', + }, + ], + project4: [ + { + source: 'project4', + target: 'project3', + type: 'static', + }, + { + source: 'project4', + target: 'project2', + type: 'static', + }, + { + source: 'project4', + target: 'project1', + type: 'static', + }, + ], + }, + nodes: { + project1: { + name: 'project1', + data: { + root: '', + }, + type: 'app' as const, + }, + project2: { + name: 'project2', + data: { + root: '', + }, + type: 'app' as const, + }, + project3: { + name: 'project3', + data: { + root: '', + }, + type: 'app' as const, + }, + project4: { + name: 'project4', + data: { + root: '', + }, + type: 'app' as const, + }, + }, + }; + const projectNodes = [ + projectGraph.nodes.project1, + projectGraph.nodes.project2, + projectGraph.nodes.project3, + projectGraph.nodes.project4, + ]; + const result = sortProjectsTopologically(projectGraph, projectNodes); + expect(result).toEqual([ + projectGraph.nodes.project1, + projectGraph.nodes.project2, + projectGraph.nodes.project3, + projectGraph.nodes.project4, + ]); + }); }); diff --git a/packages/js/src/generators/release-version/utils/sort-projects-topologically.ts b/packages/js/src/generators/release-version/utils/sort-projects-topologically.ts index b89f583a4f276..d78d3fd071a8c 100644 --- a/packages/js/src/generators/release-version/utils/sort-projects-topologically.ts +++ b/packages/js/src/generators/release-version/utils/sort-projects-topologically.ts @@ -39,14 +39,16 @@ export function sortProjectsTopologically( sortedProjects.push(node); // Process each project that depends on the current node - filteredDependencies.forEach((dep) => { - const dependentNode = projectGraph.nodes[dep.source]; - const count = edges.get(dependentNode) - 1; - edges.set(dependentNode, count); - if (count === 0) { - processQueue.push(dependentNode); - } - }); + filteredDependencies + .filter((dep) => dep.target === node.name) + .forEach((dep) => { + const dependentNode = projectGraph.nodes[dep.source]; + const count = edges.get(dependentNode) - 1; + edges.set(dependentNode, count); + if (count === 0) { + processQueue.push(dependentNode); + } + }); } /**