Skip to content

Commit

Permalink
feat(core): allow skipping lockfile for affected
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder committed Jun 4, 2024
1 parent dd6eda8 commit f1b930b
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 23 deletions.
1 change: 1 addition & 0 deletions packages/nx/src/config/nx-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export interface NrwlJsPluginConfig {
analyzeSourceFiles?: boolean;
analyzePackageJson?: boolean;
analyzeLockfile?: boolean;
projectsAffectedByDependencyUpdates?: 'all' | 'auto' | string[];
}

interface NxInstallationConfiguration {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
import { readNxJson } from '../../../../config/configuration';
import { TouchedProjectLocator } from '../../../../project-graph/affected/affected-project-graph-models';
import { WholeFileChange } from '../../../../project-graph/file-utils';
import { JsonChange } from '../../../../utils/json-diff';
import { jsPluginConfig as readJsPluginConfig } from '../../utils/config';
import { findMatchingProjects } from '../../../../utils/find-matching-projects';

export const getTouchedProjectsFromLockFile: TouchedProjectLocator<
WholeFileChange | JsonChange
> = (fileChanges, projectGraphNodes): string[] => {
const nxJson = readNxJson();
const { projectsAffectedByDependencyUpdates } = readJsPluginConfig(nxJson);

if (projectsAffectedByDependencyUpdates === 'auto') {
return [];
} else if (Array.isArray(projectsAffectedByDependencyUpdates)) {
return findMatchingProjects(
projectsAffectedByDependencyUpdates,
projectGraphNodes
);
}

const lockFiles = [
'package-lock.json',
'yarn.lock',
Expand Down
18 changes: 18 additions & 0 deletions packages/nx/src/plugins/js/project-graph/affected/npm-packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ import {
ProjectGraphExternalNode,
ProjectGraphProjectNode,
} from '../../../../config/project-graph';
import { NxJsonConfiguration } from '../../../../config/nx-json';
import { getPackageNameFromImportPath } from '../../../../utils/get-package-name-from-import-path';

export const getTouchedNpmPackages: TouchedProjectLocator<
WholeFileChange | JsonChange
> = (touchedFiles, _, nxJson, packageJson, projectGraph): string[] => {
const packageJsonChange = touchedFiles.find((f) => f.file === 'package.json');
if (!packageJsonChange) return [];

const globalPackages = new Set(getGlobalPackages(nxJson.plugins));

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

Expand Down Expand Up @@ -59,6 +63,12 @@ export const getTouchedNpmPackages: TouchedProjectLocator<
touched.push(implementationNpmPackage.name);
}
}

if ('packageName' in npmPackage.data) {
if (globalPackages.has(npmPackage.data.packageName)) {
return Object.keys(projectGraph.nodes);
}
}
}
} else if (isWholeFileChange(c)) {
// Whole file was touched, so all npm packages are touched.
Expand All @@ -76,3 +86,11 @@ export const getTouchedNpmPackages: TouchedProjectLocator<
}
return touched;
};

function getGlobalPackages(plugins: NxJsonConfiguration['plugins']) {
return (plugins ?? [])
.map((p) =>
getPackageNameFromImportPath(typeof p === 'string' ? p : p.plugin)
)
.concat('nx');
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
getRootTsConfigFileName,
resolveModuleByImport,
} from '../../utils/typescript';

