Skip to content

Commit

Permalink
fix(core): mark all projects as affected when npm package is deleted (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysoo committed Feb 19, 2020
1 parent f006593 commit f1599f5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,29 @@ describe('getTouchedNpmPackages', () => {
lhs: '0.0.1',
rhs: '0.0.2'
}
},
// If it's deleted then it should not exist in project graph.
}
]
}
],
workspaceJson,
nxJson,
{
dependencies: {
'happy-nrwl': '0.0.2'
}
}
);
expect(result).toEqual(['happy-nrwl']);
});

it('should handle package deletion', () => {
const result = getTouchedNpmPackages(
[
{
file: 'package.json',
mtime: 0,
ext: '.json',
getChanges: () => [
{
type: DiffType.Deleted,
path: ['dependencies', 'sad-nrwl'],
Expand Down Expand Up @@ -88,6 +109,6 @@ describe('getTouchedNpmPackages', () => {
}
}
);
expect(result).toEqual(['happy-nrwl', 'awesome-nrwl']);
expect(result).toEqual(['proj1', 'proj2']);
});
});
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
import { isWholeFileChange, WholeFileChange } from '../../file-utils';
import { isJsonChange, JsonChange } from '../../../utils/json-diff';
import { DiffType, isJsonChange, JsonChange } from '../../../utils/json-diff';
import { TouchedProjectLocator } from '../affected-project-graph-models';

export const getTouchedNpmPackages: TouchedProjectLocator<
WholeFileChange | JsonChange
> = (touchedFiles, workspaceJson, nxJson, packageJson): string[] => {
const packageJsonChange = touchedFiles.find(f => f.file === 'package.json');
const touched = [];
if (packageJsonChange) {
const changes = packageJsonChange.getChanges();
changes.forEach(c => {
if (
isJsonChange(c) &&
(c.path[0] === 'dependencies' || c.path[0] === 'devDependencies') &&
c.value.rhs // If rhs is blank then dep was deleted and does not exist in project graph
) {
touched.push(c.path[1]);
} else if (isWholeFileChange(c)) {
Object.keys({
if (!packageJsonChange) return [];

let touched = [];
const changes = packageJsonChange.getChanges();

for (const c of changes) {
if (
isJsonChange(c) &&
(c.path[0] === 'dependencies' || c.path[0] === 'devDependencies')
) {
// A package was deleted so mark all packages as touched
// so projects with any package dependency will be affected.
if (c.type === DiffType.Deleted) {
touched = Object.keys({
...(packageJson.dependencies || {}),
...(packageJson.devDependencies || {})
}).forEach(p => {
touched.push(p);
});
break;
} else {
touched.push(c.path[1]);
}
});
} else if (isWholeFileChange(c)) {
touched = Object.keys(workspaceJson.projects);
break;
}
}

return touched;
};

0 comments on commit f1599f5

Please sign in to comment.