|
1 | 1 | import { readNxJson } from '../../../../config/configuration';
|
2 | 2 | import { TouchedProjectLocator } from '../../../../project-graph/affected/affected-project-graph-models';
|
3 |
| -import { WholeFileChange } from '../../../../project-graph/file-utils'; |
4 |
| -import { JsonChange } from '../../../../utils/json-diff'; |
| 3 | +import { |
| 4 | + FileChange, |
| 5 | + WholeFileChange, |
| 6 | +} from '../../../../project-graph/file-utils'; |
| 7 | +import { isJsonChange, JsonChange } from '../../../../utils/json-diff'; |
5 | 8 | import { jsPluginConfig as readJsPluginConfig } from '../../utils/config';
|
6 | 9 | import { findMatchingProjects } from '../../../../utils/find-matching-projects';
|
| 10 | +import { ProjectGraphProjectNode } from '../../../../config/project-graph'; |
| 11 | + |
| 12 | +export const PNPM_LOCK_FILES = ['pnpm-lock.yaml', 'pnpm-lock.yml']; |
| 13 | + |
| 14 | +const ALL_LOCK_FILES = [ |
| 15 | + ...PNPM_LOCK_FILES, |
| 16 | + 'package-lock.json', |
| 17 | + 'yarn.lock', |
| 18 | + 'bun.lockb', |
| 19 | + 'bun.lock', |
| 20 | +]; |
7 | 21 |
|
8 | 22 | export const getTouchedProjectsFromLockFile: TouchedProjectLocator<
|
9 | 23 | WholeFileChange | JsonChange
|
10 | 24 | > = (fileChanges, projectGraphNodes): string[] => {
|
11 | 25 | const nxJson = readNxJson();
|
12 | 26 | const { projectsAffectedByDependencyUpdates } = readJsPluginConfig(nxJson);
|
13 | 27 |
|
| 28 | + const changedLockFile = fileChanges.find((f) => |
| 29 | + ALL_LOCK_FILES.includes(f.file) |
| 30 | + ); |
| 31 | + |
14 | 32 | if (projectsAffectedByDependencyUpdates === 'auto') {
|
15 |
| - return []; |
| 33 | + const changedProjectPaths = |
| 34 | + getProjectPathsAffectedByDependencyUpdates(changedLockFile); |
| 35 | + const changedProjectNames = getProjectsNamesFromPaths( |
| 36 | + projectGraphNodes, |
| 37 | + changedProjectPaths |
| 38 | + ); |
| 39 | + return changedProjectNames; |
16 | 40 | } else if (Array.isArray(projectsAffectedByDependencyUpdates)) {
|
17 | 41 | return findMatchingProjects(
|
18 | 42 | projectsAffectedByDependencyUpdates,
|
19 | 43 | projectGraphNodes
|
20 | 44 | );
|
21 | 45 | }
|
22 | 46 |
|
23 |
| - const lockFiles = [ |
24 |
| - 'package-lock.json', |
25 |
| - 'yarn.lock', |
26 |
| - 'pnpm-lock.yaml', |
27 |
| - 'pnpm-lock.yml', |
28 |
| - 'bun.lockb', |
29 |
| - 'bun.lock', |
30 |
| - ]; |
31 |
| - |
32 |
| - if (fileChanges.some((f) => lockFiles.includes(f.file))) { |
| 47 | + if (changedLockFile) { |
33 | 48 | return Object.values(projectGraphNodes).map((p) => p.name);
|
34 | 49 | }
|
35 | 50 | return [];
|
36 | 51 | };
|
| 52 | + |
| 53 | +/** |
| 54 | + * For pnpm projects, check lock file for changes to importers and return the project paths that have changes. |
| 55 | + */ |
| 56 | +const getProjectPathsAffectedByDependencyUpdates = ( |
| 57 | + changedLockFile?: FileChange<WholeFileChange | JsonChange> |
| 58 | +): string[] => { |
| 59 | + if (!changedLockFile) { |
| 60 | + return []; |
| 61 | + } |
| 62 | + const changedProjectPaths = new Set<string>(); |
| 63 | + if (PNPM_LOCK_FILES.includes(changedLockFile.file)) { |
| 64 | + for (const change of changedLockFile.getChanges()) { |
| 65 | + if ( |
| 66 | + isJsonChange(change) && |
| 67 | + change.path[0] === 'importers' && |
| 68 | + change.path[1] !== undefined |
| 69 | + ) { |
| 70 | + changedProjectPaths.add(change.path[1]); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + return Array.from(changedProjectPaths); |
| 75 | +}; |
| 76 | + |
| 77 | +const getProjectsNamesFromPaths = ( |
| 78 | + projectGraphNodes: Record<string, ProjectGraphProjectNode>, |
| 79 | + projectPaths: string[] |
| 80 | +): string[] => { |
| 81 | + const lookup = new RootPathLookup(projectGraphNodes); |
| 82 | + return projectPaths.map((path) => { |
| 83 | + return lookup.findNodeNameByRoot(path); |
| 84 | + }); |
| 85 | +}; |
| 86 | + |
| 87 | +class RootPathLookup { |
| 88 | + private rootToNameMap: Map<string, string>; |
| 89 | + |
| 90 | + constructor(nodes: Record<string, ProjectGraphProjectNode>) { |
| 91 | + this.rootToNameMap = new Map(); |
| 92 | + Object.entries(nodes).forEach(([name, node]) => { |
| 93 | + this.rootToNameMap.set(node.data.root, name); |
| 94 | + }); |
| 95 | + } |
| 96 | + |
| 97 | + findNodeNameByRoot(root: string): string | undefined { |
| 98 | + return this.rootToNameMap.get(root); |
| 99 | + } |
| 100 | +} |
0 commit comments