Skip to content

Commit

Permalink
feat(core): use createProjectFileMappings in TargetProjectLocator (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
meeroslav committed Nov 21, 2022
1 parent 41c320c commit bd4f4a2
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 64 deletions.
2 changes: 1 addition & 1 deletion packages/angular/plugins/component-testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ function withSchemaDefaults(options: any): BrowserBuilderSchema {
*/
function getTempStylesForTailwind(ctExecutorContext: ExecutorContext) {
const mappedGraphFiles = createProjectFileMappings(
ctExecutorContext.projectGraph
ctExecutorContext.projectGraph.nodes
);
const ctProjectConfig = ctExecutorContext.projectGraph.nodes[
ctExecutorContext.projectName
Expand Down
2 changes: 1 addition & 1 deletion packages/cypress/plugins/cypress-preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export function getProjectConfigByPath(
: configFileFromWorkspaceRoot
);

const mappedGraphFiles = createProjectFileMappings(graph);
const mappedGraphFiles = createProjectFileMappings(graph.nodes);
const componentTestingProjectName =
mappedGraphFiles[normalizedPathFromWorkspaceRoot];
if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1015,7 +1015,7 @@ Violation detected in:
tags: [],
implicitDependencies: [],
architect: {},
files: [createFile(`libs/other/index.ts`)],
files: [createFile(`libs/other/src/index.ts`)],
},
},
},
Expand All @@ -1033,6 +1033,7 @@ Violation detected in:
if (importKind === 'type') {
expect(failures.length).toEqual(0);
} else {
expect(failures.length).toEqual(1);
expect(failures[0].message).toEqual(
'Imports of lazy-loaded libraries are forbidden'
);
Expand Down Expand Up @@ -1144,7 +1145,10 @@ Violation detected in:
tags: [],
implicitDependencies: [],
architect: {},
files: [createFile(`libs/mylib/src/main.ts`)],
files: [
createFile(`libs/mylib/src/main.ts`),
createFile(`libs/mylib/src/index.ts`),
],
},
},
anotherlibName: {
Expand Down Expand Up @@ -1268,7 +1272,10 @@ Violation detected in:
tags: [],
implicitDependencies: [],
architect: {},
files: [createFile(`libs/mylib/src/main.ts`, ['anotherlibName'])],
files: [
createFile(`libs/mylib/src/main.ts`, ['anotherlibName']),
createFile(`libs/mylib/src/index.ts`),
],
},
},
anotherlibName: {
Expand Down Expand Up @@ -1363,6 +1370,7 @@ Circular file chain:
architect: {},
files: [
createFile(`libs/badcirclelib/src/main.ts`, ['anotherlibName']),
createFile(`libs/badcirclelib/src/index.ts`),
],
},
},
Expand Down Expand Up @@ -1886,8 +1894,9 @@ function runRule(
): TSESLint.Linter.LintMessage[] {
(global as any).projectPath = `${process.cwd()}/proj`;
(global as any).projectGraph = projectGraph;
(global as any).projectGraphFileMappings =
createProjectFileMappings(projectGraph);
(global as any).projectGraphFileMappings = createProjectFileMappings(
projectGraph.nodes
);
(global as any).targetProjectLocator = new TargetProjectLocator(
projectGraph.nodes,
projectGraph.externalNodes
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin-nx/src/utils/project-graph-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function ensureGlobalProjectGraph(ruleName: string) {
try {
(global as any).projectGraph = readCachedProjectGraph();
(global as any).projectGraphFileMappings = createProjectFileMappings(
(global as any).projectGraph
(global as any).projectGraph.nodes
);
} catch {
const WARNING_PREFIX = `${chalk.reset.keyword('orange')('warning')}`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ describe('getTouchedProjects', () => {
it('should return a list of projects for the given changes', () => {
const fileChanges = getFileChanges(['libs/a/index.ts', 'libs/b/index.ts']);
const projects = {
a: { root: 'libs/a' },
b: { root: 'libs/b' },
c: { root: 'libs/c' },
a: { root: 'libs/a', files: [{ file: 'libs/a/index.ts' }] },
b: { root: 'libs/b', files: [{ file: 'libs/b/index.ts' }] },
c: { root: 'libs/c', files: [{ file: 'libs/c/index.ts' }] },
};
expect(
getTouchedProjects(fileChanges, buildProjectGraphNodes(projects))
Expand All @@ -34,9 +34,9 @@ describe('getTouchedProjects', () => {
it('should return projects with the root matching a whole directory name in the file path', () => {
const fileChanges = getFileChanges(['libs/a-b/index.ts']);
const projects = {
a: { root: 'libs/a' },
abc: { root: 'libs/a-b-c' },
ab: { root: 'libs/a-b' },
a: { root: 'libs/a', files: [{ file: 'libs/a/index.ts' }] },
abc: { root: 'libs/a-b-c', files: [{ file: 'libs/a-b-c/index.ts' }] },
ab: { root: 'libs/a-b', files: [{ file: 'libs/a-b/index.ts' }] },
};
expect(
getTouchedProjects(fileChanges, buildProjectGraphNodes(projects))
Expand All @@ -46,9 +46,9 @@ describe('getTouchedProjects', () => {
it('should return projects with the root matching a whole directory name in the file path', () => {
const fileChanges = getFileChanges(['libs/a-b/index.ts']);
const projects = {
aaaaa: { root: 'libs/a' },
abc: { root: 'libs/a-b-c' },
ab: { root: 'libs/a-b' },
aaaaa: { root: 'libs/a', files: [{ file: 'libs/a/index.ts' }] },
abc: { root: 'libs/a-b-c', files: [{ file: 'libs/a-b-c/index.ts' }] },
ab: { root: 'libs/a-b', files: [{ file: 'libs/a-b/index.ts' }] },
};
expect(
getTouchedProjects(fileChanges, buildProjectGraphNodes(projects))
Expand All @@ -58,8 +58,8 @@ describe('getTouchedProjects', () => {
it('should return the most qualifying match with the file path', () => {
const fileChanges = getFileChanges(['libs/a/b/index.ts']);
const projects = {
aaaaa: { root: 'libs/a' },
ab: { root: 'libs/a/b' },
aaaaa: { root: 'libs/a', files: [{ file: 'libs/a/index.ts' }] },
ab: { root: 'libs/a/b', files: [{ file: 'libs/a/b/index.ts' }] },
};
expect(
getTouchedProjects(fileChanges, buildProjectGraphNodes(projects))
Expand All @@ -69,8 +69,8 @@ describe('getTouchedProjects', () => {
it('should not return parent project if nested project is touched', () => {
const fileChanges = getFileChanges(['libs/a/b/index.ts']);
const projects = {
a: { root: 'libs/a' },
b: { root: 'libs/a/b' },
a: { root: 'libs/a', files: [{ file: 'libs/a/index.ts' }] },
b: { root: 'libs/a/b', files: [{ file: 'libs/a/b/index.ts' }] },
};
expect(
getTouchedProjects(fileChanges, buildProjectGraphNodes(projects))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,23 @@ import * as minimatch from 'minimatch';
import { TouchedProjectLocator } from '../affected-project-graph-models';
import { NxJsonConfiguration } from '../../../config/nx-json';
import { ProjectGraphProjectNode } from '../../../config/project-graph';
import {
createProjectRootMappings,
findMatchingProjectForPath,
} from '../../../utils/target-project-locator';
import { createProjectFileMappings } from '../../../utils/target-project-locator';

export const getTouchedProjects: TouchedProjectLocator = (
touchedFiles,
projectGraphNodes
): string[] => {
const projectRootMap = createProjectRootMappings(projectGraphNodes);
const allProjectFiles = createProjectFileMappings(projectGraphNodes);
const affected = [];

return touchedFiles.reduce((affected, f) => {
const matchingProject = findMatchingProjectForPath(f.file, projectRootMap);
touchedFiles.forEach((f) => {
const matchingProject = allProjectFiles[f.file];
if (matchingProject) {
affected.push(matchingProject);
}
return affected;
}, []);
});

return affected;
};

export const getImplicitlyTouchedProjects: TouchedProjectLocator = (
Expand Down
1 change: 0 additions & 1 deletion packages/nx/src/utils/nx-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { workspaceRoot } from './workspace-root';
import { readJsonFile } from '../utils/fileutils';
import {
PackageJson,
readModulePackageJson,
readModulePackageJsonWithoutFallbacks,
} from './package-json';
import { registerTsProject } from './register';
Expand Down
48 changes: 28 additions & 20 deletions packages/nx/src/utils/target-project-locator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ describe('findTargetProjectWithImport', () => {
file: 'libs/proj/index.ts',
hash: 'some-hash',
},
{
file: 'libs/proj/class.ts',
hash: 'some-hash',
},
],
proj2: [
{
Expand Down Expand Up @@ -152,79 +156,79 @@ describe('findTargetProjectWithImport', () => {
type: 'lib',
data: {
root: 'libs/proj3a',
files: [],
files: ctx.fileMap['proj3a'],
},
},
proj2: {
name: 'proj2',
type: 'lib',
data: {
root: 'libs/proj2',
files: [],
files: ctx.fileMap['proj2'],
},
},
proj: {
name: 'proj',
type: 'lib',
data: {
root: 'libs/proj',
files: [],
files: ctx.fileMap['proj'],
},
},
proj1234: {
name: 'proj1234',
type: 'lib',
data: {
root: 'libs/proj1234',
files: [],
files: ctx.fileMap['proj1234'],
},
},
proj123: {
name: 'proj123',
type: 'lib',
data: {
root: 'libs/proj123',
files: [],
files: ctx.fileMap['proj123'],
},
},
proj4ab: {
name: 'proj4ab',
type: 'lib',
data: {
root: 'libs/proj4ab',
files: [],
files: ctx.fileMap['proj4ab'],
},
},
proj5: {
name: 'proj5',
type: 'lib',
data: {
root: 'libs/proj5',
files: [],
files: ctx.fileMap['proj5'],
},
},
proj6: {
name: 'proj6',
type: 'lib',
data: {
root: 'libs/proj6',
files: [],
files: ctx.fileMap['proj6'],
},
},
proj7: {
name: 'proj7',
type: 'lib',
data: {
root: 'libs/proj7',
files: [],
files: ctx.fileMap['proj7'],
},
},
'proj1234-child': {
name: 'proj1234-child',
type: 'lib',
data: {
root: 'libs/proj1234-child',
files: [],
files: ctx.fileMap['proj1234-child'],
},
},
};
Expand Down Expand Up @@ -508,6 +512,10 @@ describe('findTargetProjectWithImport (without tsconfig.json)', () => {
file: 'libs/proj/index.ts',
hash: 'some-hash',
},
{
file: 'libs/proj/class.ts',
hash: 'some-hash',
},
],
proj2: [
{
Expand Down Expand Up @@ -580,79 +588,79 @@ describe('findTargetProjectWithImport (without tsconfig.json)', () => {
type: 'lib',
data: {
root: 'libs/proj3a',
files: [],
files: ctx.fileMap.proj3a,
},
},
proj2: {
name: 'proj2',
type: 'lib',
data: {
root: 'libs/proj2',
files: [],
files: ctx.fileMap.proj2,
},
},
proj: {
name: 'proj',
type: 'lib',
data: {
root: 'libs/proj',
files: [],
files: ctx.fileMap.proj,
},
},
proj1234: {
name: 'proj1234',
type: 'lib',
data: {
root: 'libs/proj1234',
files: [],
files: ctx.fileMap.proj1234,
},
},
proj123: {
name: 'proj123',
type: 'lib',
data: {
root: 'libs/proj123',
files: [],
files: ctx.fileMap.proj123,
},
},
proj4ab: {
name: 'proj4ab',
type: 'lib',
data: {
root: 'libs/proj4ab',
files: [],
files: ctx.fileMap.proj4ab,
},
},
proj5: {
name: 'proj5',
type: 'lib',
data: {
root: 'libs/proj5',
files: [],
files: ctx.fileMap.proj5,
},
},
proj6: {
name: 'proj6',
type: 'lib',
data: {
root: 'libs/proj6',
files: [],
files: ctx.fileMap.proj6,
},
},
proj7: {
name: 'proj7',
type: 'lib',
data: {
root: 'libs/proj7',
files: [],
files: ctx.fileMap.proj7,
},
},
'proj1234-child': {
name: 'proj1234-child',
type: 'lib',
data: {
root: 'libs/proj1234-child',
files: [],
files: ctx.fileMap['proj1234-child'],
},
},
};
Expand Down

1 comment on commit bd4f4a2

@vercel
Copy link

@vercel vercel bot commented on bd4f4a2 Nov 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-five.vercel.app
nx-dev-nrwl.vercel.app
nx.dev
nx-dev-git-master-nrwl.vercel.app

Please sign in to comment.