11import { Tree } from '@nx/devkit' ;
22import { checkRuleExists } from './check-rule-exists' ;
33
4+ const allowAll = / \s * \{ \s * s o u r c e T a g : \s * ' \* ' , \s * o n l y D e p e n d O n L i b s W i t h T a g s : \s * \[ ' \* ' \] , ? \s * \} \s * , ? / ;
5+ const depConstraints = / d e p C o n s t r a i n t s : \s * \[ \s * / ;
6+
7+
48export function updateDepConst (
59 host : Tree ,
610 update : ( depConst : Array < object > ) => void
711) {
812 let filePath = 'tslint.json' ;
9- let rule = 'nx-enforce-module-boundaries' ;
13+ const rule = 'nx-enforce-module-boundaries' ;
14+ let isJson = true ;
15+ let newText = '' ;
1016
1117 if ( ! host . exists ( 'tslint.json' ) ) {
1218 if ( host . exists ( '.eslintrc.json' ) ) {
1319 filePath = '.eslintrc.json' ;
14- rule = '@nx/enforce-module-boundaries' ;
1520 console . info ( 'Found .eslintrc.json' ) ;
1621 } else if ( host . exists ( '.eslintrc' ) ) {
1722 filePath = '.eslintrc' ;
18- rule = '@nx/enforce-module-boundaries' ;
19- console . info ( 'Did not find .eslintrc.json but found .eslintrc' ) ;
20- } else if ( host . exists ( 'eslint.config.js' ) ) {
23+ console . info ( 'Found .eslintrc' ) ;
24+ } else if ( host . exists ( 'eslint.config.cjs' ) ) {
25+ filePath = 'eslint.config.cjs' ;
26+ console . info ( 'Found .eslintrc' ) ;
27+ isJson = false ;
28+ }
29+ else if ( host . exists ( 'eslint.config.js' ) ) {
2130 console . info (
2231 'ESLint flat config will be supported in next release!'
2332 ) ;
@@ -31,20 +40,44 @@ export function updateDepConst(
3140 }
3241
3342 const text = host . read ( filePath ) . toString ( ) ;
34- const json = JSON . parse ( text ) ;
35- let rules = json ;
36- if ( rules [ 'overrides' ] ) {
37- const overrides = rules [ 'overrides' ] ;
38- rules = overrides . find (
39- ( e ) => e . rules && e . rules [ '@nx/enforce-module-boundaries' ]
40- ) ;
41- }
4243
43- if ( ! checkRuleExists ( filePath , rule , rules ) ) return ;
44+ if ( isJson ) {
45+ const json = JSON . parse ( text ) ;
46+ let rules = json ;
47+ if ( rules [ 'overrides' ] ) {
48+ const overrides = rules [ 'overrides' ] ;
49+ rules = overrides . find (
50+ ( e ) => e . rules && e . rules [ '@nx/enforce-module-boundaries' ]
51+ ) ;
52+ }
53+
54+ if ( ! checkRuleExists ( filePath , rule , rules ) ) return ;
4455
45- const depConst = rules [ 'rules' ] [ rule ] [ 1 ] [ 'depConstraints' ] as Array < object > ;
46- update ( depConst ) ;
56+ const depConst = rules [ 'rules' ] [ rule ] [ 1 ] [ 'depConstraints' ] as Array < object > ;
57+ update ( depConst ) ;
58+ newText = JSON . stringify ( json , undefined , 2 ) ;
59+
60+ }
61+ else {
62+ const rules = new Array < object > ( ) ;
63+ update ( rules ) ;
64+ const code = trim ( JSON . stringify ( rules , null , 2 ) ) + ',' ;
65+ newText = text . replace ( allowAll , '' ) ;
66+ newText = newText . replace ( depConstraints , 'depConstraints: [\n' + code ) ;
67+ }
4768
48- const newText = JSON . stringify ( json , undefined , 2 ) ;
4969 host . write ( filePath , newText ) ;
5070}
71+
72+ function trim ( str : string ) {
73+
74+ if ( str . startsWith ( '[' ) ) {
75+ str = str . substring ( 1 ) ;
76+ }
77+
78+ if ( str . endsWith ( ']' ) ) {
79+ str = str . substring ( 0 , str . length - 1 ) ;
80+ }
81+
82+ return str . trim ( ) ;
83+ }
0 commit comments