Skip to content

Commit

Permalink
chore(core): update nx-plugin resolution to reuse target project loca…
Browse files Browse the repository at this point in the history
…tor logic
  • Loading branch information
AgentEnder committed Apr 6, 2022
1 parent efc1f2a commit 9095e81
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 24 deletions.
31 changes: 20 additions & 11 deletions packages/nx/src/utils/nx-plugin.ts
Expand Up @@ -13,6 +13,7 @@ import {
TargetConfiguration,
WorkspaceJsonConfiguration,
} from '../config/workspace-json-project-json';
import { findMatchingProjectForPath } from './target-project-locator';

export type ProjectTargetConfigurator = (
file: string
Expand Down Expand Up @@ -181,17 +182,15 @@ function findNxProjectForImportPath(
const possiblePaths = tsConfigPaths[importPath]?.map((p) =>
path.resolve(root, p)
);
if (tsConfigPaths[importPath]) {
const projectRootMappings = Object.entries(workspace.projects).reduce(
(m, [project, config]) => {
m[path.resolve(root, config.root)] = project;
return m;
},
{}
);
for (const root of Object.keys(projectRootMappings)) {
if (possiblePaths.some((p) => !path.relative(root, p).startsWith('..'))) {
return projectRootMappings[root];
if (possiblePaths?.length) {
const projectRootMappings = buildProjectRootMap(workspace.projects, root);
for (const tsConfigPath of possiblePaths) {
const nxProject = findMatchingProjectForPath(
tsConfigPath,
projectRootMappings
);
if (nxProject) {
return nxProject;
}
}
if (process.env.NX_VERBOSE_LOGGING) {
Expand All @@ -207,6 +206,16 @@ function findNxProjectForImportPath(
}
}

function buildProjectRootMap(
projects: Record<string, ProjectConfiguration>,
root: string
) {
return Object.entries(projects).reduce((m, [project, config]) => {
m.set(path.resolve(root, config.root), project);
return m;
}, new Map<string, string>());
}

let tsconfigPaths: Record<string, string[]>;
function readTsConfigPaths(root: string = workspaceRoot) {
if (!tsconfigPaths) {
Expand Down
39 changes: 26 additions & 13 deletions packages/nx/src/utils/target-project-locator.ts
Expand Up @@ -178,30 +178,43 @@ export class TargetProjectLocator {
}

private findMatchingProjectFiles(file: string) {
for (
let currentPath = file;
currentPath != dirname(currentPath);
currentPath = dirname(currentPath)
) {
const p = this.projectRootMappings.get(currentPath);
if (p) {
return p;
}
}
return null;
const project = findMatchingProjectForPath(file, this.projectRootMappings);
return this.nodes[project];
}
}

function createProjectRootMappings(
nodes: Record<string, ProjectGraphProjectNode>
) {
const projectRootMappings = new Map();
const projectRootMappings = new Map<string, string>();
for (const projectName of Object.keys(nodes)) {
const root = nodes[projectName].data.root;
projectRootMappings.set(
root && root.endsWith('/') ? root.substring(0, root.length - 1) : root,
nodes[projectName]
projectName
);
}
return projectRootMappings;
}

/**
* Locates a project in projectRootMap based on a file within it
* @param filePath path that is inside of projectName
* @param projectRootMap Map<projectRoot, projectName>
*/
export function findMatchingProjectForPath(
filePath: string,
projectRootMap: Map<string, string>
) {
for (
let currentPath = filePath;
currentPath != dirname(currentPath);
currentPath = dirname(currentPath)
) {
const p = projectRootMap.get(currentPath);
if (p) {
return p;
}
}
return null;
}

0 comments on commit 9095e81

Please sign in to comment.