fix(bazel): Make sure only single copy of @angular/bazel is installed (#30072)

When `ng add` is invoked independently of `ng new`, a node installation
of `@angular/bazel` is performed by the CLI before invoking the
schematic. This step appends `@angular/bazel` to the `dependencies`
section of `package.json`. The schematics then appends the same package
to `devDependencies`.

This leads to the warning:

```
warning package.json: "dependencies" has dependency "@angular/bazel" with
range "^8.0.0-beta.13" that collides with a dependency in "devDependencies"
of the same name with version "~8.0.0-beta.12"
```

PR Close #30072
This commit is contained in:
Keen Yee Liau
2019-04-23 12:51:13 -07:00
committed by Andrew Kushnir
parent e8d3246c6e
commit 2905bf5548
3 changed files with 27 additions and 8 deletions

View File

@ -55,13 +55,19 @@ function addDevDependenciesToPackageJson(options: Schema) {
};
const recorder = host.beginUpdate(packageJson);
const depsToInstall = Object.keys(devDependencies).filter((name) => {
return !findPropertyInAstObject(devDeps, name);
});
for (const packageName of depsToInstall) {
for (const packageName of Object.keys(devDependencies)) {
const existingDep = findPropertyInAstObject(deps, packageName);
if (existingDep) {
const content = packageJsonContent.toString();
removeKeyValueInAstObject(recorder, content, deps, packageName);
}
const version = devDependencies[packageName];
const indent = 4;
insertPropertyInAstObjectInOrder(recorder, devDeps, packageName, version, indent);
if (findPropertyInAstObject(devDeps, packageName)) {
replacePropertyInAstObject(recorder, devDeps, packageName, version, indent);
} else {
insertPropertyInAstObjectInOrder(recorder, devDeps, packageName, version, indent);
}
}
host.commitUpdate(recorder);
return host;