forked from nrwl/nx
/
npm-packages.ts
67 lines (61 loc) · 2.27 KB
/
npm-packages.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { isWholeFileChange, WholeFileChange } from '../../file-utils';
import {
JsonDiffType,
isJsonChange,
JsonChange,
} from '../../../utils/json-diff';
import { logger } from '../../../utils/logger';
import { TouchedProjectLocator } from '../affected-project-graph-models';
export const getTouchedNpmPackages: TouchedProjectLocator<
WholeFileChange | JsonChange
> = (touchedFiles, _, nxJson, packageJson, projectGraph): string[] => {
const packageJsonChange = touchedFiles.find((f) => f.file === 'package.json');
if (!packageJsonChange) return [];
let touched = [];
const changes = packageJsonChange.getChanges();
const npmPackages = Object.values(projectGraph.externalNodes);
const missingTouchedNpmPackages: string[] = [];
for (const c of changes) {
if (
isJsonChange(c) &&
(c.path[0] === 'dependencies' || c.path[0] === 'devDependencies') &&
c.path.length === 2
) {
// A package was deleted so mark all workspace projects as touched.
if (c.type === JsonDiffType.Deleted) {
touched = Object.keys(projectGraph.nodes);
break;
} else {
const npmPackage = npmPackages.find(
(pkg) => pkg.data.packageName === c.path[1]
);
if (!npmPackage) {
missingTouchedNpmPackages.push(c.path[1]);
continue;
}
touched.push(npmPackage.name);
// If it was a type declarations package then also mark its corresponding implementation package as affected
if (npmPackage.name.startsWith('npm:@types/')) {
const implementationNpmPackage = npmPackages.find(
(pkg) => pkg.data.packageName === c.path[1].substring(7)
);
if (implementationNpmPackage) {
touched.push(implementationNpmPackage.name);
}
}
}
} else if (isWholeFileChange(c)) {
// Whole file was touched, so all npm packages are touched.
touched = npmPackages.map((pkg) => pkg.name);
break;
}
}
if (missingTouchedNpmPackages.length) {
logger.warn(
`The affected projects might have not been identified properly. The package(s) ${missingTouchedNpmPackages.join(
', '
)} were not found. Please open an issue in Github including the package.json file.`
);
}
return touched;
};