1
1
import { Tree } from '@nx/devkit' ;
2
2
import { checkRuleExists } from './check-rule-exists' ;
3
3
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
+
4
8
export function updateDepConst (
5
9
host : Tree ,
6
10
update : ( depConst : Array < object > ) => void
7
11
) {
8
12
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 = '' ;
10
16
11
17
if ( ! host . exists ( 'tslint.json' ) ) {
12
18
if ( host . exists ( '.eslintrc.json' ) ) {
13
19
filePath = '.eslintrc.json' ;
14
- rule = '@nx/enforce-module-boundaries' ;
15
20
console . info ( 'Found .eslintrc.json' ) ;
16
21
} else if ( host . exists ( '.eslintrc' ) ) {
17
22
filePath = '.eslintrc' ;
18
- rule = '@nx/enforce-module-boundaries' ;
19
- console . info ( 'Did not find .eslintrc.json but found .eslintrc' ) ;
20
- } else {
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 {
21
30
console . info (
22
31
'Cannot add linting rules: linting config file does not exist'
23
32
) ;
@@ -26,20 +35,44 @@ export function updateDepConst(
26
35
}
27
36
28
37
const text = host . read ( filePath ) . toString ( ) ;
29
- const json = JSON . parse ( text ) ;
30
- let rules = json ;
31
- if ( rules [ 'overrides' ] ) {
32
- const overrides = rules [ 'overrides' ] ;
33
- rules = overrides . find (
34
- ( e ) => e . rules && e . rules [ '@nx/enforce-module-boundaries' ]
35
- ) ;
36
- }
37
38
38
- if ( ! checkRuleExists ( filePath , rule , rules ) ) return ;
39
+ if ( isJson ) {
40
+ const json = JSON . parse ( text ) ;
41
+ let rules = json ;
42
+ if ( rules [ 'overrides' ] ) {
43
+ const overrides = rules [ 'overrides' ] ;
44
+ rules = overrides . find (
45
+ ( e ) => e . rules && e . rules [ '@nx/enforce-module-boundaries' ]
46
+ ) ;
47
+ }
48
+
49
+ if ( ! checkRuleExists ( filePath , rule , rules ) ) return ;
39
50
40
- const depConst = rules [ 'rules' ] [ rule ] [ 1 ] [ 'depConstraints' ] as Array < object > ;
41
- update ( depConst ) ;
51
+ const depConst = rules [ 'rules' ] [ rule ] [ 1 ] [ 'depConstraints' ] as Array < object > ;
52
+ update ( depConst ) ;
53
+ newText = JSON . stringify ( json , undefined , 2 ) ;
54
+
55
+ }
56
+ else {
57
+ const rules = new Array < object > ( ) ;
58
+ update ( rules ) ;
59
+ const code = trim ( JSON . stringify ( rules , null , 2 ) ) + ',' ;
60
+ newText = text . replace ( allowAll , '' ) ;
61
+ newText = newText . replace ( depConstraints , 'depConstraints: [\n' + code ) ;
62
+ }
42
63
43
- const newText = JSON . stringify ( json , undefined , 2 ) ;
44
64
host . write ( filePath , newText ) ;
45
65
}
66
+
67
+ function trim ( str : string ) {
68
+
69
+ if ( str . startsWith ( '[' ) ) {
70
+ str = str . substring ( 1 ) ;
71
+ }
72
+
73
+ if ( str . endsWith ( ']' ) ) {
74
+ str = str . substring ( 0 , str . length - 1 ) ;
75
+ }
76
+
77
+ return str . trim ( ) ;
78
+ }
0 commit comments