Skip to content

Commit

Permalink
fix(core): check-module-boundaries should work if single quotes not c…
Browse files Browse the repository at this point in the history
…onsumed by shell
  • Loading branch information
AgentEnder committed Nov 11, 2022
1 parent decf365 commit 3870b55
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
<NodeModulesRelativePath>$([MSBuild]::MakeRelative($(MSBuildProjectDirectory), $(RepoRoot)))</NodeModulesRelativePath>
</PropertyGroup>
<Target Name="CheckNxModuleBoundaries" BeforeTargets="Build">
<Exec Command="node $(NodeModulesRelativePath)/<%= checkModuleBoundariesScriptPath %> --project-root '$(MSBuildProjectDirRelativePath)'"/>
<Exec Command="node $(NodeModulesRelativePath)/<%= checkModuleBoundariesScriptPath %> --project-root &quot;$(MSBuildProjectDirRelativePath)&quot;"/>
</Target>
</Project>
11 changes: 4 additions & 7 deletions packages/core/src/tasks/check-module-boundaries.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,10 @@ describe('enforce-module-boundaries', () => {
it('should exit early if no tags on project', async () => {
const spy = jest.spyOn(checkModule, 'loadModuleBoundaries');
const results = await checkModuleBoundariesForProject('a', {
version: 2,
projects: {
a: {
tags: [],
targets: {},
root: '',
},
a: {
tags: [],
targets: {},
root: '',
},
});
expect(spy).not.toHaveBeenCalled();
Expand Down
84 changes: 36 additions & 48 deletions packages/core/src/tasks/check-module-boundaries.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import {
NxJsonConfiguration,
ProjectConfiguration,
readJsonFile,
ProjectsConfigurations,
Tree,
WorkspaceJsonConfiguration,
workspaceRoot,
Workspaces,
} from '@nrwl/devkit';
import { Workspaces } from '@nrwl/tao/src/shared/workspace';

import { ESLint } from 'eslint';
import { join, relative } from 'path';
import { relative } from 'path';

import {
getDependantProjectsForNxProject,
Expand All @@ -19,10 +17,10 @@ import {

export async function checkModuleBoundariesForProject(
project: string,
workspace: WorkspaceJsonConfiguration,
projects: Record<string, ProjectConfiguration>,
): Promise<string[]> {
const projectRoot = workspace.projects[project].root;
const tags = workspace.projects[project].tags ?? [];
const projectRoot = projects[project].root;
const tags = projects[project].tags ?? [];
if (!tags.length) {
return [];
}
Expand All @@ -38,7 +36,7 @@ export async function checkModuleBoundariesForProject(
const violations: string[] = [];
getDependantProjectsForNxProject(
project,
workspace,
{ version: 2, projects },
(configuration, name, implicit) => {
if (implicit) return;
const dependencyTags = configuration?.tags ?? [];
Expand Down Expand Up @@ -86,57 +84,47 @@ export async function loadModuleBoundaries(
}
}

function findProjectGivenRoot(
root: string,
projects: ProjectsConfigurations['projects'],
): string {
// Note that this returns the first matching project and would succeed for multiple (cs|fs...)proj under an nx project path,
// but getProjectFileForNxProject explicitly throws if it's not exactly one.
const normalizedRoot = root.replace(/^["'](.+(?=["']$))["']$/, '$1');
const [projectName] =
Object.entries(projects).find(([, projectConfig]) => {
const relativePath = relative(projectConfig.root, normalizedRoot);
return relativePath?.startsWith('..') === false;
}) || [];

if (projectName) {
return projectName;
} else {
console.error(
`Failed to find nx workspace project associated with dotnet project directory: ${root}`,
);
process.exit(1);
}
}

async function main() {
const parser = await import('yargs-parser');
const { project, projectRoot } = parser(process.argv.slice(2), {
alias: {
project: 'p',
},
});
string: ['project', 'projectRoot'],
}) as { project?: string; projectRoot?: string };
const workspace = new Workspaces(workspaceRoot);
const workspaceJson: WorkspaceJsonConfiguration =
const { projects }: ProjectsConfigurations =
workspace.readWorkspaceConfiguration();

// Nx v12 support
const nxJson: NxJsonConfiguration & Record<string, ProjectConfiguration> =
readJsonFile(join(workspaceRoot, 'nx.json'));
if (nxJson.projects) {
Object.entries(nxJson.projects).forEach(([name, config]) => {
const existingTags = workspaceJson.projects[name]?.tags ?? [];
workspaceJson.projects[name].tags = [
...existingTags,
...(config.tags ?? []),
];
});
}
// End Nx v12 support

let nxProject = project;
// Find the associated nx project for the msbuild project directory.
if (!project && projectRoot) {
// Note that this returns the first matching project and would succeed for multiple (cs|fs...)proj under an nx project path,
// but getProjectFileForNxProject explicitly throws if it's not exactly one.
const [projectName] =
Object.entries(workspaceJson.projects).find(([, projectConfig]) => {
const relativePath = relative(projectConfig.root, projectRoot);
return relativePath?.startsWith('..') === false;
}) || [];

if (projectName) {
nxProject = projectName;
} else {
console.error(
`Failed to find nx workspace project associated with dotnet project directory: ${projectRoot}`,
);
process.exit(1);
}
}
const nxProject: string =
project ?? findProjectGivenRoot(projectRoot as string, projects);

console.log(`Checking module boundaries for ${nxProject}`);
const violations = await checkModuleBoundariesForProject(
nxProject,
workspaceJson,
);
const violations = await checkModuleBoundariesForProject(nxProject, projects);
if (violations.length) {
violations.forEach((error) => {
console.error(error);
Expand Down
12 changes: 6 additions & 6 deletions packages/utils/src/lib/utility-functions/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import {
getProjects,
normalizePath as nxNormalizePath,
ProjectConfiguration,
ProjectsConfigurations,
Tree,
WorkspaceJsonConfiguration,
workspaceRoot,
} from '@nrwl/devkit';

Expand All @@ -27,7 +27,7 @@ export function getProjectFileForNxProjectSync(project: ProjectConfiguration) {

export function getDependantProjectsForNxProject(
targetProject: string,
workspaceConfiguration: WorkspaceJsonConfiguration,
projectsConfiguration: ProjectsConfigurations,
forEachCallback?: (
project: ProjectConfiguration & { projectFile: string },
projectName: string,
Expand All @@ -39,14 +39,14 @@ export function getDependantProjectsForNxProject(
const projectRoots: { [key: string]: string } = {};
const dependantProjects: { [key: string]: ProjectConfiguration } = {};

Object.entries(workspaceConfiguration.projects).forEach(([name, project]) => {
Object.entries(projectsConfiguration.projects).forEach(([name, project]) => {
projectRoots[name] = normalizePath(resolve(project.root));
});

const absoluteNetProjectFilePath = resolve(
workspaceRoot,
getProjectFileForNxProjectSync(
workspaceConfiguration.projects[targetProject],
projectsConfiguration.projects[targetProject],
),
);
const netProjectFilePath = relative(
Expand Down Expand Up @@ -74,15 +74,15 @@ export function getDependantProjectsForNxProject(
if (forEachCallback) {
forEachCallback(
{
...workspaceConfiguration.projects[dependency],
...projectsConfiguration.projects[dependency],
projectFile: workspaceFilePath,
},
dependency,
implicit,
);
}
dependantProjects[dependency] =
workspaceConfiguration.projects[dependency];
projectsConfiguration.projects[dependency];
}
});
}),
Expand Down

0 comments on commit 3870b55

Please sign in to comment.