Skip to content

Commit

Permalink
fix(package-graph): Throw errors when package names are not unique
Browse files Browse the repository at this point in the history
  • Loading branch information
evocateur committed Aug 20, 2018
1 parent e0a361f commit 387df2b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
23 changes: 23 additions & 0 deletions core/package-graph/index.js
Expand Up @@ -2,6 +2,7 @@

const npa = require("npm-package-arg");
const semver = require("semver");
const ValidationError = require("@lerna/validation-error");

/**
* Represents a node in a PackageGraph.
Expand Down Expand Up @@ -66,6 +67,28 @@ class PackageGraph extends Map {
constructor(packages, graphType = "allDependencies", forceLocal) {
super(packages.map(pkg => [pkg.name, new PackageGraphNode(pkg)]));

if (packages.size !== this.size) {
// weed out the duplicates
const seen = new Map();

for (const { name, location } of packages) {
if (seen.has(name)) {
seen.get(name).push(location);
} else {
seen.set(name, [location]);
}
}

for (const [name, locations] of seen) {
if (locations.length > 1) {
throw new ValidationError(
"ENAME",
[`Package name "${name}" used in multiple packages:`, ...locations].join("\n\t")
);
}
}
}

this.forEach((currentNode, currentName) => {
const graphDependencies =
graphType === "dependencies"
Expand Down
1 change: 1 addition & 0 deletions core/package-graph/package.json
Expand Up @@ -30,6 +30,7 @@
"test": "echo \"Run tests from root\" && exit 1"
},
"dependencies": {
"@lerna/validation-error": "file:../validation-error",
"npm-package-arg": "^6.0.0",
"semver": "^5.5.0"
}
Expand Down
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 387df2b

Please sign in to comment.