-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): #81 support for nx-enforce-module-boundaries
- Loading branch information
1 parent
f652fc4
commit 3fc92fd
Showing
10 changed files
with
265 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './graph/process-project-graph'; | ||
export * from './tasks/check-module-boundaries'; |
30 changes: 30 additions & 0 deletions
30
packages/core/src/migrations/1.2.0/add-module-boundaries-check/migrate.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
import { Tree } from '@nrwl/devkit'; | ||
import { | ||
getNxDotnetProjects, | ||
getProjectFileForNxProject, | ||
} from '@nx-dotnet/utils'; | ||
|
||
import { addPrebuildMsbuildTask } from '../../../generators/utils/generate-project'; | ||
|
||
import { XmlDocument } from 'xmldoc'; | ||
|
||
export default async function update(host: Tree) { | ||
const projects = getNxDotnetProjects(host); | ||
for (const [name, project] of projects.entries()) { | ||
const projectFilePath = await getProjectFileForNxProject(project); | ||
const buffer = host.read(projectFilePath); | ||
if (!buffer) { | ||
throw new Error(`Error reading file ${projectFilePath}`); | ||
} | ||
const xml = new XmlDocument(buffer.toString()); | ||
if ( | ||
!xml | ||
.childrenNamed('Target') | ||
.some((x) => x.attr['Name'] === 'CheckNxModuleBoundaries') | ||
) { | ||
addPrebuildMsbuildTask(host, { name, projectRoot: project.root }, xml); | ||
host.write(projectFilePath, xml.toString()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { appRootPath } from '@nrwl/tao/src/utils/app-root'; | ||
import { | ||
ProjectConfiguration, | ||
WorkspaceJsonConfiguration, | ||
Workspaces, | ||
} from '@nrwl/tao/src/shared/workspace'; | ||
|
||
import { ESLint } from 'eslint'; | ||
|
||
import { getDependantProjectsForNxProject } from '@nx-dotnet/utils'; | ||
import { | ||
NxJsonConfiguration, | ||
NxJsonProjectConfiguration, | ||
readJsonFile, | ||
} from '@nrwl/devkit'; | ||
|
||
type ExtendedWorkspaceJson = WorkspaceJsonConfiguration & { | ||
projects: Record<string, ProjectConfiguration & NxJsonProjectConfiguration>; | ||
}; | ||
|
||
export async function checkModuleBoundariesForProject( | ||
project: string, | ||
workspace: ExtendedWorkspaceJson, | ||
): Promise<string[]> { | ||
const projectRoot = workspace.projects[project].root; | ||
const tags = workspace.projects[project].tags ?? []; | ||
if (!tags.length) { | ||
// | ||
return []; | ||
} | ||
|
||
const { rules } = await new ESLint().calculateConfigForFile( | ||
`${projectRoot}/non-existant.ts`, | ||
); | ||
const [, moduleBoundaryConfig] = rules['@nrwl/nx/enforce-module-boundaries']; | ||
const configuredConstraints: { | ||
sourceTag: '*' | string; | ||
onlyDependOnLibsWithTags: string[]; | ||
}[] = moduleBoundaryConfig?.depConstraints ?? []; | ||
const relevantConstraints = configuredConstraints.filter( | ||
(x) => | ||
tags.includes(x.sourceTag) && !x.onlyDependOnLibsWithTags.includes('*'), | ||
); | ||
if (!relevantConstraints.length) { | ||
return []; | ||
} | ||
|
||
const violations: string[] = []; | ||
getDependantProjectsForNxProject( | ||
project, | ||
workspace, | ||
(configuration, name) => { | ||
const tags = configuration?.tags ?? []; | ||
for (const constraint of relevantConstraints) { | ||
if ( | ||
!tags.some((x) => constraint.onlyDependOnLibsWithTags.includes(x)) | ||
) { | ||
violations.push( | ||
`${project} cannot depend on ${name}. Project tag ${constraint} is not satisfied.`, | ||
); | ||
} | ||
} | ||
}, | ||
); | ||
return violations; | ||
} | ||
|
||
async function main() { | ||
const parser = await import('yargs-parser'); | ||
const { project } = parser(process.argv.slice(2), { | ||
alias: { | ||
project: 'p', | ||
}, | ||
}); | ||
const workspace = new Workspaces(appRootPath); | ||
const workspaceJson: ExtendedWorkspaceJson = | ||
workspace.readWorkspaceConfiguration(); | ||
const nxJsonProjects = readJsonFile<NxJsonConfiguration>( | ||
`${appRootPath}/nx.json`, | ||
).projects; | ||
if (nxJsonProjects) { | ||
Object.entries(nxJsonProjects).forEach(([name, config]) => { | ||
const existingTags = workspaceJson.projects[name]?.tags ?? []; | ||
workspaceJson.projects[name].tags = [ | ||
...existingTags, | ||
...(config.tags ?? []), | ||
]; | ||
}); | ||
} | ||
console.log(`Checking module boundaries for ${project}`); | ||
const violations = await checkModuleBoundariesForProject( | ||
project, | ||
workspaceJson, | ||
); | ||
if (violations.length) { | ||
violations.forEach((error) => { | ||
console.error(error); | ||
}); | ||
process.exit(1); | ||
} | ||
process.exit(0); | ||
} | ||
|
||
if (require.main === module) { | ||
process.chdir(appRootPath); | ||
main(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.