11import {
2- NxJsonConfiguration ,
32 ProjectConfiguration ,
4- readJsonFile ,
3+ ProjectsConfigurations ,
54 Tree ,
6- WorkspaceJsonConfiguration ,
75 workspaceRoot ,
6+ Workspaces ,
87} from '@nrwl/devkit' ;
9- import { Workspaces } from '@nrwl/tao/src/shared/workspace' ;
108
119import { ESLint } from 'eslint' ;
12- import { join , relative } from 'path' ;
10+ import { relative } from 'path' ;
1311
1412import {
1513 getDependantProjectsForNxProject ,
@@ -19,10 +17,10 @@ import {
1917
2018export async function checkModuleBoundariesForProject (
2119 project : string ,
22- workspace : WorkspaceJsonConfiguration ,
20+ projects : Record < string , ProjectConfiguration > ,
2321) : Promise < string [ ] > {
24- const projectRoot = workspace . projects [ project ] . root ;
25- const tags = workspace . projects [ project ] . tags ?? [ ] ;
22+ const projectRoot = projects [ project ] . root ;
23+ const tags = projects [ project ] . tags ?? [ ] ;
2624 if ( ! tags . length ) {
2725 return [ ] ;
2826 }
@@ -38,7 +36,7 @@ export async function checkModuleBoundariesForProject(
3836 const violations : string [ ] = [ ] ;
3937 getDependantProjectsForNxProject (
4038 project ,
41- workspace ,
39+ { version : 2 , projects } ,
4240 ( configuration , name , implicit ) => {
4341 if ( implicit ) return ;
4442 const dependencyTags = configuration ?. tags ?? [ ] ;
@@ -86,57 +84,47 @@ export async function loadModuleBoundaries(
8684 }
8785}
8886
87+ function findProjectGivenRoot (
88+ root : string ,
89+ projects : ProjectsConfigurations [ 'projects' ] ,
90+ ) : string {
91+ // Note that this returns the first matching project and would succeed for multiple (cs|fs...)proj under an nx project path,
92+ // but getProjectFileForNxProject explicitly throws if it's not exactly one.
93+ const normalizedRoot = root . replace ( / ^ [ " ' ] ( .+ (? = [ " ' ] $ ) ) [ " ' ] $ / , '$1' ) ;
94+ const [ projectName ] =
95+ Object . entries ( projects ) . find ( ( [ , projectConfig ] ) => {
96+ const relativePath = relative ( projectConfig . root , normalizedRoot ) ;
97+ return relativePath ?. startsWith ( '..' ) === false ;
98+ } ) || [ ] ;
99+
100+ if ( projectName ) {
101+ return projectName ;
102+ } else {
103+ console . error (
104+ `Failed to find nx workspace project associated with dotnet project directory: ${ root } ` ,
105+ ) ;
106+ process . exit ( 1 ) ;
107+ }
108+ }
109+
89110async function main ( ) {
90111 const parser = await import ( 'yargs-parser' ) ;
91112 const { project, projectRoot } = parser ( process . argv . slice ( 2 ) , {
92113 alias : {
93114 project : 'p' ,
94115 } ,
95- } ) ;
116+ string : [ 'project' , 'projectRoot' ] ,
117+ } ) as { project ?: string ; projectRoot ?: string } ;
96118 const workspace = new Workspaces ( workspaceRoot ) ;
97- const workspaceJson : WorkspaceJsonConfiguration =
119+ const { projects } : ProjectsConfigurations =
98120 workspace . readWorkspaceConfiguration ( ) ;
99121
100- // Nx v12 support
101- const nxJson : NxJsonConfiguration & Record < string , ProjectConfiguration > =
102- readJsonFile ( join ( workspaceRoot , 'nx.json' ) ) ;
103- if ( nxJson . projects ) {
104- Object . entries ( nxJson . projects ) . forEach ( ( [ name , config ] ) => {
105- const existingTags = workspaceJson . projects [ name ] ?. tags ?? [ ] ;
106- workspaceJson . projects [ name ] . tags = [
107- ...existingTags ,
108- ...( config . tags ?? [ ] ) ,
109- ] ;
110- } ) ;
111- }
112- // End Nx v12 support
113-
114- let nxProject = project ;
115122 // Find the associated nx project for the msbuild project directory.
116- if ( ! project && projectRoot ) {
117- // Note that this returns the first matching project and would succeed for multiple (cs|fs...)proj under an nx project path,
118- // but getProjectFileForNxProject explicitly throws if it's not exactly one.
119- const [ projectName ] =
120- Object . entries ( workspaceJson . projects ) . find ( ( [ , projectConfig ] ) => {
121- const relativePath = relative ( projectConfig . root , projectRoot ) ;
122- return relativePath ?. startsWith ( '..' ) === false ;
123- } ) || [ ] ;
124-
125- if ( projectName ) {
126- nxProject = projectName ;
127- } else {
128- console . error (
129- `Failed to find nx workspace project associated with dotnet project directory: ${ projectRoot } ` ,
130- ) ;
131- process . exit ( 1 ) ;
132- }
133- }
123+ const nxProject : string =
124+ project ?? findProjectGivenRoot ( projectRoot as string , projects ) ;
134125
135126 console . log ( `Checking module boundaries for ${ nxProject } ` ) ;
136- const violations = await checkModuleBoundariesForProject (
137- nxProject ,
138- workspaceJson ,
139- ) ;
127+ const violations = await checkModuleBoundariesForProject ( nxProject , projects ) ;
140128 if ( violations . length ) {
141129 violations . forEach ( ( error ) => {
142130 console . error ( error ) ;
0 commit comments