Skip to content

Commit

Permalink
fix(devkit): do not duplicate dependencies (nrwl#10921)
Browse files Browse the repository at this point in the history
  • Loading branch information
Coly010 authored Aug 3, 2022
1 parent 1c66a22 commit f4ce897
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 3 deletions.
68 changes: 68 additions & 0 deletions packages/devkit/src/utils/package-json.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,72 @@ describe('addDependenciesToPackageJson', () => {
});
expect(installTask).toBeDefined();
});

it('should not add dependencies when they exist in devDependencies or vice versa', () => {
// ARRANGE
writeJson(tree, 'package.json', {
dependencies: {
'@nrwl/angular': 'latest',
},
devDependencies: {
'@nrwl/next': 'latest',
},
});

// ACT
const installTask = addDependenciesToPackageJson(
tree,
{
'@nrwl/next': 'next',
},
{
'@nrwl/angular': 'next',
}
);

// ASSERT
const { dependencies, devDependencies } = readJson(tree, 'package.json');
expect(dependencies).toEqual({
'@nrwl/angular': 'latest',
});
expect(devDependencies).toEqual({
'@nrwl/next': 'latest',
});
expect(installTask).toBeDefined();
});

it('should add additional dependencies when they dont exist in devDependencies or vice versa and not update the ones that do exist', () => {
// ARRANGE
writeJson(tree, 'package.json', {
dependencies: {
'@nrwl/angular': 'latest',
},
devDependencies: {
'@nrwl/next': 'latest',
},
});

// ACT
const installTask = addDependenciesToPackageJson(
tree,
{
'@nrwl/next': 'next',
'@nrwl/cypress': 'latest',
},
{
'@nrwl/angular': 'next',
}
);

// ASSERT
const { dependencies, devDependencies } = readJson(tree, 'package.json');
expect(dependencies).toEqual({
'@nrwl/angular': 'latest',
'@nrwl/cypress': 'latest',
});
expect(devDependencies).toEqual({
'@nrwl/next': 'latest',
});
expect(installTask).toBeDefined();
});
});
32 changes: 29 additions & 3 deletions packages/devkit/src/utils/package-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@ import { installPackagesTask } from '../tasks/install-packages-task';
import type { Tree } from 'nx/src/generators/tree';
import { GeneratorCallback } from 'nx/src/config/misc-interfaces';

function filterExistingDependencies(
dependencies: Record<string, string>,
existingDependencies: Record<string, string>
) {
if (!existingDependencies) {
return dependencies;
}

return Object.keys(dependencies ?? {})
.filter((d) => !existingDependencies[d])
.reduce((acc, d) => ({ ...acc, [d]: dependencies[d] }), {});
}

/**
* Add Dependencies and Dev Dependencies to package.json
*
Expand All @@ -26,18 +39,31 @@ export function addDependenciesToPackageJson(
): GeneratorCallback {
const currentPackageJson = readJson(tree, packageJsonPath);

const filteredDependencies = filterExistingDependencies(
dependencies,
currentPackageJson.devDependencies
);
const filteredDevDependencies = filterExistingDependencies(
devDependencies,
currentPackageJson.dependencies
);

if (
requiresAddingOfPackages(currentPackageJson, dependencies, devDependencies)
requiresAddingOfPackages(
currentPackageJson,
filteredDependencies,
filteredDevDependencies
)
) {
updateJson(tree, packageJsonPath, (json) => {
json.dependencies = {
...(json.dependencies || {}),
...dependencies,
...filteredDependencies,
...(json.dependencies || {}),
};
json.devDependencies = {
...(json.devDependencies || {}),
...devDependencies,
...filteredDevDependencies,
...(json.devDependencies || {}),
};
json.dependencies = sortObjectByKeys(json.dependencies);
Expand Down

0 comments on commit f4ce897

Please sign in to comment.