import { getPackageNameFromImportPath } from '../../../../utils/get-package-name-from-import-path';
/**
* The key is a combination of the package name and the workspace relative directory
* containing the file importing it e.g. `lodash__packages/my-lib`, the value is the
Expand All @@ -36,7 +36,7 @@ const builtInModuleSet = new Set<string>([
]);

export function isBuiltinModuleImport(importExpr: string): boolean {
const packageName = parsePackageNameFromImportExpression(importExpr);
const packageName = getPackageNameFromImportPath(importExpr);
return builtInModuleSet.has(packageName);
}

Expand Down Expand Up @@ -153,7 +153,7 @@ export class TargetProjectLocator {
importExpr: string,
fromFilePath: string
): string | null {
const packageName = parsePackageNameFromImportExpression(importExpr);
const packageName = getPackageNameFromImportPath(importExpr);

let fullFilePath = fromFilePath;
let workspaceRelativeFilePath = fromFilePath;
Expand Down Expand Up @@ -362,15 +362,3 @@ export class TargetProjectLocator {
}
}
}

function parsePackageNameFromImportExpression(
importExpression: string
): string {
// Check if the package is scoped
if (importExpression.startsWith('@')) {
// For scoped packages, the package name is up to the second '/'
return importExpression.split('/').slice(0, 2).join('/');
}
// For unscoped packages, the package name is up to the first '/'
return importExpression.split('/')[0];
}
4 changes: 4 additions & 0 deletions packages/nx/src/plugins/js/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export function jsPluginConfig(
analyzePackageJson: true,
analyzeSourceFiles: true,
analyzeLockfile,
projectsAffectedByDependencyUpdates: 'all',
...nxJsonConfig,
};
}
Expand All @@ -38,6 +39,7 @@ export function jsPluginConfig(
analyzeLockfile: false,
analyzePackageJson: false,
analyzeSourceFiles: false,
projectsAffectedByDependencyUpdates: 'all',
};
}

Expand Down Expand Up @@ -69,12 +71,14 @@ export function jsPluginConfig(
analyzePackageJson: true,
analyzeLockfile,
analyzeSourceFiles: true,
projectsAffectedByDependencyUpdates: 'all',
};
} else {
return {
analyzePackageJson: true,
analyzeLockfile,
analyzeSourceFiles: false,
projectsAffectedByDependencyUpdates: 'all',
};
}
}
16 changes: 8 additions & 8 deletions packages/nx/src/project-graph/affected/affected-project-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ function filterAffectedProjects(
};
const reversed = reverse(graph);
ctx.touchedProjects.forEach((p) => {
addAffectedNodes(p, reversed, result, []);
addAffectedNodes(p, reversed, result, new Set());
});
ctx.touchedProjects.forEach((p) => {
addAffectedDependencies(p, reversed, result, []);
addAffectedDependencies(p, reversed, result, new Set());
});
return result;
}
Expand All @@ -72,15 +72,15 @@ function addAffectedNodes(
startingProject: string,
reversed: ProjectGraph,
result: ProjectGraph,
visited: string[]
visited: Set<string>
): void {
if (visited.indexOf(startingProject) > -1) return;
if (visited.has(startingProject)) return;
const reversedNode = reversed.nodes[startingProject];
const reversedExternalNode = reversed.externalNodes[startingProject];
if (!reversedNode && !reversedExternalNode) {
throw new Error(`Invalid project name is detected: "${startingProject}"`);
}
visited.push(startingProject);
visited.add(startingProject);
if (reversedNode) {
result.nodes[startingProject] = reversedNode;
result.dependencies[startingProject] = [];
Expand All @@ -96,10 +96,10 @@ function addAffectedDependencies(
startingProject: string,
reversed: ProjectGraph,
result: ProjectGraph,
visited: string[]
visited: Set<string>
): void {
if (visited.indexOf(startingProject) > -1) return;
visited.push(startingProject);
if (visited.has(startingProject)) return;
visited.add(startingProject);
if (reversed.dependencies[startingProject]) {
reversed.dependencies[startingProject].forEach(({ target }) =>
addAffectedDependencies(target, reversed, result, visited)
Expand Down
13 changes: 13 additions & 0 deletions packages/nx/src/utils/get-package-name-from-import-path.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { getPackageNameFromImportPath } from './get-package-name-from-import-path';

describe('getPackageNameFromImportPath', () => {
it.each([
['@nx/workspace', '@nx/workspace'],
['@nx/workspace/plugin', '@nx/workspace'],
['@nx/workspace/other', '@nx/workspace'],
['nx/plugin', 'nx'],
['nx', 'nx'],
])('should return %s for %s', (input, expected) => {
expect(getPackageNameFromImportPath(input)).toEqual(expected);
});
});
14 changes: 14 additions & 0 deletions packages/nx/src/utils/get-package-name-from-import-path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//# Converts import paths to package names.
//# e.g. - `@nx/workspace` -> `@nx/workspace`
//# - `@nx/workspace/plugin` -> `@nx/workspace`
//# - `@nx/workspace/other` -> `@nx/workspace`
//# - `nx/plugin` -> `nx`
export function getPackageNameFromImportPath(importExpression: string) {
// Check if the package is scoped
if (importExpression.startsWith('@')) {
// For scoped packages, the package name is up to the second '/'
return importExpression.split('/').slice(0, 2).join('/');
}
// For unscoped packages, the package name is up to the first '/'
return importExpression.split('/')[0];
}

0 comments on commit f1b930b

Please sign in to comment.