Skip to content

Commit

Permalink
feat(query-graph): Add toposort() helper
Browse files Browse the repository at this point in the history
  • Loading branch information
evocateur committed May 14, 2019
1 parent 290539b commit 90759c2
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions utils/query-graph/query-graph.js
Expand Up @@ -8,7 +8,6 @@ const PackageGraph = require("@lerna/package-graph");
* @param {!Array.<Package>} packages An array of Packages to build the graph out of
* @param {!boolean} rejectCycles Whether or not to reject cycles
*/

class QueryGraph {
constructor(packages, rejectCycles) {
// Create dependency graph
Expand All @@ -35,7 +34,7 @@ class QueryGraph {
return [this.graph.get(this.cyclicalPackageWithMostDependents.name)];
}

// Otherwise, return any package that does not depend on the pacakge referenced above
// Otherwise, return any package that does not depend on the package referenced above
return Array.from(this.graph.values()).filter(
node => !node.localDependencies.has(this.cyclicalPackageWithMostDependents)
);
Expand All @@ -56,7 +55,7 @@ class QueryGraph {
return availablePackages;
}

// Or, get the next cylical packages
// Or, get the next cyclical packages
if (this.cyclePaths.size && this._onlyCyclesLeft()) {
return this._getNextCycle();
}
Expand All @@ -74,3 +73,31 @@ class QueryGraph {
}

module.exports = QueryGraph;
module.exports.toposort = toposort;

/**
* Sort the input list topologically.
*
* @param {!Array.<Package>} packages An array of Packages to build the list out of
* @param {!boolean} rejectCycles Whether or not to reject cycles
*
* @returns {Array<Package>} a list of Package instances in topological order
*/
function toposort(packages, rejectCycles) {
const graph = new QueryGraph(packages, rejectCycles);
const result = [];

let batch = graph.getAvailablePackages();

while (batch.length) {
for (const node of batch) {
// no need to take() in synchronous loop
result.push(node.pkg);
graph.markAsDone(node);
}

batch = graph.getAvailablePackages();
}

return result;
}

0 comments on commit 90759c2

Please sign in to comment.